【发布时间】:2020-10-16 16:19:41
【问题描述】:
这里我有一个代码,用于为电报制作一个简单的回声机器人。
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
from data.constants import API_TOKEN, LOGGING_FORMAT
logging.basicConfig(format=LOGGING_FORMAT, level=logging.DEBUG)
updater = Updater(token=API_TOKEN)
dispatcher = updater.dispatcher
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text='Hello, dude.')
def echo(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text=update.message.text)
start_handler = CommandHandler('start', start)
echo_handler = MessageHandler(Filters.text, echo)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(echo_handler)
if __name__ == '__main__':
updater.start_polling()
updater.idle()
我为此使用了 python-telegram-bot 库。
由于某种原因,这个机器人只能识别命令,而不是纯文本。我的意思是,echo 函数只与我呼应文本,从/ 开始。函数start 可以正常工作。
更有趣的是,我在另一个电报机器人库中遇到了同样的问题 - aiogram。
发疯后,我写信给python-telegram-bot 支持咨询以解决我的问题。
运行我的 sn-p 后,支持人员承认,这对他们来说效果很好,并建议我最后的希望之举:在新机器人上尝试。我从@BotFather 获得了新的 API 密钥,尝试了一下,但现在我在这里。这意味着我的代码仍然无法正常工作。
有一些参考资料,可以帮助我们解决它:
https://python-telegram-bot.readthedocs.io/en/latest/telegram.html https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-–-Your-first-Bot
但我想,问题出在我的工作环境中,尽管我尝试在不同的新设备上运行我的代码。
【问题讨论】:
标签: python python-3.6 telegram-bot python-telegram-bot