/
start_polling.go
executable file
·55 lines (53 loc) · 1.23 KB
/
start_polling.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
package gobotapi
import (
"errors"
"github.com/GoBotApiOfficial/gobotapi/methods"
"github.com/GoBotApiOfficial/gobotapi/types"
"time"
)
func (ctx *PollingClient) Start() error {
if ctx.isRunning {
return errors.New("client is already running")
}
if ctx.PollingTimeout == 0 {
ctx.PollingTimeout = time.Second * 15
}
ctx.BasicClient.setup()
if ctx.NoUpdates {
return nil
}
ctx.logging.Info(nil, "Connecting...")
res, err := ctx.Invoke(&methods.GetMe{})
if err != nil {
return err
}
me := res.Result.(types.User)
ctx.me = &me
ctx.logging.Info(ctx.Client, "Connected")
go func() {
for {
getUpdates := &methods.GetUpdates{
AllowedUpdates: ctx.AllowedUpdates,
Timeout: int(ctx.PollingTimeout.Seconds()),
Offset: ctx.lastUpdateID,
}
rawUpdates, errUpdate := ctx.Invoke(getUpdates)
if !ctx.isRunning {
break
}
if errUpdate != nil {
time.Sleep(time.Second * 5)
continue
}
updates := rawUpdates.Result.([]types.Update)
if len(updates) > 0 {
ctx.logging.Info(ctx.Client, "Received", len(updates), "updates")
}
for _, update := range updates {
ctx.lastUpdateID = int(update.UpdateID) + 1
ctx.handleUpdate(ctx.me, ctx.Token, update)
}
}
}()
return nil
}