-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_bot.go
174 lines (148 loc) · 3.84 KB
/
telegram_bot.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
package mail
import (
"strings"
"sync"
"github.com/google/uuid"
"gitlab.com/toby3d/telegram"
)
type tgEntity struct {
sync.RWMutex
chans map[string][]int64
}
type tgReg map[string]*tgEntity
type tgMultibotSubscribers struct {
sync.RWMutex
bots tgReg
}
var tgRegister = tgMultibotSubscribers{
bots: make(tgReg),
}
const tgWelcomeMsg = `Hello.
To create a channel and get a new channelUUID, use /create.
Note: You'll be autosubscribed to your new channel.
To subscribe to a channel, use /subscribe channelUUID
` + FullVersion
func (tg *TelegramSubSender) serveBot() {
// we should allow only message update
updates := tg.bot.NewLongPollingChannel(&telegram.GetUpdatesParameters{
AllowedUpdates: []string{"message"},
})
// TODO: consider a webhook-based sender bot.NewWebhookChannel() and tg login
for update := range updates {
// bullet-proof double check if it is a message
if !update.IsMessage() {
// not a message
tg.log.
WithField("got", update.Type()).
Warn("got non-message update with filtered message-only request")
continue
}
// Ok, it's definitely a message. From here on use
// only this struct to avoid unnecessary panics
u := update.Message
go tg.processUpdate(u)
}
}
func (tg *TelegramSubSender) warmup() {
tg.bot, tg.botInitErr = telegram.New(tg.AccessToken)
if tg.botInitErr == nil {
tg.log = log.WithField("submod", "TelegramSubSender")
// accessToken can be mutated or removed after initialization
// but we should track the same bot anyway
tg.botKey = tg.AccessToken
tgRegister.Lock()
if _, ok := tgRegister.bots[tg.botKey]; !ok {
tgRegister.bots[tg.botKey] = &tgEntity{
chans: make(map[string][]int64),
}
}
tgRegister.Unlock()
go tg.serveBot()
}
}
func (tg *TelegramSubSender) Warmup() error {
if len(tg.AccessToken) == 0 {
NewCredReqErr("AccessToken", "TelegramSub")
}
tg.initBot.Do(tg.warmup)
return tg.botInitErr
}
func (tg *TelegramSubSender) sendMsg(msg *telegram.SendMessageParameters) (err error) {
for i := 0; i < 8; i++ {
_, err = tg.bot.SendMessage(msg)
if err == nil {
return nil
}
}
return err
}
func (tg *TelegramSubSender) processUpdate(u *telegram.Message) {
// we work only in private chats
if !u.Chat.IsPrivate() {
tg.sendMsg(telegram.NewMessage(u.Chat.ID, "OH MY... So much humans!"))
tg.sendMsg(telegram.NewMessage(u.Chat.ID, "/me scared"))
tg.bot.LeaveChat(u.Chat.ID)
tg.log.Warn("not a private chat")
return
}
// check if this command send to the bot
if !tg.bot.IsCommandToMe(u) {
// nope, this msg seems to be sent to anyone else
return
}
switch {
case u.IsCommandEqual("start"):
tg.sendMsg(telegram.NewMessage(u.Chat.ID, tgWelcomeMsg))
case u.IsCommandEqual("create"):
tgRegister.RLock()
botReg := tgRegister.bots[tg.botKey]
tgRegister.RUnlock()
botReg.Lock()
for {
chanUUID := uuid.New().String()
if _, ok := botReg.chans[chanUUID]; !ok {
botReg.chans[chanUUID] = []int64{
u.Chat.ID,
}
tg.sendMsg(telegram.NewMessage(
u.Chat.ID,
"Ok, we've got you covered. Here's your new channel UUID: "+chanUUID,
))
break
}
}
botReg.Unlock()
case u.IsCommandEqual("subscribe"):
uuid := u.CommandArgument()
if len(strings.TrimSpace(uuid)) == 0 {
tg.sendMsg(telegram.NewMessage(
u.Chat.ID, "Subscribe to... what?",
))
return
}
tgRegister.RLock()
botReg := tgRegister.bots[tg.botKey]
tgRegister.RUnlock()
botReg.Lock()
if _, ok := botReg.chans[uuid]; !ok {
tg.sendMsg(telegram.NewMessage(
u.Chat.ID,
"Wait, I don't have such channel",
))
botReg.Unlock()
return
}
for _, v := range botReg.chans[uuid] {
if v == u.Chat.ID {
tg.sendMsg(telegram.NewMessage(
u.Chat.ID,
"You already subscribed --.",
))
botReg.Unlock()
return
}
}
botReg.chans[uuid] = append(botReg.chans[uuid], u.Chat.ID)
botReg.Unlock()
}
}