【发布时间】:2021-08-24 11:07:09
【问题描述】:
我正在尝试设置一个机器人:
- 从 TG 组接收
/search_msgs userkey命令中的关键字 - 在 DB 中搜索
userkey并将相应的文本发回
我遇到了两个错误
- None类型对象没有属性args,在
callback_search_msgs(context),见代码sn-p - AttributeError: 'int' object has no attribute 'job_queue',
in search_msgs(update, context),见代码 sn-p。
Telegram 的官方文档对我来说太难阅读和理解了。连一个地方都找不到 Updater、update、Commandhandler、context 都用例子来解释的地方。
如何修复此代码?
import telegram
from telegram.ext import Updater,CommandHandler, JobQueue
token = "Token"
bot = telegram.Bot(token=token)
# Search specific msgs on user request
def search_msgs(update, context):
context.job_queue.run_once(callback_search_msgs, context=update.message.chat_id)
def callback_search_msgs(context):
print('In TG, args', context.args)
chat_id = context.job.context
search_msgs(context, chat_id)
def main():
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("search_msgs",search_msgs, pass_job_queue=True,
pass_user_data=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
【问题讨论】:
标签: asynchronous telegram telegram-bot python-telegram-bot