在telegram-cli中发送多条响应消息(使用Lua脚本)
2017-2-7 5:8:24
收藏:0
阅读:89
评论:2
我想使用以下修改后的Lua脚本,在Telegram-CLI中发送自动回复消息:
function ok_cb(extra, success, result)
end
function wait(seconds)
local start = os.time()
repeat until os.time() > start + seconds
end
function on_msg_receive (msg)
if msg.out then
return
end
if (string.find(msg.text, '你好')) then
wait(1)
send_msg (msg.from.print_name, '你好啊', ok_cb, false)
else
--do nothing
end
end
当我运行上面的脚本时,如果我收到一个消息“你好”,该脚本将等待1秒钟,然后发送“你好啊”回复消息。
当我修改脚本以添加另一个回复消息时,结果与我预期的不同。
function ok_cb(extra, success, result)
end
function wait(seconds)
local start = os.time()
repeat until os.time() > start + seconds
end
function on_msg_receive (msg)
if msg.out then
return
end
if (string.find(msg.text, '你好')) then
wait(1)
send_msg (msg.from.print_name, '你好啊', ok_cb, false)
wait(3) --new command
send_msg (msg.from.print_name, '世界!', ok_cb, false) --new command
else
--do nothing
end
end
我期望修改后的脚本在收到“你好”消息时,将等待1秒钟,然后发送“你好啊”消息,再等待3秒钟,最后发送“世界!”消息。
现实发生的是,该脚本将等待3秒钟,然后同时发送“你好啊”和“世界!”消息。
有没有人有关于这个问题的线索?提前谢谢。
点赞
用户15246322
问题在于 send_msg 命令被包含在 on_msg_receive 函数中。要解决这个问题,使用布尔变量和函数 cron。在 cron 函数中增加第二个 send_msg,并使用布尔变量。
function on_msg_receive(msg)
.
.
blnSendMsgdMsg = true
.
.
function cron()
.
.
if blnSendMsgdMsg then
send_msg .,
blnSendMsgdMsg = false
end
2021-02-20 02:39:03
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你只需编辑 on_msg_receive 函数:
function on_msg_receive(msg) if started == 0 then return end if msg.out then return end if msg.text then mark_read(msg.from.print_name, ok_cb, false) end -- Optional: Only allow messages from one number if msg.from.print_name ~= 'Prename_surname' then os.execute('*path_to_your_send_script*' ..msg.from.print_name.." 'Not allowed'") return end if (string.lower(msg.text) == 'uptime') then local handle = io.popen("sudo python *path_to_your_python* uptime") local res = handle:read("*a") handle:close() os.execute("*path_to_your_send_script* "..msg.from.print_name.." '"..res.."' ") return end如果 Lua 脚本出现以下错误消息:
namespace.lua:149: Typelib file for namespace 'Notify' (any version) not found那么您需要注释或删除“Notification code{{{"中的所有内容。
您可以扩展上面的命令,只需编辑 Lua 文件和 Python 文件即可(现在当用户发送内容为“Hi”时,轻松回复“嗨,怎么了?”):
if (string.lower(msg.text) == 'hi there') then os.execute('*path_to_your_send_script*' ..msg.from.print_name.." 'Hey, what's up?'") return end来源:源
此外,请确保通过 add_contact 添加联系人以接收来自其的消息。
您可以通过输入以下命令在 Lua 脚本中启动 telegram-cli:
screen -dmS TelegramCLI ~/tg/bin/telegram-cli -s ~/tg/test.lua在此之前,请安装 screen 包。