How do I add my bot to a channel?
I'm using my bot to tell important news, but when I using sendMessage
to the channel I'm receiving the following error:
{"ok":false,"error_code":403,"description":"Error: Forbidden: bot is not a participant of the channel"}
In the Change Log they mention it's not possible via the client to send a message to a channel, but then what is the other way to do that?
So, the question is, how to add my bot to the channel?
Now all clients allow to do it, but it's not pretty simple.
In any Telegram client:
- Open Channel info (in app title)
- Choose
Administrators
- Add Administrator
- There will be no bots in contact list, so you need to search for it. Enter your bot's username
- Clicking on it you make it as administrator.
This is how I've added a bot to my channel and set up notifications:
- Make sure the channel is public (you can set it private later)
- Add administrators > Type the bot username and make it administrator
- Your bot will join your channel
- set a channel id by setting the channel url like
telegram.me/whateverIWantAndAvailable
the channel id will be @whateverIWantAndAvailable
Now set up your bot to send notifications by pusshing the messages here:
https://api.telegram.org/botTOKENOFTHEBOT/sendMessage?chat_id=@whateverIWantAndAvailable&text=Test
the message which bot will notify is: Test
I strongly suggest an urlencode of the message like
https://api.telegram.org/botTOKENOFTHEBOT/sendMessage?chat_id=@whateverIWantAndAvailable&text=Testing%20if%20this%20works
in php you can use urlencode("Test if this works"); in js you can encodeURIComponent("Test if this works");
I hope it helps
As of now:
- Only the creator of the channel can add a bot.
- Other administrators can't add bots to channels.
- Channel can be public or private (doesn't matter)
- bots can be added only as admins, not members.*
To add the bot to your channel:
click on the channel name:
click on admins:
click on Add Admin:
search for your bot like @your_bot_name, and click add:**
* In some platforms like mac native telegram client it may look like that you can add bot as a member, but at the end it won't work.
** the bot doesn't need to be in your contact list.
Are you using the right chat_id and including your bot's token after "bot" in the address? (api.telegram.org/bottoken/sendMessage)
This page explains a few things about sending (down in "sendMessage" section) - basic stuff, but I often forget the basics.
To quote:
In order to use the sendMessage method we need to use the proper chat_id.
First things first let's send the /start command to our bot via a Telegram client.
After sent this command let's perform a getUpdates commands.
curl -s \
-X POST \ https://api.telegram.org/bot<token>/getUpdates \ | jq .
The response will be like the following
{ "result": [
{
"message": {
"text": "/start",
"date": 1435176541,
"chat": {
"username": "yourusername",
"first_name": "yourfirstname",
"id": 65535
},
"from": {
"username": "yourusername",
"first_name": "yourfirstname",
"id": 65535
},
"message_id": 1
},
"update_id": 714636917
} ], "ok": true }
We are interested in the property result.message[0].chat.id, save this information elsewhere.
Please note that this is only an example, you may want to set up some automatism to handle those informations Now how we can send a message ? It's simple let's check out this snippet.
curl -s \
-X POST \ https://api.telegram.org/bot<token>/sendMessage \
-d text="A message from your bot" \
-d chat_id=65535 \ | jq .
Where chat_id is the piece of information saved before.
I hope that helps.