-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.go
144 lines (117 loc) · 3.37 KB
/
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
package tgo
import (
"errors"
"net/http"
"sync"
"syscall"
"time"
)
// Sendable is an interface that represents any object that can be sent using an API client.
type Sendable interface {
// GetChatID returns the chat ID associated with the sendable object.
GetChatID() ChatID
// SetChatID sets the chat ID for the sendable object.
SetChatID(id int64)
// Send sends the sendable object using the provided API client.
// It returns the sent message and any error encountered.
Send(api *API) (*Message, error)
}
// ParseModeSettable is an interface that represents any object that can have its ParseMode set
// Or in other words, messages with captions.
type ParseModeSettable interface {
GetParseMode() ParseMode
SetParseMode(mode ParseMode)
}
// Replyable is an interface that represents any object that can be replied to.
type Replyable interface {
Sendable
SetReplyToMessageId(id int64)
}
type Filter interface{ Check(update *Update) bool }
type Router interface {
Setup(bot *Bot) error
HandleUpdate(bot *Bot, upd *Update) (used bool)
}
type Bot struct {
*API // embedding the api to add all api methods to the bot
DefaultParseMode ParseMode
asks map[string]chan<- *Message
askMut sync.RWMutex
routers []Router
// contains user-ids with their session
sessions sync.Map
}
type Options struct {
Host string
Client *http.Client
DefaultParseMode ParseMode
}
func NewBot(token string, opts Options) (bot *Bot) {
api := NewAPI(token, opts.Host, opts.Client)
return &Bot{
API: api,
DefaultParseMode: opts.DefaultParseMode,
asks: make(map[string]chan<- *Message),
}
}
// GetSession returns the stored session as a sync.Map.
// it creates a new session if session id didn't exists.
func (bot *Bot) GetSession(sessionID int64) *sync.Map {
result, ok := bot.sessions.Load(sessionID)
if ok {
return result.(*sync.Map)
}
session := &sync.Map{}
bot.sessions.Store(sessionID, session)
return session
}
func (bot *Bot) AddRouter(router Router) error {
if err := router.Setup(bot); err != nil {
return err
}
bot.routers = append(bot.routers, router)
return nil
}
// Send sends a message with the preferred ParseMode.
func (b *Bot) Send(msg Sendable) (*Message, error) {
if x, ok := msg.(ParseModeSettable); ok {
if x.GetParseMode() == ParseModeNone {
x.SetParseMode(b.DefaultParseMode)
}
}
return msg.Send(b.API)
}
// StartPolling does an infinite GetUpdates with the timeout of the passed timeoutSeconds.
// allowedUpdates by default passes nothing and uses the telegram's default.
//
// see tgo.GetUpdate for more detailed information.
func (bot *Bot) StartPolling(timeoutSeconds int64, allowedUpdates ...string) error {
var offset int64
for {
data, err := bot.GetUpdates(&GetUpdates{
Offset: offset, // Is there any better way to do this? open an issue/pull-request if you know. thx.
Timeout: timeoutSeconds,
AllowedUpdates: allowedUpdates,
})
if err != nil {
if errors.Is(err, syscall.ECONNRESET) {
time.Sleep(time.Second / 2)
continue
}
return err
}
for _, update := range data {
offset = update.UpdateId + 1
go func(update *Update) {
if update.Message != nil && bot.sendAnswerIfAsked(update.Message) {
return
}
for _, router := range bot.routers {
if used := router.HandleUpdate(bot, update); used {
return
}
}
}(update)
}
}
}