Python电报机器人“ context.bot.send_message”

时间:2020-11-09 19:42:36

标签: python bots telegram

我正在使用“ python-telegram-bot”库,并且一直在通过互联网查看各种示例。

我注意到对话中有两种回复方式:

第一个: '''context.bot.send_message(chat_id = update.effective_chat.id,text = msg)'''

第二个: '''update.message.reply_text(text = msg)'''

实际上,它们都起作用。 我应该更喜欢另一个吗? 来说明我的问题。在回显功能第3和第4行中给出相同的结果

回复功能

def echo(update, context):
msg='Hi, nice to see you!'
context.bot.send_message(chat_id=update.effective_chat.id, text=msg)
update.message.reply_text(text=msg)
    

主要功能:

def main():
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
echo_handler=MessageHandler()
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dispatcher.add_handler(echo_handler)
updater.start_polling()
enter code here

1 个答案:

答案 0 :(得分:0)

我认为“context.bot.send_message”更适用于来自作业的消息,因为您可以粘贴上下文。 以此为例:https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py