Trouble sending messages with Telegram Bot API

I’m having issues with the Telegram Bot API’s sendMessage method in my custom library. I made a channel called “bottest_channel” and I’m the only user (“bottest_user”). Even though I’m the admin, I can’t send messages to the channel or myself using the bot.

Here’s what I’ve tried:

bot.message_send(receiver="@bottest_channel", content="Hello, channel!")

# Also tried
bot.message_send(receiver="@bottest_channel@bottest_user", content="Hello, user!")

# And this
bot.message_send(receiver="@bottest_user", content="Hello, me!")

All these attempts give me a 400 Bad Request error. I think the problem might be with how I’m formatting the channel or username IDs. Are these the right ways to use the receiver parameter?

Other API methods in my library are working fine, so I’m pretty sure the issue is specific to sendMessage. Any ideas on what I’m doing wrong here?

I’ve been there, mate. Ran into the same headache when I first started tinkering with the Telegram Bot API. Here’s what worked for me:

For channels, you need to use the channel’s ID, not the username. It’s a bit tricky to get, but you can use @username_to_id_bot to find it. It’ll look something like ‘-1001234567890’.

For users, you’ll want their numeric ID. Again, @username_to_id_bot can help, or you can use @userinfobot.

So your code might look like:

bot.message_send(receiver=‘-1001234567890’, content=‘Hello, channel!’)
bot.message_send(receiver=‘987654321’, content=‘Hello, user!’)

Also, double-check that your bot has the right permissions. It needs to be an admin in the channel to post there.

If you’re still stuck, it might be worth looking at the raw API response as it sometimes gives more detailed error info.

hey, maybe u could use the channel’s actual id instead of the name? i faced this too. check that your bot has admin perms in channel. sometimes telegram expects quotes around ids or just the numeric data. hope it helps!

It appears you’re encountering a common issue with the Telegram Bot API. The problem likely stems from how you’re specifying the receiver. For channels, you need to use the channel’s unique identifier, which starts with ‘-100’ followed by the channel’s ID. For users, you should use their numeric user ID.

Try modifying your code like this:

bot.message_send(receiver='-100123456789', content='Hello, channel!')
bot.message_send(receiver='123456789', content='Hello, user!')

To obtain these IDs, you can use the getUpdates method or a third-party bot like @userinfobot. Also, ensure your bot has the necessary permissions in the channel and that it’s been added as an administrator. If issues persist, double-check your bot token and API endpoint.