-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·222 lines (195 loc) · 6.52 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"log"
"net/http"
"strings"
"telegram-chat_bot/betypes"
"telegram-chat_bot/commands"
"telegram-chat_bot/logger"
"telegram-chat_bot/markups"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
go func() {
log.Fatalln(http.ListenAndServe(":"+betypes.GetConfig().Bot.Port, nil))
}()
bot, err := tgbotapi.NewBotAPI(betypes.GetConfig().Bot.Token)
chats := betypes.NewChats(betypes.GetConfig().Chat.Queue, betypes.GetConfig().Chat.Users)
logger.ForInfo("Authorized on account")
if err != nil {
logger.ForError(err.Error())
}
logger.ForInfo("Bot have created successfully")
var updates tgbotapi.UpdatesChannel
if !strings.EqualFold(betypes.GetConfig().Bot.Webhook, "") {
updates, err = setWebhook(betypes.GetConfig().Bot.Webhook, bot)
if err != nil {
logger.ForError(err.Error())
}
}
if updates == nil {
updates, err = setPolling(betypes.GetConfig().Bot.Polling.Offset, betypes.GetConfig().Bot.Polling.Limit,
betypes.GetConfig().Bot.Polling.Timeout, bot)
if err != nil {
logger.ForError(err.Error())
}
}
for update := range updates {
logger.ForInfo(fmt.Sprintf("Update ID - %d", update.UpdateID))
go checkUpdate(update, chats, bot)
}
}
func checkUpdate(update tgbotapi.Update, chats *betypes.Chats, bot *tgbotapi.BotAPI) {
switch {
case update.CallbackQuery != nil:
checkCallbackQuery(*update.CallbackQuery, bot)
break
case update.Message != nil:
isCommand := update.Message.IsCommand()
isUserInChat := chats.IsUserInChat(update.Message.From.ID)
switch {
case isCommand:
checkCommand(*update.Message, chats, bot)
break
case !isCommand && isUserInChat: // Message to another user.
var msg tgbotapi.Chattable
interlocutors := chats.GetInterlocutorsByUserID(update.Message.From.ID)
if interlocutors == nil {
msg = tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: int64(update.Message.From.ID)},
Text: betypes.GetTexts().Chat.NotInChat,
ParseMode: betypes.GetTexts().ParseMode,
}
_, err := bot.Send(msg)
if err != nil {
logger.ForError(err.Error())
}
return
}
for i, interlocutor := range interlocutors {
switch {
case update.Message.Audio != nil:
msg = tgbotapi.NewAudioShare(int64(interlocutor.ID), update.Message.Audio.FileID)
break
case update.Message.Document != nil:
msg = tgbotapi.NewDocumentShare(int64(interlocutor.ID), update.Message.Document.FileID)
break
case update.Message.Animation != nil:
msg = tgbotapi.NewAnimationShare(int64(interlocutor.ID), update.Message.Animation.FileID)
break
case update.Message.Photo != nil:
msg = tgbotapi.NewPhotoShare(int64(interlocutor.ID), (*update.Message.Photo)[0].FileID)
break
case update.Message.Sticker != nil:
msg = tgbotapi.NewStickerShare(int64(interlocutor.ID), update.Message.Sticker.FileID)
break
case update.Message.Video != nil:
msg = tgbotapi.NewVideoShare(int64(interlocutor.ID), update.Message.Video.FileID)
break
case update.Message.VideoNote != nil:
msg = tgbotapi.NewVideoNoteShare(int64(interlocutor.ID), update.Message.VideoNote.Length,
update.Message.VideoNote.FileID)
break
case update.Message.Voice != nil:
msg = tgbotapi.NewVoiceShare(int64(interlocutor.ID), update.Message.Voice.FileID)
break
default:
if betypes.GetConfig().Chat.Users > 2 { // If there are more than two users in the chat.
msg = tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: int64(interlocutor.ID)},
Text: fmt.Sprintf("*INTERLOCUTOR %d:* %s", i+1, update.Message.Text),
ParseMode: "MARKDOWN",
}
break
}
msg = tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: int64(interlocutor.ID)},
Text: update.Message.Text,
}
}
_, err := bot.Send(msg)
if err != nil {
logger.ForError(err.Error())
}
}
break
default:
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: int64(update.Message.From.ID)},
Text: betypes.GetTexts().Commands.Unknown.Text,
ParseMode: betypes.GetTexts().ParseMode,
}
_, err := bot.Send(msg)
if err != nil {
logger.ForError(err.Error())
}
}
}
}
func checkCommand(message tgbotapi.Message, chats *betypes.Chats, bot *tgbotapi.BotAPI) {
switch message.Command() {
case betypes.GetTexts().Commands.Start.Command:
commands.Start(*betypes.NewUser(tgbotapi.User{
ID: message.From.ID,
FirstName: message.From.FirstName,
LastName: message.From.LastName,
UserName: message.From.UserName,
LanguageCode: message.From.LanguageCode,
IsBot: message.From.IsBot}), bot)
case betypes.GetTexts().Commands.Help.Command:
commands.Help(message.From.ID, bot)
case betypes.GetTexts().Commands.Chatting.Start:
commands.StartChatting(message.From.ID, chats, bot)
case betypes.GetTexts().Commands.Chatting.Stop:
commands.StopChatting(message.From.ID, chats, bot)
case betypes.GetTexts().Commands.Settings.Command:
commands.Settings(message.From.ID, bot)
case betypes.GetTexts().Commands.Me.Command:
commands.Me(message.From.ID, bot)
default:
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: int64(message.From.ID)},
Text: betypes.GetTexts().Commands.Unknown.Text,
ParseMode: betypes.GetTexts().ParseMode,
}
_, err := bot.Send(msg)
if err != nil {
logger.ForError(err.Error())
}
}
}
func checkCallbackQuery(callbackQuery tgbotapi.CallbackQuery, bot *tgbotapi.BotAPI) {
settings := markups.Settings{}
switch {
case markups.IsThereCloseCallback(callbackQuery.Data):
_, err := bot.Send(tgbotapi.NewDeleteMessage(int64(callbackQuery.From.ID), callbackQuery.Message.MessageID))
if err != nil {
logger.ForError(err.Error())
}
case settings.IsMarkupRequest(callbackQuery.Data):
settings.SendMarkupByCallbackQuery(callbackQuery, bot)
case settings.IsCallbackForChangeUserData(callbackQuery.Data):
settings.ChangeUserDataByCallbackQuery(callbackQuery, bot)
}
}
func setPolling(offset, limit, timeout int, bot *tgbotapi.BotAPI) (tgbotapi.UpdatesChannel, error) {
u := tgbotapi.UpdateConfig{
Offset: offset,
Limit: limit,
Timeout: timeout,
}
updates, err := bot.GetUpdatesChan(u)
if err != nil {
return nil, err
}
return updates, nil
}
func setWebhook(webhook string, bot *tgbotapi.BotAPI) (tgbotapi.UpdatesChannel, error) {
_, err := bot.SetWebhook(tgbotapi.NewWebhook(webhook))
if err != nil {
return nil, err
}
updates := bot.ListenForWebhook("/")
return updates, nil
}