telegram-bot
telegram-bot copied to clipboard
Lua binding improvements
Add new bindings for lua scripting:
-
block_user
-
unblock_user
-
channel_join
-
channel_leave
-
channel_list
-
channel_set_about
-
channel_set_admin
-
channel_set_photo
-
channel_set_username
-
export_channel_link
-
get_message
-
import_channel_link
-
media_caption
-
rename_channel
-
resolve_username
Code examples of:
-
block_user
andunblock_user
to block and unbloking a user.
do
local function run(msg, matches)
if matches[2]:match('^%d.+$') then
if matches[1] == 'block' then
block_user('user#id'..matches[2], ok_cb, true)
return 'User '..matches[2]..' blocked'
elseif matches[1] == 'unblock' then
unblock_user('user#id'..matches[2], ok_cb, true)
return 'User '..matches[2]..' unblocked'
end
end
end
--------------------------------------------------------------------------------
return {
description = 'Block a user.',
usage = {
' !block [user_id] : Block user by their ID.'
},
patterns = {
'^!(block) (%d.+)$',
'^!(unblock) (%d.+)$'
},
run = run
}
end
-
rename_channel
to rename channels/supergroups title.
do
local function run(msg, matches)
if matches[1] == 'rename' then
rename_channel(get_receiver(msg), matches[2], ok_cb, true)
end
end
--------------------------------------------------------------------------------
return {
description = 'Set channel/supergroup title.',
usage = {
' !channel rename [title] : Set channel/supergroup title to [title].'
},
patterns = {
'^!channel (rename) (.*)$'
},
run = run
}
end
-
channel_set_username
to set channels/supergroups username.
This username will also become channels/supergroups URL.
do
local function run(msg, matches)
if matches[1] == 'setname' then
channel_set_username(get_receiver(msg), matches[2], ok_cb, true)
end
end
--------------------------------------------------------------------------------
return {
description = 'Set channel/supergroup username.',
usage = {
' !channel setname [username] : Set channel/supergroup username to [username].'
},
patterns = {
'^!channel (setname) (%w.+)$'
},
run = run
}
end
-
export_channel_link
to generate channels/supergroups invite link.
do
local function exp ort_channel_link_cb(extra, success, result)
send_msg(get_receiver(extra), result, ok_cb, true)
end
--------------------------------------------------------------------------------
local function run(msg, matches)
if matches[1] == 'set' then
export_channel_link(get_receiver(msg), export_channel_link_cb, msg)
end
end
--------------------------------------------------------------------------------
return {
description = 'Generate channels/supergroups invite link.',
usage = {
'!link set : Generate channels/supergroups invite link.'
},
patterns = {
'^!link (set)$'
},
run = run
}
end
-
get_message
to get userid
by reply:
do
local function action_by_reply(extra, success, result)
if result.from.username then
user_name = '@'..result.from.username
else
user_name = ''
end
local text = 'Name: '..(result.from.first_name or '')..' '..(result.from.last_name or '')..'\n'
..'First name: '..(result.from.first_name or '')..'\n'
..'Last name: '..(result.from.last_name or '')..'\n'
..'User name: '..user_name..'\n'
..'ID: '..result.from.peer_id
send_large_msg(extra, text)
end
--------------------------------------------------------------------------------
local function run(msg)
if msg.text == '!id' and msg.reply_id then
get_message(msg.reply_id, action_by_reply, get_receiver(msg))
end
end
--------------------------------------------------------------------------------
return {
decription = 'Print user_id by_reply',
usage = 'Reply to a message then type: !id',
patterns = {
'^!id$'
},
run = run
}
end
-
import_channel_link
to Join a channel/supergoup via its invite link.
do
local function export_channel_link_cb(extra, success, result)
send_msg(get_receiver(extra), result, ok_cb, true)
end
--------------------------------------------------------------------------------
local function run(msg, matches)
if matches[1] == 'join' then
import_chat_link(matches[2], ok_cb, false)
end
end
--------------------------------------------------------------------------------
return {
description = 'Join a channel/supergroup via its invite link.',
usage = {
'!join [invite_link] : Join a channel/supergroup via its invite link.'
},
patterns = {
'^!(join) (.*)$'
},
run = run
}
end
-
media_caption
to warn sticker sender:
do
local function pre_process(msg)
if msg.media and msg.media.caption == 'sticker.webp' then
send_msg(get_receiver(msg), 'WARNING!\nDO NOT send sticker into this group!', ok_cb, true)
end
end
--------------------------------------------------------------------------------
return {
description = 'Warn sticker sender.',
patterns = {
'^!!tgservice (.+)$'
},
pre_process = pre_process
}
end
-
resolve_username
to invite user by its username:
do
local function resolve_username_cb(extra, success, result)
if success == 1 then
if extra.to.type == 'channel' then
channel_invite_user(get_receiver(extra), result.id, ok_cb, false)
else
chat_add_user(get_receiver(extra), result.id, ok_cb, false)
end
end
end
--------------------------------------------------------------------------------
local function run(msg, matches)
if matches[1]:match('^@.+$') then
resolve_username(matches[1]:gsub('@', ''), resolve_username_cb, msg)
end
end
--------------------------------------------------------------------------------
return {
description = 'Invite other user to the chat group.',
usage = {
' !invite @[user_name] : Invite by their @[user_name].'
},
patterns = {
'^!invite (@.*)$'
},
run = run
}
end