skip to Main Content

In my own library I’m trying to send a message using “sendMessage” API method from Telegram Bot API. I’ve create a channel titled, say, “my_channel123” and my user name is “my_username123”. So I’m an admin of the channel and the only user.

When I’m trying to send a message to the whole channel or myself from a bot, I get an error Bad request 400. Here’s how I’m trying to do that:

my_bot.send_message(chat_id="@my_channel123", text="tetfdsfd")

# or
my_bot.send_message(chat_id="@my_channel123my_username123", text= "tetfdsfd")

# or
my_bot.send_message(chat_id="@my_username123", text="tetfdsfd")

I believe the error is somewhere in the format of the ids of the channel or user name or both. Are all these 3 calls correct, meaning is the format I use for chat_id correct?

Note that most likely the probable case is the id of the chat or user_name (or rather, the format) or something else because other post and get methods in my library work properly.

2

Answers


  1. You have to add the bot to be an administrator of the channel before it can send messages to the channel. After you do that, your first line should work:

    my_bot.send_message(chat_id="@my_channel123", text="tetfdsfd")
    

    Also remember that whatever follows the @ should be the channel username, not the title.

    Login or Signup to reply.
  2. when sending a message to a channel, i think its easiest to send it with the channel id…
    there are a few ways to get the channel id

    1. if the bot is a admin in the channel (go to “add admin…” and search for your bot to add it), then any message sent in the channel, the bot would be able to see it, and in every message there is an id of the sender, if its in a channel, its the channel id…
    2. use this bot @ChannelIdBot… though the bot will still need to be in the channel as an admin, in order to send messages…
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search