向频道发送消息 - bot.send_message 不再有效

Sending messages to channels - bot.send_message no longer works

前段时间,我曾经这样向频道发送消息:

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

我会回复用户:

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

现在,CommandHandlers 的参数似乎已从 (bot, update) 更改为 (update, context)。因此,我仍然可以使用 update 参数回复用户,如下所示:

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

但我无法再向频道发送消息。我该怎么做?

From documentationbot 可用于 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
  • ...

所以函数,

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

可以这样改写:

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

如上所述 botcontext 中可用,因此函数

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

可以改写为

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