Amir Shabani
Amir Shabani

Reputation: 4227

Sending messages to channels - bot.send_message no longer works

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?

Upvotes: 0

Views: 2155

Answers (2)

Daniil
Daniil

Reputation: 11

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)

Upvotes: 0

Amir Shabani
Amir Shabani

Reputation: 4227

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)

Upvotes: 2

Related Questions