30

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

6
  • 4
    You need to have written to the bot, before the bot can send you a message.
    – Maak
    Dec 16, 2016 at 0:30
  • what is get id command? maybe its not user id and its bot id Dec 16, 2016 at 15:14
  • 1
    He means the @myidbot which does return the user id.
    – Maak
    Dec 17, 2016 at 17:14
  • @Maak when I write to the bot, I just get an immediate answer about default commands.
    – Adam
    Jan 15, 2022 at 11:08
  • @Maak How do you write to the bot?
    – Bhav
    Feb 2, 2022 at 21:09

9 Answers 9

38

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

2
  • How do we get their bot's name?
    – Ritter7
    Oct 25, 2020 at 8:29
  • 2
    How to accomplish this? I have opened bot's page in Telegram and typed something. This didn't help
    – Dims
    Jun 12, 2022 at 13:11
18

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
3
  • 1
    This should be updated in Telegram docs. I wasted hours trying out various things before I found the solution.
    – NKM
    Jan 30, 2021 at 6: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, 2021 at 12:14
  • That did the trick for me. Probably because the group in question is of type "Supergroup".
    – Kuepper
    Oct 13, 2021 at 17:09
8

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"
3
  • 1
    This is not the case. You need to message the bot before it can message you
    – Bijan
    Jul 10, 2020 at 18:45
  • 3
    This is not asked. Especially group ids start with -100.
    – PythoNic
    Feb 14, 2021 at 12:11
  • This worked for me too - is this documented anywhere?
    – Malachi
    Nov 1, 2021 at 20:37
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.

1
  • 1
    the link is broken Oct 7, 2020 at 6:19
1

For me it worked only with @ prefix before channel id

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.

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

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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.