【问题标题】:Telegram group chat id not working for bot电报群聊 ID 不适用于机器人
【发布时间】:2022-01-05 20:26:28
【问题描述】:

我正在尝试使用pyTelegrambotApi 构建一个电报机器人。但是我在从机器人发送消息时遇到错误。代码如下。

import telebot

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
 
  bot.send_message(ID,"Thanks for adding me!!")

bot.infinity_polling()

这里的 ID 是我使用 similar question 中提到的 rawdatabot 获得的 ID。但这里的问题是它只响应我的命令。当其他成员发出相同的命令时,该机器人不适用于其他成员。有人可以在这里指出错误吗?

【问题讨论】:

    标签: python python-3.x telegram telegram-bot py-telegram-bot-api


    【解决方案1】:

    使用 message.chat.id 代替 ID,它会自动定义聊天 ID。这意味着机器人会回复其所在位置的请求。

    import telebot
    from telebot.types import Message
    
    bot = telebot.TeleBot('TOKEN')
    
    @bot.message_handler(commands=['start', 'help'])
    def send_welcome(message: Message):
      bot.send_message(message.chat.id, "Thanks for adding me!!")
    
    bot.infinity_polling()
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2022-12-11
      相关资源
      最近更新 更多