python telegram bot "context.bot.send_message"
Asked Answered
R

2

8

I am using "python-telegram-bot" library and have been looking at various examples over the internet.

I have noticed there are two ways to reply in a conversation:

the first: ''' context.bot.send_message(chat_id=update.effective_chat.id, text=msg)'''

the second: '''update.message.reply_text(text=msg)'''

practically they both work. Should I prefer one on the other? to illustarte my question. in echo function line 3 and 4 give the same result

reply function

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)
    

main function::

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
Resourceful answered 9/11, 2020 at 19:42 Comment(0)
H
2

I think the "context.bot.send_message" is more used to messages from jobs, because you can paste the context. Take this as example: https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py

Halftimbered answered 7/2, 2021 at 17:56 Comment(0)
L
1

update.message.reply_text would be a reply to an existing message. It will quote it. context.bot.send_message would be a new message. It will not act the same as a direct reply.

Lemniscate answered 13/9, 2021 at 3:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.