-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
424 lines (365 loc) · 10.6 KB
/
main.lua
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
--
-- Examples of how some methods and commands work
--
local fiber = require('fiber')
local log = require('log')
local bot = require('core.bot')
local parse_mode = require('core.enums.parse_mode')
-- Optional
-- local p = require('pimp')
-- _G.p = p
-- Setup
bot:cfg {
token = os.getenv('BOT_TOKEN'), -- Your bot Token
parse_mode = parse_mode.HTML, -- Mode for parsing entities
}
-- Commands:
-- /start
-- /set_commands
-- /timer
-- /args_test arg1 arg2
-- /send_photo
-- /send_reply_buttons_1
-- /send_reply_buttons_2
-- /remove_reply_keyboard
-- /send_inline_buttons_1
-- /send_inline_buttons_2
-- /send_media_group
-- Load all libs/extensions
local dec = require('core.extensions.html_formatter')
local chat_member_status = require('core.enums.chat_member_status')
local bot_command_scope = require('core.enums.bot_command_scope')
-- Inputs
local InputFile = require('core.types.InputFile')
local InputMedia = require('core.types.InputMedia')
local InputMediaPhoto = require('core.types.InputMediaPhoto')
-- Inline buttons
local InlineKeyboardMarkup = require('core.types.InlineKeyboardMarkup')
local InlineKeyboardButton = require('core.types.InlineKeyboardButton')
-- Reply buttons
local ReplyKeyboardMarkup = require('core.types.ReplyKeyboardMarkup')
local KeyboardButton = require('core.types.KeyboardButton')
local ReplyKeyboardRemove = require('core.types.ReplyKeyboardRemove')
-- Bot commands
local BotCommand = require('core.types.BotCommand')
local BotCommandScope = require('core.types.BotCommandScope')
-- Command handler
local function processCommand(data, opts)
-- Callback commanmd
if opts and opts.is_callback then
local command = bot.CallbackCommand(data)
if command then
command(data)
-- Answer callback
bot:call('answerCallbackQuery', {
callback_query_id = data:getQueryId()
})
end
return
end
-- Bot command or other
local command = bot.Command(data)
if command then
command(data)
end
end
-- Command /start
bot.commands['/start'] = function(message)
-- Get bot information
local data, err = bot:call('getMe')
if err then
log.error(err)
return
end
-- Creating textual information about the bot
--
local infoText = dec.bold('Bot Info:\n')
for paramName, value in pairs(data.result) do
paramName = dec.bold(paramName)
value = dec.monospaced(value)
infoText = infoText .. paramName .. ': ' .. value .. '\n'
end
-- Send text message
bot:call('sendMessage', {
text = infoText,
chat_id = message:getChatId(),
})
end
-- Set bot commands
bot.commands['/set_commands'] = function(message)
local _, err = bot:call('setMyCommands', {
commands = {
BotCommand({ '/start', 'Start the bot' }),
BotCommand({ '/timer', 'Timer test' }),
BotCommand({ '/test' })
},
scope = BotCommandScope(bot_command_scope.DEFAULT)
})
if err then
log.error(err)
return
end
bot:call('sendMessage', {
text = 'Commands installed',
chat_id = message:getChatId(),
})
end
-- Delete bot commands
bot.commands['/delete_commands'] = function(message)
local _, err = bot:call('deleteMyCommands', {
scope = BotCommandScope(bot_command_scope.DEFAULT)
})
if err then
log.error(err)
return
end
bot:call('sendMessage', {
text = 'Commands removed',
chat_id = message:getChatId(),
})
end
-- Test timer
bot.commands['/timer'] = function(message)
for i = 1, 3 do
bot:call('sendMessage', {
text = 'timeout: ' .. i,
chat_id = message:getChatId(),
})
fiber.sleep(1)
end
end
-- Command /args_test
-- Paring argumnts
bot.commands['/args_test'] = function(message)
-- arguments[1] -- Command name
-- arguments[2] -- Argument 1
-- arguments[3] -- Argument 2
local arguments = message:getArguments({ count = 3 })
local arg1 = arguments[1] or 'nil'
local arg2 = arguments[2] or 'nil'
local arg3 = arguments[3] or 'nil'
local text_fmt = 'Command: %s\n arg2: %s\n arg3: %s\n'
local text = text_fmt:format(arg1, arg2, arg3)
bot:call('sendMessage', {
text = text,
chat_id = message:getChatId(),
})
end
-- Close all callback command
bot.commands['cb_close'] = function(callback)
-- Delete message
bot:call('deleteMessage', {
chat_id = callback:getChatId(),
message_id = callback:getMessageId(),
})
end
bot.commands['cb_button'] = function(callback)
-- arguments[1] - Command name
-- arguments[2] - Button ID
local arguments = callback:getArguments({ count = 2 })
local buttonId = tonumber(arguments[2])
bot:call('sendMessage', {
text = 'Press Button: '..buttonId,
chat_id = callback:getChatId(),
})
end
-- Command /send_photo
-- Method sendPhoto
bot.commands['/send_photo'] = function(message)
local data, err = bot:call('sendPhoto', {
photo = InputFile('image.jpg'),
caption = 'Omg! It\'s photo from disk!',
chat_id = message:getChatId(),
}, { multipart_post = true })
if err then
log.error(err)
return
end
local fileIdArr = data.result.photo
local file_id = fileIdArr[#fileIdArr].file_id
local width = fileIdArr[#fileIdArr].width
local height = fileIdArr[#fileIdArr].height
log.info('File ID: %s Width: %d Height: %d', file_id, width, height)
end
-- Command /send_reply_buttons_1
-- Method sendMessage with reply_markup
bot.commands['/send_reply_buttons_1'] = function(message)
bot:call('sendMessage', {
text = 'Reply keyboard buttons test 1',
chat_id = message:getChatId(),
reply_markup = ReplyKeyboardMarkup({
keyboard = {
{ KeyboardButton(nil, { text = 'Apple' }), KeyboardButton(nil, { text = 'Banana' }) },
{ KeyboardButton(nil, { text = 'Orange' }) },
},
one_time_keyboard = true,
})
})
end
-- Command /send_reply_buttons_2
-- Method sendMessage with reply_markup and keyboard buttons
bot.commands['/send_reply_buttons_2'] = function(message)
-- Another option for building buttons
local keyboard = ReplyKeyboardMarkup({ one_time_keyboard = false })
KeyboardButton(keyboard, { text = 'Apple' })
KeyboardButton(keyboard, { text = 'Banana' })
KeyboardButton(keyboard, { text = 'Orange', row = 2 })
bot:call('sendMessage', {
text = 'Reply keyboard buttons test 2',
chat_id = message:getChatId(),
reply_markup = keyboard,
})
end
-- Command remove_reply_keyboard
-- Work only in private chat type
-- see https://core.telegram.org/bots/api#replykeyboardremove
bot.commands['/remove_reply_keyboard'] = function(message)
bot:call('sendMessage', {
text = 'Removed reply keyboard',
chat_id = message:getChatId(),
reply_markup = ReplyKeyboardRemove({ selective = true }),
})
end
-- Command send_inline_buttons_1
-- Method sendMessage with reply_markup
bot.commands['/send_inline_buttons_1'] = function(message)
bot:call('sendMessage', {
text = 'Inline keyboard buttons test 1',
chat_id = message:getChatId(),
reply_markup = InlineKeyboardMarkup({
inline_keyboard = {
{
InlineKeyboardButton(nil, { text = 'Button 1', callback_data = 'cb_button 1' }),
InlineKeyboardButton(nil, { text = 'Button 2', callback_data = 'cb_button 2' })
},
{ InlineKeyboardButton(nil, { text = 'Close', callback_data = 'cb_close' }) }
}
})
})
end
-- Command send_inline_buttons_2
-- Method sendMessage with reply_markup and inline buttons
bot.commands['/send_inline_buttons_2'] = function(message)
-- Another option for building buttons
local keyboard = InlineKeyboardMarkup()
InlineKeyboardButton(keyboard, { text = 'Button 1', callback_data = 'cb_button 1' })
InlineKeyboardButton(keyboard, { text = 'Button 2', callback_data = 'cb_button 2', row = 1 })
InlineKeyboardButton(keyboard, { text = 'Close', callback_data = 'cb_close' })
bot:call('sendMessage', {
text = 'Inline keyboard buttons test 2',
chat_id = message:getChatId(),
reply_markup = keyboard,
})
end
-- Command /send_media_group
-- Method sendMediaGroup
bot.commands['/send_media_group'] = function(message)
local data = InputMedia({
-- For file id
-- InputMediaPhoto({
-- media = 'AgACAgIAAxkDAAIJ52RX2qzt6oCMY5P9Ge9uVuZgTDH_AAL-yTEb9gABuEp-yXhmUY3rfAEAAwIAA3MAAy8E',
-- caption = 'Photo with file_id',
-- }),
InputMediaPhoto({
media = 'attach://'..'image.jpg',
caption = 'Photo with disk',
}),
InputMediaPhoto({
media = 'https://raw.githubusercontent.com/uriid1/scrfmp/main/perfect-arkanoid/arkanoid.png',
caption = 'Photo with url',
}),
})
data.chat_id = message:getChatId()
bot:call('sendMediaGroup', data, {
multipart_post = true
})
end
-- Event of getting entities
bot.events.onGetEntities = function(message)
local entities = message:getEntities()
if entities[1] and entities[1].type == 'bot_command' then
-- Call bot command
processCommand(message)
end
end
-- Event of getting callbacks
bot.events.onCallbackQuery = function(callback)
-- Call bot callback
processCommand(callback, { is_callback = true })
end
-- Event of getting any message
bot.events.onGetMessageText = function(message)
local text = message:getText()
local result
if text == 'Apple' then
result = 'You got +1 apple!'
elseif text == 'Banana' then
result = 'You got +1 banana!'
elseif text == 'Orange' then
result = 'You got +1 orange!'
end
if result then
bot:call('sendMessage', {
text = result,
chat_id = message:getChatId(),
})
return
end
-- Send message
bot:call('sendMessage', {
text = dec.bold(message:getText()),
chat_id = message:getChatId(),
})
end
-- Event of my chat member
bot.events.onMyChatMember = function(myChatMember)
local status = myChatMember:getNewChatMemberStatus()
if status == chat_member_status.MEMBER then
local ufrom = myChatMember:getUserFrom()
bot:call('sendMessage', {
text = dec.user(ufrom) .. ' - Added me to this chat',
chat_id = myChatMember:getChatId(),
})
end
end
-- Handle errors
bot.events.onRequestErr = function(err, _, _)
log.error(err)
end
-- Run bot
-- Use long polling to develop
-- and webhook for release
--
-- Enable long polling
bot:startLongPolling()
--[[ Setup Web Hook
bot:startWebHook({
-- Server setup
host = '0.0.0.0',
port = 5505,
-- Bot setup
url = 'https://mysite.ru/myBotUrl',
certificate = '/etc/cert/myBotName/public.pem',
drop_pending_updates = true,
allowed_updates = {
"message",
"my_chat_member",
"callback_query"
},
-- Custom routes (array of routes)
routes = {
-- Example
{
path = '/bot/v1/payments',
method = 'GET',
callback = function(req)
return {
status = 200,
headers = { ['content-type'] = 'text/plain' },
body = "Payments: no data"
}
end
}
},
})
]]