我正在用 python3 和 python-telegram-bot 编写一个 python 机器人,我希望它在发送消息之前显示 "is typing..."
I'm writing a python bot with python3 and python-telegram-bot, and i want it to display "is typing..." before sending a message
我希望机器人显示“键入...”操作,我找到了关于这个主题的其他讨论,但没有关于这个库的讨论,所以如果可能的话会很有帮助,在此先感谢。
我找到了这段代码
import logging
from telegram import *
from telegram.ext import *
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def message(update, context):
bot.sendChatAction(chat_id=update.message.chat_id, action = telegram.ChatAction.TYPING)
sleep(random() * 2 + 3.)
bot.sendMessage(chat_id=update.message.chat_id, text="Hi")
def error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
updater = Updater("token", use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text, message))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
并且错误显然是“错误“名称 'bot' 未定义”,但问题是,如何在不与更新程序冲突的情况下创建 bot 对象?帮助
您应该在回调函数中使用 context.bot
而不仅仅是 bot
。从 python-telegram-bot 的第 12 版开始,他们添加了 context-based 回调。 bot
对象现在位于 context
下。要查看 context
包含的其他对象,请检查 their documentation for callback context.
这是您的代码的固定版本:
import logging
from telegram import *
from telegram.ext import *
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
from time import sleep #You need to import sleep to be able to use that.
from random import random #You must also import random since you are using that.
def message(update, context):
context.bot.sendChatAction(chat_id=update.message.chat_id, action = telegram.ChatAction.TYPING)
sleep(random() * 2 + 3.)
context.bot.sendMessage(chat_id=update.message.chat_id, text="Hi")
def error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
updater = Updater("token", use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text, message))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
我希望机器人显示“键入...”操作,我找到了关于这个主题的其他讨论,但没有关于这个库的讨论,所以如果可能的话会很有帮助,在此先感谢。
我找到了这段代码
import logging
from telegram import *
from telegram.ext import *
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def message(update, context):
bot.sendChatAction(chat_id=update.message.chat_id, action = telegram.ChatAction.TYPING)
sleep(random() * 2 + 3.)
bot.sendMessage(chat_id=update.message.chat_id, text="Hi")
def error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
updater = Updater("token", use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text, message))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
并且错误显然是“错误“名称 'bot' 未定义”,但问题是,如何在不与更新程序冲突的情况下创建 bot 对象?帮助
您应该在回调函数中使用 context.bot
而不仅仅是 bot
。从 python-telegram-bot 的第 12 版开始,他们添加了 context-based 回调。 bot
对象现在位于 context
下。要查看 context
包含的其他对象,请检查 their documentation for callback context.
这是您的代码的固定版本:
import logging
from telegram import *
from telegram.ext import *
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
from time import sleep #You need to import sleep to be able to use that.
from random import random #You must also import random since you are using that.
def message(update, context):
context.bot.sendChatAction(chat_id=update.message.chat_id, action = telegram.ChatAction.TYPING)
sleep(random() * 2 + 3.)
context.bot.sendMessage(chat_id=update.message.chat_id, text="Hi")
def error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
updater = Updater("token", use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text, message))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()