skip to Main Content

A while back, I used to send messages to channels like this:

def broadcast(bot, update):
    bot.send_message(channel_id, text)

And I would reply to the user with:

def reply(bot, update):
    update.message.reply_text(text)

Now, it seems that the arguments for CommandHandlers have changed from (bot, update) to (update, context). As a result, I can still reply to the user with the update argument, something like this:

def reply(update, context):
    update.message.reply_text(text)

But I can no longer send messages to a channel. How can I do this?

2

Answers


  1. Chosen as BEST ANSWER

    From documentation, bot is available in context.

    So what information is stored on a CallbackContext? The parameters marked with a star will only be set on specific updates.

    • bot
    • job_queue
    • update_queue
    • ...

    So the function,

    def broadcast(bot, update):
        bot.send_message(channel_id, text)
    

    can be rewritten like this:

    def broadcast(update, context):
        context.bot.send_message(channel_id, text)
    

  2. As mentioned above bot is available in context so alternatively the function

    def broadcast(bot, update):
        bot.send_message(channel_id, text)
    

    can be rewritten as

    def broadcast(update, context):
        bot = context.bot
        bot.send_message(channel_id, text)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search