-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateloop.go
73 lines (57 loc) · 1.59 KB
/
updateloop.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
package telebot
import (
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"strconv"
)
// Parse Telegram API response body to get the slice of updates.
func parseTelegramGetUpdateResponse(r *http.Response) ([]Update, error) {
var getUpdateResponse GetUpdateResponse
// Try to parse responde body.
if err := json.NewDecoder(r.Body).Decode(&getUpdateResponse); err != nil {
log.Printf("Could not decode incoming response %s", err.Error())
return nil, err
}
if !getUpdateResponse.Ok {
log.Printf("Telegram response was not ok")
return nil, errors.New("telegram response was not ok")
}
return getUpdateResponse.Result, nil
}
//Fetch and dispatch last updates for a Bot with Telegram /getUpdates API endpoint.
func (b *Bot) getUpdates(offset int) int {
// Get Updates with Telegram /getUpdates API.
res, err := http.PostForm(
telegramApiBaseUrl+b.apiToken+getUpdatesEndpoint,
url.Values{
"offset": {strconv.Itoa(offset)},
},
)
// In case of connection error.
if err != nil {
log.Printf("Error fetching updates: %s", err.Error())
return offset
}
defer res.Body.Close()
// Parse Telegram response.
updates, err := parseTelegramGetUpdateResponse(res)
// Handle parsing errors.
if err != nil {
log.Printf("Error parsing update, %s", err.Error())
return offset
}
// Dispatch fetched updates to handlers.
for _, update := range updates {
b.dispatchUpdate(&update)
}
// If Updates were received, we must update offset
if len(updates) > 0 {
newOffset := updates[len(updates)-1].UpdateId + 1
return newOffset
}
// If not offset stay the same
return offset
}