-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgHandle.go
195 lines (176 loc) · 5.54 KB
/
tgHandle.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
//tgHandle.go
//Handles all callbacks for the bot. Including the slash command and inline keyboard buttons.
package main
import (
"strconv"
"strings"
"go.mongodb.org/mongo-driver/bson"
tgAPI "gopkg.in/telebot.v3"
)
// Handlers From Telegram
func findSubOrUnsubKeyboard(chatID int64) [][]tgAPI.InlineButton {
var keyboardToSend [][]tgAPI.InlineButton
if users.itemExists(bson.M{"chatId": chatID}) {
keyboardToSend = keyboards["mainUnsub"]
} else {
keyboardToSend = keyboards["mainSub"]
}
return keyboardToSend
}
// Handles the /start command and returns an inline keyboard to the user
func handleStart(ctx tgAPI.Context) error {
if ctx.Message().Chat.Type == "channel" || ctx.Message().Chat.Type == "privatechannel" {
return nil
}
ctx.Send(config.MainBot.WelcomeMsg, &tgAPI.ReplyMarkup{
InlineKeyboard: findSubOrUnsubKeyboard(ctx.Message().Chat.ID),
})
return nil
}
// Handles when the bot is added to a group.
// TODO: Unify above function into one.
func handleGroupAdd(ctx tgAPI.Context) error {
ctx.Send(config.MainBot.GroupAddMsg, &tgAPI.ReplyMarkup{
InlineKeyboard: findSubOrUnsubKeyboard(ctx.Message().Chat.ID),
})
return nil
}
// If a chat is migrated, handle migration by updating group ID in database
func handleMigration(ctx tgAPI.Context) error {
to, from := ctx.Migration()
if !users.itemExists(bson.M{"chatId": from}) {
return nil
}
users.update(bson.M{"chatId": from}, bson.M{"$set": bson.M{"chatId": to}})
return nil
}
//May be used at a later time
// //Checks if user using commmand is an owner. And sends a test image to check image genration, etc.
// func handleTest(ctx tgAPI.Context) error {
// owners := strings.Split(config.MainBot.Owners, ",")
// for i := range owners {
// ownerID, _ := strconv.Atoi(owners[i])
// if ownerID == int(ctx.Sender().ID) {
// img := createImg()
// photo := tgAPI.Photo{File: tgAPI.FromReader(img.ImgReader)}
// err := ctx.Send(photo)
// if err != nil {
// log.Println(err)
// }
// }
// }
// return nil
// }
// Handles subscribe button event
func handleSubBtn(ctx tgAPI.Context) error {
//Calls helper to make sure user has permissions to subscribe the group to the bot
if ctx.Chat().Type == tgAPI.ChatGroup {
if shouldContinue := handleChatUser(ctx); !shouldContinue {
return nil
}
}
status := handleSub(ctx.Chat().ID, ctx.Message().FromGroup())
if status {
handleBtnClick(config.MainBot.SubMsg, keyboards["back"], ctx)
//Messages the owners in the config when subscribe is successful
if config.MessageOnSub {
owners := strings.Split(config.MainBot.Owners, ",")
for i := range owners {
idToSend, _ := strconv.Atoi(owners[i])
bot.Send(&tgAPI.User{
ID: int64(idToSend),
}, ctx.Message().Chat.Username+" Subscribed!")
}
}
} else {
handleBtnClick("I'm sorry, you are already subscribed.", keyboards["back"], ctx)
}
return nil
}
// Handles an unsubscribe button click
func handleUnsubBtn(ctx tgAPI.Context) error {
//Calls helper to make sure user has permissions to subscribe the group to the bot
if ctx.Message().FromGroup() {
if shouldContinue := handleChatUser(ctx); !shouldContinue {
return nil
}
}
status := handleUnsub(ctx.Message().Chat.ID)
if status {
handleBtnClick("Unsubscribed", keyboards["back"], ctx)
} else {
handleBtnClick("I'm sorry you are not unsubscribed", keyboards["back"], ctx)
}
return nil
}
// Button handle for command
func handleCommandBtn(ctx tgAPI.Context) error {
handleBtnClick("Commands:", keyboards["cmd"], ctx)
return nil
}
// Button handle for Home
func handleHomeBtn(ctx tgAPI.Context) error {
handleBtnClick(config.MainBot.WelcomeMsg, findSubOrUnsubKeyboard(ctx.Message().Chat.ID), ctx)
return nil
}
// Button handle for info
func handleInfoBtn(ctx tgAPI.Context) error {
var totalUsers []user
users.findAll(bson.M{}, &totalUsers)
sendString := "Bot created by @JustAnOpossum" + "\n\nUsers Subscribed: " + strconv.Itoa(len(totalUsers))
handleBtnClick(sendString, keyboards["back"], ctx)
return nil
}
// Button handle for days
func handleDaysBtn(ctx tgAPI.Context) error {
dayStr := strconv.Itoa(getDays(config.Date)) + " Days Until " + config.Con + "!"
if getDays(config.Date) == 1 {
dayStr = strconv.Itoa(getDays(config.Date)) + " Day Until " + config.Con + "!"
}
if getDays(config.Date) < 1 {
dayStr = "Have fun at " + config.Con + "!"
}
handleBtnClick(dayStr, keyboards["back"], ctx)
return nil
}
// Handles a user or group subscription to that database
func handleSub(chatID int64, isGroup bool) bool {
if users.itemExists(bson.M{"chatId": chatID}) {
return false
}
itemToInsert := user{
ChatID: chatID,
Group: isGroup,
}
users.insert(itemToInsert)
return true
}
func handleUnsub(chatID int64) bool {
if !users.itemExists(bson.M{"chatId": chatID}) {
return false
}
users.removeOne(bson.M{"chatId": chatID})
return true
}
// Helper function for subscibing a user of a group to the bot
func handleChatUser(ctx tgAPI.Context) bool {
chatMember, err := bot.ChatMemberOf(ctx.Chat(), ctx.Sender())
if err != nil {
handleBtnClick("An Error Occured", keyboards["back"], ctx)
handleErr(err)
return false
}
isAdmin := checkForAdmin(chatMember)
if !isAdmin {
handleBtnClick("You don't have permissoins to subscribe this group. Please ask a group admin or owner to subscribe the bot.", keyboards["back"], ctx)
return false
}
return true
}
// Helper function to make sure the user trying to subscribe is an admin of the group.
func checkForAdmin(chatMember *tgAPI.ChatMember) bool {
if chatMember.Role == "creator" || chatMember.Role == "administrator" {
return true
}
return false
}