forked from gldmtr/bot-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
298 lines (256 loc) · 8.05 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package botgolang
/*
💥 botgolang is zero-configuration library with convenient interface.
Crafted with love in @mail for your awesome bots.
*/
import (
"context"
"fmt"
"net/http"
"github.com/sirupsen/logrus"
)
const (
defaultAPIURL = "https://api.icq.net/bot/v1"
defaultDebug = false
)
// Bot is the main structure for interaction with API.
// All fields are private, you can configure bot using config arguments in NewBot func.
type Bot struct {
client *Client
updater *Updater
logger *logrus.Logger
Info *BotInfo
}
// GetInfo returns information about bot:
// id, name, about, avatar
func (b *Bot) GetInfo() (*BotInfo, error) {
return b.client.GetInfo()
}
// GetChatInfo returns information about chat:
// id, type, title, public, group, inviteLink, admins
func (b *Bot) GetChatInfo(chatID string) (*Chat, error) {
return b.client.GetChatInfo(chatID)
}
// SendChatActions sends an actions like "typing, looking"
func (b *Bot) SendChatActions(chatID string, actions ...ChatAction) error {
return b.client.SendChatActions(chatID, actions...)
}
// GetChatAdmins returns chat admins list with fields:
// userID, creator flag
func (b *Bot) GetChatAdmins(chatID string) ([]ChatMember, error) {
return b.client.GetChatAdmins(chatID)
}
// GetChatMem returns chat members list with fields:
// userID, creator flag, admin flag
func (b *Bot) GetChatMembers(chatID string) ([]ChatMember, error) {
return b.client.GetChatMembers(chatID)
}
// GetChatBlockedUsers returns chat blocked users list:
// userID
func (b *Bot) GetChatBlockedUsers(chatID string) ([]User, error) {
return b.client.GetChatBlockedUsers(chatID)
}
// GetChatPendingUsers returns chat join pending users list:
// userID
func (b *Bot) GetChatPendingUsers(chatID string) ([]User, error) {
return b.client.GetChatPendingUsers(chatID)
}
// BlockChatUser blocks user and removes him from chat.
// If deleteLastMessages is true, the messages written recently will be deleted
func (b *Bot) BlockChatUser(chatID, userID string, deleteLastMessages bool) error {
return b.client.BlockChatUser(chatID, userID, deleteLastMessages)
}
// UnblockChatUser unblocks user in chat
func (b *Bot) UnblockChatUser(chatID, userID string) error {
return b.client.UnblockChatUser(chatID, userID)
}
// ResolveChatJoinRequests sends a decision to accept/decline user join to chat
func (b *Bot) ResolveChatJoinRequests(chatID, userID string, accept, everyone bool) error {
return b.client.ResolveChatPending(chatID, userID, accept, everyone)
}
// SetChatTitle changes chat title
func (b *Bot) SetChatTitle(chatID, title string) error {
return b.client.SetChatTitle(chatID, title)
}
// SetChatAbout changes chat about
func (b *Bot) SetChatAbout(chatID, about string) error {
return b.client.SetChatAbout(chatID, about)
}
// SetChatRules changes chat rules
func (b *Bot) SetChatRules(chatID, rules string) error {
return b.client.SetChatRules(chatID, rules)
}
// GetFileInfo returns information about file:
// id, type, size, filename, url
func (b *Bot) GetFileInfo(fileID string) (*File, error) {
return b.client.GetFileInfo(fileID)
}
// NewMessage returns new message
func (b *Bot) NewMessage(chatID string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
}
}
// NewTextMessage returns new text message
func (b *Bot) NewTextMessage(chatID, text string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
Text: text,
ContentType: Text,
}
}
// NewTextMessageWithRequestID returns new text message with client requestID
func (b *Bot) NewTextMessageWithRequestID(chatID, text, requestID string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
Text: text,
ContentType: Text,
RequestID: requestID,
}
}
// NewInlineKeyboardMessage returns new text message with inline keyboard
func (b *Bot) NewInlineKeyboardMessage(chatID, text string, keyboard Keyboard) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
Text: text,
ContentType: Text,
InlineKeyboard: &keyboard,
}
}
// NewDeeplinkMessage returns new text message with inline keyboard and deeplink
func (b *Bot) NewDeeplinkMessage(chatID, text string, keyboard Keyboard, deeplink string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
Text: text,
ContentType: Deeplink,
InlineKeyboard: &keyboard,
Deeplink: deeplink,
}
}
// NewFileMessage returns new file message
func (b *Bot) NewFileMessage(chatID string, file *MessageFile) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
File: file,
ContentType: OtherFile,
}
}
// NewFileMessageByFileID returns new message with previously uploaded file id
func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
FileID: fileID,
ContentType: OtherFile,
}
}
// NewVoiceMessage returns new voice message
func (b *Bot) NewVoiceMessage(chatID string, file *MessageFile) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
File: file,
ContentType: Voice,
}
}
// NewVoiceMessageByFileID returns new message with previously uploaded voice file id
func (b *Bot) NewVoiceMessageByFileID(chatID, fileID string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
FileID: fileID,
ContentType: Voice,
}
}
// NewMessageFromPart returns new message based on part message
func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
return &Message{
client: b.client,
ID: message.MsgID,
Chat: Chat{ID: message.From.User.ID, Title: message.From.FirstName},
Text: message.Text,
Timestamp: message.Timestamp,
}
}
// NewButtonResponse returns new ButtonResponse
func (b *Bot) NewButtonResponse(queryID, url, text string, showAlert bool) *ButtonResponse {
return &ButtonResponse{
client: b.client,
QueryID: queryID,
Text: text,
URL: url,
ShowAlert: showAlert,
}
}
func (b *Bot) NewChat(id string) *Chat {
return &Chat{
client: b.client,
ID: id,
}
}
// SendMessage sends a message, passed as an argument.
// This method fills the argument with ID of sent message and returns an error if any.
func (b *Bot) SendMessage(message *Message) error {
message.client = b.client
return message.Send()
}
// EditMessage edit a message passed as an argument.
func (b *Bot) EditMessage(message *Message) error {
return b.client.EditMessage(message)
}
// GetUpdatesChannel returns a channel, which will be filled with events.
// You can pass cancellable context there and stop receiving events.
// The channel will be closed after context cancellation.
func (b *Bot) GetUpdatesChannel(ctx context.Context) <-chan Event {
updates := make(chan Event)
go b.updater.RunUpdatesCheck(ctx, updates)
return updates
}
// NewBot returns new bot object.
// All communications with bot API must go through Bot struct.
// In general you don't need to configure this bot, therefore all options are optional arguments.
func NewBot(token string, opts ...BotOption) (*Bot, error) {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
apiURL := defaultAPIURL
debug := defaultDebug
client := *http.DefaultClient
lastEventID := 0
for _, option := range opts {
switch option.Type() {
case "api_url":
apiURL = option.Value().(string)
case "debug":
debug = option.Value().(bool)
case "http_client":
client = option.Value().(http.Client)
case "last_event_id":
lastEventID = option.Value().(int)
}
}
if debug {
logger.SetLevel(logrus.DebugLevel)
}
tgClient := NewCustomClient(&client, apiURL, token, logger)
updater := NewUpdater(tgClient, 0, logger)
updater.lastEventID = lastEventID
info, err := tgClient.GetInfo()
if err != nil {
return nil, fmt.Errorf("cannot get info about bot: %s", err)
}
return &Bot{
client: tgClient,
updater: updater,
logger: logger,
Info: info,
}, nil
}