Search code examples
pythonpython-telegram-bot

How can I fetch Telegram chat_id?


Hi there I have a problem with telebot.

I'm trying to do it as explained in official documentation but they use a variable called chat_id (I know what it is) and I'm willing to know how to retrieve it and use it.

Bonus: How can I let the keys on keyboard send a message from user to activate functions. Explaining better: The user presses a button on keyboard and sends a command (such as /it_lang) and then use a command handling function to do something.

docs: https://github.com/eternnoir/pyTelegramBotAPI

    import telebot
    from telebot import types
    from config import TOKEN, bot_is_active
    bot = telebot.TeleBot(TOKEN)
    if bot_is_active :
    u/bot.message_handler(commands=['start'])
    def choose_lang(message):
            markup = types.ReplyKeyboardMarkup()
            it_btn = types.KeyboardButton('IT')
            en_btn = types.KeyboardButton('EN')
            markup.row(it_btn)
            markup.row(en_btn)
            bot.send_message(chat_id, "Please choose a language:", reply_markup=markup)

        bot.polling()
    else:
    u/bot.message_handler(commands=['start'])
    def choose_lang(message):
            bot.reply_to(message, "I'm sorry the bot is no available,try later")
        bot.polling()

Solution

  • If i understand you right:

    cid = message.chat.id
    

    All info what you can get:

    @bot.message_handler(commands=['start'])
    def start(message):
        print(message)
    

    UPD:

    bot = telebot.TeleBot('Token')
    keyboard1 = telebot.types.ReplyKeyboardMarkup()
    keyboard1.row('IT', 'ENG')
    
    @bot.message_handler(commands=['start'])
    def start_message(message):
        bot.send_message(message.chat.id, 'Hello, i reveive /start', reply_markup=keyboard1)
    
    @bot.message_handler(content_types=['text'])
    def send_text(message):
        if message.text.lower() == 'it':
            bot.send_message(message.chat.id, 'Your language is IT')
            <do something>
        elif message.text.lower() == 'eng':
            bot.send_message(message.chat.id, 'Your language is ENG')
            <do something>