【问题标题】:Sending messages to channels - bot.send_message no longer works向频道发送消息 - bot.send_message 不再有效
【发布时间】:2019-10-17 18:04:07
【问题描述】:

不久前,我曾经向这样的频道发送消息:

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)

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

【问题讨论】:

    标签: telegram telegram-bot python-telegram-bot


    【解决方案1】:

    From documentation、bot 在context 中可用。

    那么 CallbackContext 中存储了哪些信息?参数 带有星号的标记只会在特定更新时设置。

    • 机器人
    • 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)
    

    【讨论】:

      【解决方案2】:

      如上所述,bot 可以在 context 中使用,因此也可以使用函数

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

      可以改写为

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-03
        • 2021-01-10
        • 2021-03-24
        • 2020-04-07
        • 1970-01-01
        • 2020-07-02
        • 2021-08-21
        相关资源
        最近更新 更多