forked from ihciah/tg_channel_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_manager.go
229 lines (212 loc) · 6.31 KB
/
tg_manager.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
223
224
225
226
227
228
229
package main
import (
"fmt"
tb "github.com/ihciah/telebot"
"log"
"strconv"
"strings"
)
func (TGBOT *TelegramBot) h_addchannel(p []string, m *tb.Message) string {
if len(p) != 1 {
return "Usage: addchannel @channel_id/chat_id"
}
c, err := AddChannelIfNotExists(TGBOT, p[0])
if err != nil {
return fmt.Sprintf("Channel/Chat %s cannot be added. %s", p, err)
}
*TGBOT.Channels = append(*TGBOT.Channels, c)
go c.Push()
return fmt.Sprintf("Channel/Chat %s added.", p)
}
func (TGBOT *TelegramBot) h_delchannel(p []string, m *tb.Message) string {
if len(p) != 1 {
return "Usage: delchannel @channel_id/chat_id"
}
if err := DelChannelIfExists(TGBOT, p[0]); err != nil {
return fmt.Sprintf("Channel/Chat %s cannot be deleted.", p)
}
for i, v := range *TGBOT.Channels {
if v.ID == p[0] {
go v.Exit()
*TGBOT.Channels = append((*TGBOT.Channels)[:i], (*TGBOT.Channels)[i+1:]...)
break
}
}
return fmt.Sprintf("Channel/Chat %s deleted.", p)
}
func (TGBOT *TelegramBot) h_addfollow(p []string, m *tb.Message) string {
return TGBOT.h_user(p, m, true)
}
func (TGBOT *TelegramBot) h_delfollow(p []string, m *tb.Message) string {
return TGBOT.h_user(p, m, false)
}
func (TGBOT *TelegramBot) h_user(p []string, m *tb.Message, is_add bool) string {
if len(p) != 3 {
return "Usage: addfollow/delfollow @channel_id/chat_id site(twitter/tumblr) userid"
}
for _, v := range *TGBOT.Channels {
if v.ID == p[0] {
if !auth_user(m.Sender, *v.AdminUserIDs, TGBOT.Admins) {
return "Unauthorized."
}
module := MakeModuleLabeler().Str2Module(p[1])
if module == -1 {
return "Unsupported site."
}
if is_add {
v.AddFollowing(ModuleUser{module, p[2]})
return "Following added."
} else {
v.DelFollowing(ModuleUser{module, p[2]})
return "Following deleted."
}
}
}
return "No such channel."
}
func (TGBOT *TelegramBot) h_addadmin(p []string, m *tb.Message) string {
return TGBOT.h_admin(p, m, true)
}
func (TGBOT *TelegramBot) h_deladmin(p []string, m *tb.Message) string {
return TGBOT.h_admin(p, m, false)
}
func (TGBOT *TelegramBot) h_listadmin(p []string, m *tb.Message) string {
if len(p) != 1 {
return "Usage: listadmin @channel_id/chat_id"
}
for _, v := range *TGBOT.Channels {
if v.ID == p[0] {
if len(*v.AdminUserIDs) == 0 {
return "No admin."
}
return fmt.Sprintf("Admins for %s:\n%s", v.ID, strings.Join(*v.AdminUserIDs, "\n"))
}
}
return "No such channel."
}
func (TGBOT *TelegramBot) h_admin(p []string, m *tb.Message, is_add bool) string {
if len(p) != 2 {
return "Usage: addadmin/deladmin @channel_id/chat_id userid"
}
for _, v := range *TGBOT.Channels {
if v.ID == p[0] {
if is_add {
v.AddAdmin(p[1])
return "Admin added."
} else {
v.DelAdmin(p[1])
return "Admin deleted."
}
}
}
return "No such channel/chat."
}
func (TGBOT *TelegramBot) h_listfollow(p []string, m *tb.Message) string {
if len(p) != 1 {
return "Usage: listfollow @channel_id/chat_id"
}
for _, v := range *TGBOT.Channels {
if v.ID == p[0] {
if !auth_user(m.Sender, *v.AdminUserIDs, TGBOT.Admins) {
return "Unauthorized."
}
ret := make([]string, 0, len(*TGBOT.Channels))
for module_id, names := range *v.Followings {
ret = append(ret, fmt.Sprintf("Module: %s\nUpdateInterval: %d\nFollowings:\n%s", MakeModuleLabeler().Module2Str(module_id), (*v.PushIntervals)[module_id], strings.Join(names, "\n")))
}
if len(ret) == 0 {
return "No followings."
}
return strings.Join(ret, "\n\n")
}
}
return "No such channel"
}
func (TGBOT *TelegramBot) h_setinterval(p []string, m *tb.Message) string {
if len(p) != 3 {
return "Usage: setinterval @channel_id/chat_id site N(second)"
}
interval, err := strconv.Atoi(p[2])
if err != nil || interval <= 0 {
return "Usage: setinterval @Channel/chat_id site N(second), N should be a positive number"
}
for _, v := range *TGBOT.Channels {
if v.ID == p[0] {
if !auth_user(m.Sender, *v.AdminUserIDs, TGBOT.Admins) {
return "Unauthorized."
}
module_id := MakeModuleLabeler().Str2Module(p[1])
if module_id < 0 {
return "Unsupported site."
}
v.UpdateInterval(ModuleInterval{module_id, interval})
return "Push Interval Updated."
}
}
return "No such channel/chat"
}
func (TGBOT *TelegramBot) h_listchannel(p []string, m *tb.Message) string {
names := make([]string, 0, len(*TGBOT.Channels))
for _, v := range *TGBOT.Channels {
names = append(names, v.ID)
}
return "Channels:\n" + strings.Join(names, "\n")
}
func (TGBOT *TelegramBot) h_goback(p []string, m *tb.Message) string {
if len(p) != 3 {
return "Usage: goback @channel_id/chat_id site N(second), N=0 means reset to Now."
}
back, err := strconv.ParseInt(p[2], 10, 64)
if err != nil || back < 0 {
return "Usage: goback @Channel/chat_id site N(second), N >= 0"
}
for _, v := range *TGBOT.Channels {
if v.ID == p[0] {
if !auth_user(m.Sender, *v.AdminUserIDs, TGBOT.Admins) {
return "Unauthorized."
}
module_id := MakeModuleLabeler().Str2Module(p[1])
if module_id < 0 {
return "Unsupported site."
}
fetcher := TGBOT.CreateModule(module_id, v.ID)
if err := fetcher.GoBack(v.ID, back); err != nil {
return fmt.Sprintf("Error when go back. %s", err)
}
return fmt.Sprintf("Site %s for channel/chat %s has been set to %d seconds before.", p[1], v.ID, back)
}
}
return "No such channel/chat"
}
func (TGBOT *TelegramBot) h_getid(p []string, m *tb.Message) string {
chat_id := m.Chat.ID
chat_title := m.Chat.Title
user_id := m.Sender.ID
first_name := m.Sender.FirstName
last_name := m.Sender.LastName
username := m.Sender.Username
return fmt.Sprintf("Hi %s %s(%s) !\nYour ID: %d\n\nChat: %s\nChatID: %d", last_name, first_name, username, user_id, chat_title, chat_id)
}
func (TGBOT *TelegramBot) requireSuperAdmin(f func([]string, *tb.Message) string) func([]string, *tb.Message) string {
return func(p []string, m *tb.Message) string {
if auth_user(m.Sender, []string{}, TGBOT.Admins) {
log.Println("Authorized.")
return f(p, m)
}
log.Println("Unauthorized", m.Sender.Username)
return "Unauthorized user. Superadmin needed."
}
}
func auth_user(user *tb.User, admin_list []string, super_admin_list []string) bool {
for _, u := range admin_list {
if user.Username == u {
return true
}
}
for _, su := range super_admin_list {
if user.Username == su {
return true
}
}
return false
}