Search code examples
pythontelegramtelegram-botpython-telegram-bot

Why telegram bot not sending the message?


Why bot not sending message? My code:

import telegram
import requests

bot_token = 'token***'

my_bot = telegram.Bot(token=bot_token)

def get_chat_id(bot_token):
    request = requests.post(f'https://api.telegram.org/bot{bot_token}/getUpdates')
    result = request.json()['result'][0]['message']['chat']['id']
    return result

chat_id = get_chat_id(bot_token)

msg = 'My PC'
my_bot.sendMessage(chat_id=chat_id, text=msg)

Python 3.11.5, Telegram 20.7


Solution

  • In my telegram bot I use the next config

        #Token of the telegram bot
    
        TOKEN = "yours"
    
        #Id for the chat
        chat_id  = "the_chat_id"
    
        msj_err = "Bot is alive"
     
     
     
     requests.post('https://api.telegram.org/bot'+TOKEN+'/sendMessage?chat_id='+chat_id+'&text='+msj_err)
    

    The chat_id is something i needed to set manually, because sometimes it does't take it. Now this bot has been working for 3 months.

    The problem I think is that the chat id is not correct. Try to extract the chat id and compare of what is getting in your request.