-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
272 lines (225 loc) · 5.63 KB
/
telegram.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package telegram
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"github.com/tonybounty/longpoll"
)
const (
urlApiTelegram = "https://api.telegram.org/"
)
type Telegram struct {
// URL to the Telegram BOT API, eg https://api.telegram.org/botTOKEN/
BotURL string
// offset parameter for getUpdates
LastOffset string
Timeout int
// PollService longpoll.LongPoll
EndPoll chan struct{}
LoopStarted bool
cancelRequest context.CancelFunc
Commands []Command
OtherTextFunc func(msg *MessageTlg)
}
func New(token string) *Telegram {
return &Telegram{
BotURL: urlApiTelegram + "bot" + token + "/",
OtherTextFunc: nil,
LoopStarted: false,
Timeout: 120,
}
}
func (t *Telegram) decodeJson(jsonData []byte, structData interface{}) error {
err := json.Unmarshal(jsonData, structData)
if err != nil {
return fmt.Errorf("%v", err)
}
return nil
}
// checkReceiveErrTlg check if there is no error from API
func (t *Telegram) checkReceiveErrTlg(recv *ReceiveTlg) error {
if recv.Ok != true {
return fmt.Errorf("telegram api: %s", recv.Description)
}
return nil
}
// popMessage pop & return Message struct and update id from ReceiveTlg struct
func (t *Telegram) popMessage(recv *ReceiveTlg) (updateid int, message MessageTlg, valid bool) {
if len(recv.Result) == 0 {
return -1, MessageTlg{}, false
}
msgid := recv.Result[0].Message.MessageID
msgEdid := recv.Result[0].EditedMessage.MessageID
if msgEdid == 0 && msgid == 0 {
return -1, MessageTlg{}, false
}
var msg MessageTlg
if msgid > 0 {
msg = recv.Result[0].Message
} else {
msg = recv.Result[0].EditedMessage
}
id := recv.Result[0].UpdateId
recv.Result = recv.Result[1:]
return id, msg, true
}
func (t *Telegram) poll(ctx context.Context, url string,
outcomingMsg chan *MessageTlg, done chan struct{}) {
r, err := longpoll.RunWithParm(ctx, url, map[string]string{
"offset": t.LastOffset,
"timeout": strconv.Itoa(t.Timeout),
})
if err != nil {
log.Println("Error polling: ", err)
return
}
recv := &ReceiveTlg{}
err = t.decodeJson(r, recv)
if err != nil {
fmt.Println("decode error: ", err)
return
}
if err := t.checkReceiveErrTlg(recv); err != nil {
log.Println("Error api: ", err)
return
}
for {
updateid, message, valid := t.popMessage(recv)
if !valid {
break
}
// sending message to startPollLoop
outcomingMsg <- &message
// updating offset
t.LastOffset = strconv.Itoa(updateid + 1)
}
done <- struct{}{}
}
// Start or restart long polling loop for waiting message from telegram server
// Emit signal on EndPoll chan when server loop is stopped
func (t *Telegram) Start() (EndPoll <-chan struct{}) {
t.EndPoll = make(chan struct{})
t.startLoopHandler()
t.LoopStarted = true
return t.EndPoll
}
// Stop current polling, stop also the current request.
func (t *Telegram) Stop() {
t.cancelRequest()
t.LoopStarted = false
}
func (t *Telegram) startLoopHandler() {
url := t.BotURL + "getUpdates"
// context is used to stop http request in long polling loop
ctx, cancelPoll := context.WithCancel(context.Background())
t.cancelRequest = cancelPoll
incomingMsg := make(chan *MessageTlg)
done := make(chan struct{})
go func() {
go t.poll(ctx, url, incomingMsg, done)
for {
select {
case <-ctx.Done():
log.Println("context.Done() cancel called")
close(t.EndPoll)
return
case msg := <-incomingMsg:
go t.dispatchMessage(*msg)
case <-done:
go t.poll(ctx, url, incomingMsg, done)
}
}
}()
}
func (t *Telegram) BindCommand(cmd *Command) {
t.Commands = append(t.Commands, *cmd)
}
func (t *Telegram) BindOtherText(callback func(msg *MessageTlg)) {
t.OtherTextFunc = callback
}
func (t *Telegram) handleCommand(msg *MessageTlg) {
cmdName := GetCommandName(msg)
var command *Command
// search for command
for _, cmd := range t.Commands {
if cmd.CmdName == cmdName {
command = &cmd
break
}
}
if command == nil {
log.Printf("Command '%s' not found\n", cmdName)
return
}
cmdInfo, err := command.GetInfo(msg)
if err != nil {
log.Println("handleCommand error: ", err)
return
}
command.Callback(cmdInfo)
}
func (t *Telegram) dispatchMessage(msg MessageTlg) {
if len(msg.Entities) > 0 {
if msg.Entities[0].Type == "bot_command" {
t.handleCommand(&msg)
return
}
}
// if not a command
if t.OtherTextFunc != nil {
t.OtherTextFunc(&msg)
}
}
func (t *Telegram) addAuthorizedId(id int) {
}
func (t *Telegram) delAuthorizedId(id int) {
}
func (t *Telegram) addAdminId(id int) {
}
func (t *Telegram) delAdminId(id int) {
}
func (t *Telegram) getMe() {
}
func (t *Telegram) SendChatMessage(chatID int, text string) error {
return t.SendMessageParam(map[string]string{
"chat_id": strconv.Itoa(chatID),
"text": text,
})
}
func (t *Telegram) ReplyChatMessage(chatID int, msgID int, text string) error {
return t.SendMessageParam(map[string]string{
"chat_id": strconv.Itoa(chatID),
"reply_to_message_id": strconv.Itoa(msgID),
"text": text,
})
}
func (t *Telegram) SendMessageParam(param map[string]string) error {
botUrl := t.BotURL + "sendMessage?"
for paramName, value := range param {
botUrl += "&" + paramName + "=" + url.QueryEscape(value)
}
log.Println("sending message with botUrl:", botUrl)
res, err := http.Get(botUrl)
if err != nil {
return err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
d := ReceiveReturnMSgTlg{}
err = t.decodeJson(b, &d)
if err != nil {
return fmt.Errorf("json decode: %v", err)
}
if !d.Ok || res.StatusCode != http.StatusOK {
return fmt.Errorf("api error: %s", d.Description)
}
return nil
}