Search code examples
pythonpython-3.xtelegramtelegram-botpy-telegram-bot-api

Telegram group chat id not working for bot


I am trying to build a telegram bot using pyTelegrambotApi. But I am facing error in sending messages from bot. The code is given below.

import telebot

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

bot.infinity_polling()

here ID is the id I got from using rawdatabot as mentioned in similar question. But the problem here is that it only responds to my command. The bot is not working for other members when others give the same command. Can someone point the error here?


Solution

  • Instead of ID, use the message.chat.id it auto defines the chat id. This means that the bot reply to requests for where it is located.

    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()