【问题标题】:Telegram bot developed using telebot library in python won't work in telegram channels?在 python 中使用 Telebot 库开发的 Telegram bot 在电报频道中不起作用?
【发布时间】:2022-01-03 12:13:10
【问题描述】:

我使用 telebot 库构建了一个简单的电报机器人,它可以成功运行!然后我将机器人添加到电报频道它不起作用!另外,我将同一个机器人添加到电报组,然后它就可以工作了!我有点困惑为什么电报机器人不能在我的电报频道中工作?谁能帮我解决这个问题?

这是我的代码。

import telebot

from constatnts import API_KEY

bot = telebot.TeleBot(API_KEY, parse_mode=None)


@bot.message_handler(commands=['help', 'hello'])
def send_help_message(msg):
    bot.reply_to(msg, "Hello this is test bot for ml5")
    print(msg)


@bot.message_handler(func=lambda msg: msg.content_type == 'text' )
def send_message(msg):
    bot.reply_to(msg, "Gotcha")

bot.polling()

【问题讨论】:

    标签: python telegram telegram-bot py-telegram-bot-api


    【解决方案1】:

    IIRC、消息和频道帖子有不同的处理程序:

    @bot.channel_post_handler(commands=['help', 'hello'])
    

    还要确保机器人是频道的管理员。

    【讨论】:

      【解决方案2】:
      import telebot
      
      from constatnts import API_KEY
      
      bot = telebot.TeleBot(API_KEY, parse_mode=None)
      
      
      @bot.channel_post_handler(commands=['help', 'hello'])
      def send_help_message(msg):
          bot.reply_to(msg, "Hello this is test bot for ml5")
          print(msg)
      
      
      @bot.message_handler(func=lambda msg: msg.content_type == 'text' )
      def send_message(msg):
          bot.reply_to(msg, "Gotcha")
      
      bot.polling()
      

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 2021-08-24
        • 2019-08-07
        • 2020-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        相关资源
        最近更新 更多