How to get the chat id, bot id, message id, channel id, group id using python-telegram-bot API?

Viewed 1279

I have created a bot in telegram and I wish to control it using python.

I am using python-telegram-bot API. What is the easiest way to figure out various ids using the python: bot id, chat id, message id, group id, channel id? Is there any readymade code available for this?

1 Answers

I suppose you have something like that

updater = Updater(TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text, echo))

with dispatcher.add_handler you associate a specific function to run when the bot receive a message (in this case only text messages due to Filters.text)

The function will be something like that:

def echo(update, context):
    ...
    ...
    chat_id = update.message.chat_id
    message_id = update.message.message_id
    text = update.message.text
    ...
    ...

All the informations you need are inside the Update object, received by the Dispatcher, i extracted the more usefull, but there are a lot others. Check the documentation for more details.

Related