Reputation: 23
I received data about myself through API method /getMe
{'ok': True, 'result': {'id': 32484XXXX, 'first_name': 'Name', 'last_name': 'NameN', 'username': '@myname',
'type': 'private',
...
Then I try to send from bot to myself a message with telegram ID and with telegram username. With ID, everything works fine, but with username it doesn’t work. What am I doing wrong? How do I send messages with username?
import requests
telegram_bot_url = 'https://api.telegram.org/botXXXXXXXX:AAHfPgXkYrqPSakpsxB81UTsYwfmaq9dryY/'
params_name = {'chat_id': '@myname', 'text': 'test777'}
params_id = {'chat_id': 32484XXXX, 'text': 'test777'}
print("ID")
response = requests.post(telegram_bot_url + 'sendMessage', data=params_id)
print(response.json())
print("USERNAME")
response = requests.post(telegram_bot_url + 'sendMessage', data=params_name)
print(response.json())
and response
ID
{'ok': True, 'result': {'message_id': 15, 'from': {'id': 121079XXXX, 'is_bot': True, 'first_name': 'RRRR', 'username': 'RRRR'}, 'chat': {'id': 32484XXXX, 'first_name': 'Name', 'last_name': 'NameN', 'username': 'myname', 'type': 'private'}, 'date': 1592754152, 'text': 'test777'}}
USERNAME
{'ok': False, 'error_code': 400, 'description': 'Bad Request: chat not found'}
Upvotes: 2
Views: 8212
Reputation: 486
If you look closely on the BotAPI docs,
Unique identifier for the target chat or username of the target channel (in the format @channelusername)
Unique identifier -> which is the chat id (can be used for any type of chats)
Username -> @username can only be used for public channels/groups (no private chats/channels/groups)
Upvotes: 2