-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
165 lines (139 loc) · 7.37 KB
/
endpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package scenes
import (
"encoding/json"
"log"
"strings"
chatbot "github.com/green-api/whatsapp-chatbot-golang"
"github.com/green-api/whatsapp-demo-chatbot-golang/model"
"github.com/green-api/whatsapp-demo-chatbot-golang/util"
)
type EndpointsScene struct {
}
func (s EndpointsScene) Start(bot *chatbot.Bot) {
bot.IncomingMessageHandler(func(message *chatbot.Notification) {
if !util.IsSessionExpired(message) {
lang := message.GetStateData()["lang"].(string)
text, _ := message.Text()
senderName := message.Body["senderData"].(map[string]interface{})["senderName"].(string)
senderId, _ := message.Sender()
botNumber := message.Body["instanceData"].(map[string]interface{})["wid"].(string)
if message.Filter(map[string][]string{"messageType": {"pollUpdateMessage"}}) {
s.processPollUpdate(message, lang, senderId)
}
switch text {
case "1":
message.SendText(util.GetString([]string{"send_text_message", lang})+util.GetString([]string{"links", lang, "send_text_documentation"}), "false")
case "2":
message.SendUrlFile(
"https://storage.yandexcloud.net/sw-prod-03-test/ChatBot/corgi.pdf",
"corgi.pdf",
util.GetString([]string{"send_file_message", lang})+util.GetString([]string{"links", lang, "send_file_documentation"}))
case "3":
message.SendUrlFile(
"https://storage.yandexcloud.net/sw-prod-03-test/ChatBot/corgi.jpg",
"corgi.jpg",
util.GetString([]string{"send_image_message", lang})+util.GetString([]string{"links", lang, "send_file_documentation"}))
case "4":
message.SendText(util.GetString([]string{"send_audio_message", lang})+util.GetString([]string{"links", lang, "send_file_documentation"}), "false")
var fileLink = "https://storage.yandexcloud.net/sw-prod-03-test/ChatBot/Audio_bot_eng.mp3"
if lang == "ru" {
fileLink = "https://storage.yandexcloud.net/sw-prod-03-test/ChatBot/Audio_bot.mp3"
}
message.SendUrlFile(fileLink, "audio.mp3", "")
case "5":
var fileLink = "https://storage.yandexcloud.net/sw-prod-03-test/ChatBot/Video_bot_eng.mp4"
if lang == "ru" {
fileLink = "https://storage.yandexcloud.net/sw-prod-03-test/ChatBot/Video_bot_ru.mp4"
}
message.SendUrlFile(fileLink, "video.mp4",
util.GetString([]string{"send_video_message", lang})+util.GetString([]string{"links", lang, "send_file_documentation"}))
case "6":
message.SendText(util.GetString([]string{"send_contact_message", lang})+util.GetString([]string{"links", lang, "send_contact_documentation"}), "false")
message.SendContact(map[string]interface{}{"firstName": senderName, "phoneContact": strings.ReplaceAll(senderId, "@c.us", "")})
case "7":
message.SendText(util.GetString([]string{"send_location_message", lang})+util.GetString([]string{"links", lang, "send_location_documentation"}), "false")
message.SendLocation("", "", 35.888171, 14.440230)
case "8":
message.SendText(util.GetString([]string{"send_poll_message", lang})+
util.GetString([]string{"links", lang, "send_poll_as_buttons"})+
util.GetString([]string{"send_poll_message_1", lang})+
util.GetString([]string{"links", lang, "send_poll_documentation"}), "false")
message.SendPoll(util.GetString([]string{"poll_question", lang}), false,
[]map[string]interface{}{
{"optionName": util.GetString([]string{"poll_option_1", lang})},
{"optionName": util.GetString([]string{"poll_option_2", lang})},
{"optionName": util.GetString([]string{"poll_option_3", lang})},
})
case "9":
message.SendText(util.GetString([]string{"get_avatar_message", lang})+util.GetString([]string{"links", lang, "get_avatar_documentation"}), "false")
avatar, _ := message.GreenAPI.Methods().Service().GetAvatar(senderId)
if avatar["urlAvatar"] != "" {
message.SendUrlFile(
avatar["urlAvatar"].(string),
"avatar",
util.GetString([]string{"avatar_found", lang}))
} else {
message.SendText(util.GetString([]string{"avatar_not_found", lang}))
}
case "10":
message.SendText(util.GetString([]string{"send_link_message_preview", lang}) + util.GetString([]string{"links", lang, "send_link_documentation"}))
message.SendText(util.GetString([]string{"send_link_message_no_preview", lang})+util.GetString([]string{"links", lang, "send_link_documentation"}), "false")
case "11":
message.SendText(util.GetString([]string{"add_to_contact", lang}), "false")
message.SendContact(map[string]interface{}{"firstName": util.GetString([]string{"bot_name", lang}), "phoneContact": strings.ReplaceAll(botNumber, "@c.us", "")})
message.ActivateNextScene(CreateGroupScene{})
case "12":
message.AnswerWithText(util.GetString([]string{"send_quoted_message", lang})+util.GetString([]string{"links", lang, "send_quoted_message_documentation"}), "false")
case "13":
message.SendUrlFile("https://raw.githubusercontent.com/green-api/whatsapp-demo-chatbot-golang/refs/heads/master/assets/about_go.jpg", "logo.jpg",
util.GetString([]string{"about_go_chatbot", lang})+
util.GetString([]string{"link_to_docs", lang})+
util.GetString([]string{"links", lang, "chatbot_documentation"})+
util.GetString([]string{"link_to_source_code", lang})+
util.GetString([]string{"links", lang, "chatbot_source_code"})+
util.GetString([]string{"link_to_green_api", lang})+
util.GetString([]string{"links", lang, "greenapi_website"})+
util.GetString([]string{"link_to_console", lang})+
util.GetString([]string{"links", lang, "greenapi_console"})+
util.GetString([]string{"link_to_youtube", lang})+
util.GetString([]string{"links", lang, "youtube_channel"}))
case "стоп", "Стоп", "stop", "Stop", "0":
message.SendText(util.GetString([]string{"stop_message", lang})+"*"+senderName+"*!", "false")
message.ActivateNextScene(StartScene{})
case "menu", "меню", "Menu", "Меню":
var welcomeFileURL string
if lang == "en" {
welcomeFileURL = "https://raw.githubusercontent.com/green-api/whatsapp-demo-chatbot-golang/refs/heads/master/assets/welcome_en.jpg"
} else {
welcomeFileURL = "https://raw.githubusercontent.com/green-api/whatsapp-demo-chatbot-golang/refs/heads/master/assets/welcome_ru.jpg"
}
message.SendUrlFile(welcomeFileURL, "welcome.jpg", util.GetString([]string{"menu", lang}))
case "":
default:
message.SendText(util.GetString([]string{"not_recognized_message", lang}), "false")
}
} else {
message.ActivateNextScene(MainMenuScene{})
message.SendText(util.GetString([]string{"select_language"}), "false")
}
})
}
func (s EndpointsScene) processPollUpdate(message *chatbot.Notification, lang string, senderId string) {
webhookBody, _ := json.Marshal(message.Body)
var pollMessage model.PollMessage
if err := json.Unmarshal(webhookBody, &pollMessage); err != nil {
log.Fatal(err)
}
isYes := util.ContainString(pollMessage.MessageData.PollMessageData.Votes[0].OptionVoters, senderId)
isNo := util.ContainString(pollMessage.MessageData.PollMessageData.Votes[1].OptionVoters, senderId)
isNothing := util.ContainString(pollMessage.MessageData.PollMessageData.Votes[2].OptionVoters, senderId)
var messageText string
if isYes {
messageText = util.GetString([]string{"poll_answer_1", lang})
} else if isNo {
messageText = util.GetString([]string{"poll_answer_2", lang})
} else if isNothing {
messageText = util.GetString([]string{"poll_answer_3", lang})
}
message.SendText(messageText)
}