4

I am trying to send a message on a chat on telegram. I want telegram setups to be done only with the official UI as I want it to be possibly done by by an end user.

Here are telegram setup I do:

  • I created the bot XXXXXXX_bot with Botfather by getting the token: no problem
  • I created a Channel:
    • click "new channel"
    • channel name: TestChannel
    • click "Next"
    • select "Private channel"
    • click "Save"
    • Add my bot XXXXXXX_bot
    • Click "Make admin"
    • Click "Save"
  • I create the chat:
    • open the channel TestChannel
    • on the channel menu, select "Manage Channel"
    • click on "Add a group" in the discussion
    • click on "Create a new group"
    • group name: TestChannelChat
    • click "Create"
    • click "Save"
  • I add the bot to the new group:
    • open the group TestChannelChat
    • on the right panel, I click "add member"
    • Add my bot XXXXXXX_bot
    • click "Add"
    • right click on the newly added user in the chat and select "Promote to admin"
    • click "Save"

Here is the setup of admins of the channel:

enter image description here

At the end of the day, the setup of the chat/group is the following in the telegram UI:

enter image description here

I make the following http call :

GET https://api.telegram.org/botXXXXXXTOKENXXXXXXX/sendMessage?chat_id=@TestChannelChat&text=coucou

that gives me the following answer:

{
  "ok": false,
  "error_code": 403,
  "description": "Forbidden: bot is not a member of the supergroup chat"
}

I also made the following (to bypass the actual chat and directly publish a message in the channel):

GET https://api.telegram.org/botXXXXXXTOKENXXXXXXX/sendMessage?chat_id=@TestChannel&text=coucou

that gives me the following answer:

{
  "ok": false,
  "error_code": 403,
  "description": "Forbidden: bot is not a member of the channel chat"
}

One simple question, as a bot is also meant to broadcast messages, what part of its setup am I missing?

Stephane
  • 1,359
  • 1
  • 15
  • 27

2 Answers2

4

You are in a right track for working with Telegram bots. But first understand about chat_id.

chat_id is either chat username or id of the chat. You can set username only for Public chats/groups. In your case you have a private group and there's no username for it. The value for chat_id that you're passing @TestChannelChat doesn't belong to your chat. You have to pass the id of the chat or set a public username and pass it.

If you don't know how to check id of the chat, read here: https://stackoverflow.com/a/38388851/10359385

Gagan T K
  • 588
  • 2
  • 13
  • Cool, thanks. Is there a neater way to get the id of the chat? – Stephane Jan 17 '21 at 19:10
  • You can get from bot updates API too. https://api.telegram.org/bot{token}/getUpdates – Gagan T K Jan 18 '21 at 03:19
  • This is what I did, but sending a dummy message to the chat, and then, polling received message by the bot is not very "neat". I expected something like api.telegram.org/MyUserTokenThatIGetIDontKnowHow/getChannels – Stephane Jan 18 '21 at 14:19
-1

used send post/message your private chat:

@dispatcher.message_handler(chat_type=[ChatType.SUPERGROUP])
async def send_chat_msg(message: types.Message):
    await bot.send_message(f'Message in your private chat ', reply_markup=markup)
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Puch
  • 1
  • 1