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
3035 silver badges13 bronze badges
asked Dec 15, 2016 at 23:10
6
As @maak pointed out, you need to first send a message to the bot before the bot can send messages to you.
answered Dec 22, 2016 at 21:07
BijanBijan
7,75718 gold badges89 silver badges149 bronze badges
2
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
answered Jan 30, 2021 at 6:51
NKMNKM
6021 gold badge6 silver badges19 bronze badges
3
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"
answered Jul 10, 2020 at 13:12
Dan WaltersDan Walters
1,2181 gold badge18 silver badges30 bronze badges
3
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.
answered Jan 18 at 9:02
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.
answered Mar 28, 2018 at 8:44
Martin S.Martin S.
2561 silver badge10 bronze badges
1
For me it worked only with @
prefix before channel id
answered May 14, 2021 at 19:47
kashlokashlo
2,3131 gold badge28 silver badges39 bronze badges
If you want to use a bot message to the channel, you can refer step here
Steps:
- Create a Telegram public channel
- Create a Telegram BOT (for example
x_bot
) via BotFather - 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.
answered Nov 25, 2021 at 7:32
Tan NguyenTan Nguyen
9577 silver badges8 bronze badges
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
answered Dec 7, 2022 at 9:36
1
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.7k14 gold badges30 silver badges40 bronze badges
answered Jul 5, 2021 at 12:12
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.
answered Jul 1 at 8:30
using the user_id in int() form worked for me
answered Aug 26 at 11:33
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
3035 silver badges13 bronze badges
asked Dec 15, 2016 at 23:10
6
As @maak pointed out, you need to first send a message to the bot before the bot can send messages to you.
answered Dec 22, 2016 at 21:07
BijanBijan
7,75718 gold badges89 silver badges149 bronze badges
2
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
answered Jan 30, 2021 at 6:51
NKMNKM
6021 gold badge6 silver badges19 bronze badges
3
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"
answered Jul 10, 2020 at 13:12
Dan WaltersDan Walters
1,2181 gold badge18 silver badges30 bronze badges
3
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.
answered Jan 18 at 9:02
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.
answered Mar 28, 2018 at 8:44
Martin S.Martin S.
2561 silver badge10 bronze badges
1
For me it worked only with @
prefix before channel id
answered May 14, 2021 at 19:47
kashlokashlo
2,3131 gold badge28 silver badges39 bronze badges
If you want to use a bot message to the channel, you can refer step here
Steps:
- Create a Telegram public channel
- Create a Telegram BOT (for example
x_bot
) via BotFather - 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.
answered Nov 25, 2021 at 7:32
Tan NguyenTan Nguyen
9577 silver badges8 bronze badges
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
answered Dec 7, 2022 at 9:36
1
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.7k14 gold badges30 silver badges40 bronze badges
answered Jul 5, 2021 at 12:12
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.
answered Jul 1 at 8:30
using the user_id in int() form worked for me
answered Aug 26 at 11:33
Please check the bot token is correct and that you’ve messaged & obtained the chat id from the same bot which is configured.
Chat IDs are unique to each bot for every user and cannot be shared/reused.
I have tried with another bot chat id, i have got same error. The token is
correct cause, i can send text via bot api.
…
You can’t use «another bot» chat id. I just tested this on a fresh project and I can send the notification without issues.
Here’s how it should be:
- Bot A Token is configured.
- You message Bot A.
- You save the chat ID of the user gotten to your Bot A.
- You use that chat ID to send notifications from your Bot A.
Do not mix with different bots.
Also, what version of Laravel and this channel lib you’re using?
I have done the steps appropriately. Laravel 7 and latest version on the
package
…
On Thu, Sep 24, 2020, 6:59 PM Irfaq Syed ***@***.***> wrote:
You can’t use «another bot» chat id. I just tested this on a fresh project
and I can send the notification without issues.
Here’s how it should be:
— Bot A Token is configured.
— You message Bot A.
— You save the chat ID of the user gotten to your Bot A.
— You use that chat ID to send notifications from your Bot A.
Do not mix with different bots.
Also, what version of Laravel and this channel lib you’re using?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#92 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKGKSF3QH7EDPXB3NGN5Q3LSHM7DXANCNFSM4RX526UQ>
.
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.
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.
Just use Http request can send messages,but use this package not work
You can use a Telegram bot named @userinfobot to get the ID of a user. Beware, there are many accounts named @userinfobot, use them only if they are a bot.
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.
Just use Http request can send messages,but use this package not work
Same issue.
@gorkau your advice working, sending to user_id works fine.
But I think that package should send messages by user’s login as well
Доброго дня!
Создаю телеграм бота
Вот код:
@bot.message_handler(commands=['start'])
def start(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=1)
btn1 = types.InlineKeyboardButton('Ферма')
markup.add(btn1)
bot.send_message(message.chat.id, parse_mode='html', reply_markup=markup)
if msg == "Ферма":
keyboard = types.InlineKeyboardMarkup(row_width=1)
one_k = types.InlineKeyboardButton(text='Магазин', callback_data='one_k')
two_k = types.InlineKeyboardButton(text='Мои животные', callback_data='two_k')
keyboard.add(one_k, two_k)
bot.send_message(message.chat.id, 'Выберите, куда зайти.', reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.data == 'one_k':
g = "Заяц\nСтоимость 10₽\nПродукты в час: 10"
keyboar = types.InlineKeyboardMarkup(row_width=1)
one = types.InlineKeyboardButton(text='*', callback_data='one')
two = types.InlineKeyboardButton(text='2', callback_data='two')
tree = types.InlineKeyboardButton(text='3', callback_data='tree')
keyboar.add(one, two, tree)
bot.send_photo(id, "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSDZYuv2dlppOlXQuwaj9z2QmwP1bfrcM_w3g&usqp=CAU", g, reply_markup=keyboar)
if call.data == 'one':
s = 'Курица\nСтоимость 10₽\nПродукты в час: 10'
bot.send_photo(id, "https://st.depositphotos.com/1784872/1392/i/600/depositphotos_13927400-stock-photo-chicken-isolated-on-white.jpg", s)
elif call.data == 'two':
b = 'Кошка\nСтоимость 10₽\nПродукты в час: 10"'
bot.send_photo(id, "url изображения"
Answer by Dorothy Castaneda
Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly. See the meta post Discourage screenshots of code and/or errors.
– user202729
Feb 27 at 10:26
,Other commands like /start or just answering on usual messages works fine, but this is the error I get when trying to send the bitcoin rate:,
Stack Overflow for Teams
Where developers & technologists share private knowledge with coworkers
,Of course, don’t forget to edit your chatId, which has to be «The unique identifier for the target chat or username of the target channel (in the format @channelusername)».
It looks like you are giving several arguments for your text parameter, try to wrap it up in a statement, like:
text = json.loads(response)
messageText = 'Продажа '+text["RUB"]["sell"]+'рублей'+'\nПокупка '+text["RUB"]["buy"]+'рублей'
So that:
@bot.message_handler(commands=['bit'])
def bit(message):
link = 'https://blockchain.info/ru/ticker'
response = requests.get(link).text
text = json.loads(response)
chatId = '@channelusername'
messageText = 'Продажа '+text["RUB"]["sell"]+'рублей'+'\nПокупка '+text["RUB"]["buy"]+'рублей'
bot.send_message(chatId, messageText, parse_mode=HTML)
Answer by Denver Kirk
The query contains errors. In the event that a request was created using a form and contains user generated data, the user should be notified that the data must be corrected before the query is repeated.,USER_MIGRATE_X: the user whose identity is being used to execute queries is associated with a different data center (for registration),In all these cases, the error description’s string literal contains the number of the data center (instead of the X) to which the repeated query must be sent.
More information about redirects between data centers »,PHONE_MIGRATE_X: the phone number a user is trying to use for authorization is associated with a different data center.
Error Type
A string literal in the form of /[A-Z_0-9]+/
, which summarizes the problem. For example, AUTH_KEY_UNREGISTERED
. This is an optional parameter.
/[A-Z_0-9]+/
Error Type
A string literal in the form of /[A-Z_0-9]+/
, which summarizes the problem. For example, AUTH_KEY_UNREGISTERED
. This is an optional parameter.
AUTH_KEY_UNREGISTERED
Answer by Ariya Abbott
Successfully merging a pull request may close this issue.,
Sorry, something went wrong.
,
Why GitHub?
Features →
Mobile →
Actions →
Codespaces →
Packages →
Security →
Code review →
Issues →
Integrations →
GitHub Sponsors →
Customer stories →
,How do you set version_parametr in build environment?
########################
App :: *app*
Env :: stage_app
Status build :: *SUCCESS* ✅
########################
Git commit id :: b0a42b5f
Git commit desc :: Y1-000
Job Name :: experiments/Auth
Job id jenkins :: 001
org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error sending message: [400] Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 84
at org.telegram.telegrambots.api.methods.send.SendMessage.deserializeResponse(SendMessage.java:170)
at org.telegram.telegrambots.api.methods.send.SendMessage.deserializeResponse(SendMessage.java:24)
at jenkinsci.plugins.telegrambot.telegram.TelegramBot.sendApiMethodWithProxy(TelegramBot.java:222)
at jenkinsci.plugins.telegrambot.telegram.TelegramBot.execute(TelegramBot.java:153)
at jenkinsci.plugins.telegrambot.telegram.TelegramBot.sendMessage(TelegramBot.java:80)
at jenkinsci.plugins.telegrambot.telegram.TelegramBot.lambda$sendMessage$0(TelegramBot.java:102)
at java.lang.Iterable.forEach(Iterable.java:75)
at jenkinsci.plugins.telegrambot.telegram.TelegramBot.sendMessage(TelegramBot.java:102)
at jenkinsci.plugins.telegrambot.TelegramBotBuilder.perform(TelegramBotBuilder.java:63)
at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
at hudson.model.Build$BuildExecution.build(Build.java:206)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1810)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Answer by Ila Salinas
Checked with 2 different bots in different chats, same result — 400 error. Log level debug doesn’t help, no additional information on error in log files.,lvl=eror msg=»Failed to send webhook» logger=alerting.notifier.telegram error=»Webhook response status 400 Bad Request» webhook=my-telegram-bot,Not really sure. When I searched for this, it seems to be a general error that Telegram returns that can mean a lot of different things.,Upon more troubleshooting, seems like message cannot be sent if the check box for sending image is checked.
Grafana version is 5.0.0-11204pre1.
Direct image rendering (via phanotomjs) is resulted in timeout for any panel if this panel has legend on...
Answer by Marianna Griffin
When my Telegram bot sends sendMessage to Telegram server it gets the error message:,Your bot is stuck to some state where it is sending an empty message. Somehow it will not process any other request on the same URL until that state is changed. Are you printing a list which has the possibility of being empty ?, as I did in my to-do list bot. You can consider running segments of your code one by one till you find the empty message printing segment. ,I got this error too.
I used sendMessage() method only with «low-level» Node https:,curl -s -X POST https://api.telegram.org/bot{apitoken}/sendMessage \
-F chat_id=’-1234567890′ -F text=’test message’
When my Telegram bot sends sendMessage to Telegram server it gets the error message:
{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}
The problem appeared this morning, before that my bot worked a whole year without errors. GetUpdates command works well as before. I use GET HTTP method to send commads:
https://api.telegram.org/bot<MyToken>/sendMessage
with UTF-8-encoded data attached:
{"chat_id":123456789,"text":"any text"}
Answer by Laurel Hickman
i get such this error :,What is the reason of that error?
Bad Request: wrong file identifier/HTTP URL specified,when i use this code:,Go to @webpagebot and send him an URL to the file. The telegram’s cache will be invalidated, and this should work. Seems that it is a bug on the server.
when i use this code:
$url = 'https://api.telegram.org/bot'.token.'/sendVideo?chat_id='.uid."&video=".$file."&caption="
.urlencode($caption);
file_get_contents($url);
i get such this error :
{"ok":false,"error_code":400,"description":"Bad Request: wrong file identifier/HTTP URL specified"}
Answer by Christina Madden
{«ok»:false,»error_code»:400,»description»:»Bad Request: chat not found»}
where my written code in python file is :,what could be reason of this error?,‘Channel id’ is the id name that you have provided in your invite link
Here I’ve attached my channel that i creates, the highlighted one is the ‘channel id’ that is to be provided for receiving the alert message… This can clear out the ‘400:Chat not found’ error!!
,Most Probably the error resides in your configuration i.e. in your telegram chat id and bot id. Check it once and make sure it matches your correct data.
what could be reason of this error?
{"ok":false,"error_code":400,"description":"Bad Request: chat not found"}
where my written code in python file is :
{"ok":false,"error_code":400,"description":"Bad Request: chat not found"}