Telegram Bot "chat not found"

Bijan picture Bijan · Dec 16, 2016 · Viewed 30.7k times · Source

I have the following code in Python to send a message to myself from a bot.

import requests

token = '123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI'
method = 'sendMessage'
myuserid = 1949275XX
response = requests.post(
    url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
    data={'chat_id': myuserid, 'text': 'hello friend'}
).json()
print(response)

but this returns {'description': 'Bad Request: chat not found', 'error_code': 400, 'ok': False}

What am I doing wrong? I got myuserid by sending /getid to @myidbot and I got my token from @BotFather

Answer

Bijan picture Bijan · Dec 22, 2016

As @maak pointed out, you need to first send a message to the bot before the bot can send messages to you.