37

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

PythoNic
  • 303
  • 5
  • 13
Bijan
  • 7,737
  • 18
  • 89
  • 149

11 Answers11

49

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

Muhammed Rahif
  • 434
  • 1
  • 5
  • 17
Bijan
  • 7,737
  • 18
  • 89
  • 149
24

I was using prefix @ before the value of chat_id as suggested everywhere. I removed it and it started working. Note: if your chat id is 12345678 then you need to prefix it with -100 such that it is -10012345678. Example Postman call:

/sendMessage?chat_id=-10012345678&text=Let's get together
NKM
  • 602
  • 1
  • 6
  • 18
  • 2
    This should be updated in Telegram docs. I wasted hours trying out various things before I found the solution. – NKM Jan 30 '21 at 06:51
  • Adding -100 is totally wrong. Why you want to do that? He wants to write to a private chat. Group ids start with -100. You can't just add that, because you'll try to send a message to random, unknown group or channel. – PythoNic Feb 14 '21 at 12:14
  • That did the trick for me. Probably because the group in question is of type "Supergroup". – Kuepper Oct 13 '21 at 17:09
10

If your trying to send messages to a group, you must add a ‘-‘ in front of your chat ID. For example:

TELEGRAM_REG_CHAT_ID="1949275XX"

should be

TELEGRAM_REG_CHAT_ID="-1949275XX"
Dan Walters
  • 1,218
  • 1
  • 18
  • 30
1

There is a way to send notifications messages to telegram. It's a bit tricky but the tutorial is great!

http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification

I just sended a message of my apache state to a privat channel. Works also on public channel but it's not what i wantet. As you call a script (bash) you can prepare the parameters in any script language.

Hope that helps.

Martin S.
  • 256
  • 1
  • 10
1

For me it worked only with @ prefix before channel id

kashlo
  • 2,313
  • 1
  • 28
  • 39
1

If you want to use a bot message to the channel, you can refer step here

Steps:

  1. Create a Telegram public channel
  2. Create a Telegram BOT (for example x_bot) via BotFather
  3. Set the x_bot as an administrator in your channel

the chat_id is @x_bot, it's a part of https://t.me/x_bot that does not add your channel name.

Tan Nguyen
  • 947
  • 7
  • 8
1

I had some trouble with this after upgrading to a supergroup. The chat_id was updated and it was a bit harder to find this new id.

In the end I solved this with this by following this comment: https://stackoverflow.com/a/56078309/14213187

1

Telegram bots can't send messages to user, if that user hasn't started conversation with bot yet, or bot is not present in chat (if it's a group chat). This issue is not related to the library, this is simply Telegram restriction, so that bots can't spam users without their permission.

you need to first send a message to the bot before the bot can send messages to you.

0

If you use a username, it does not require any prefix. That means the following are incorrect:

https://t.me/vahid_esmaily_ie
t.me/vahid_esmaily_ie

And this is the correct case:

vahid_esmaily_ie
joanis
  • 10,635
  • 14
  • 30
  • 40
0

Make sure you use user id instead of username if you're sending message to user.

From the docs:

Parameter Type Required Description
chat_id Integer or String Yes Unique identifier for the target chat or username of the target channel (in the format @channelusername)
... ... ... ...

To get the chat id programmatically, see Getting updates for ways to get Update JSON object. For example, when a user sends a message, the Update object will have a Message object inside it which has a Chat object which has the chat id. Turns out the chat id is the same as the user id.

M Imam Pratama
  • 998
  • 11
  • 26
0

using the user_id in int() form worked for me

jedidiah
  • 11
  • 2