Telegram bot message delivery using username instead of user ID

I’m working with a Python Telegram bot using the telepot library. Currently I can send messages when I have the user’s numeric ID. But I’m wondering if there’s a method to send messages directly using just the username (the @username format) instead of needing the numeric user ID. Is this possible? Also, is there any way to get the numeric user ID from a username? I’ve been searching but can’t find clear documentation on this. Any help would be great!

Unfortunately, the Telegram Bot API doesn’t support sending messages directly to usernames for privacy reasons. You must have the numeric chat ID to initiate conversations. However, there are a few workarounds I’ve used in practice. If users have interacted with your bot before, you can store their chat IDs in a database mapped to their usernames. Another approach is implementing a registration system where users provide their username when they first contact your bot. For public channels and groups, you can use @username, but this doesn’t work for private messaging. The Bot API deliberately restricts this functionality to prevent spam and protect user privacy. Consider redesigning your bot flow so users initiate contact first, then you can store their IDs for future messaging.

hey, ur right, telepot needs the numeric id to msg users. there’s no way to send msgs just using the @username format. if the user contacts ur bot first, then you can saver their chat_id. but yeah, telegram’s pretty strict on that.

I ran into this exact issue when building my first telegram bot last year. The short answer is no, you cannot send messages using just the username through the Bot API. Telegram requires the numeric chat ID for direct messaging to users. What I ended up doing was creating a simple command like /register that users could send to my bot, which would then store their chat ID along with their username in my database. This way, when I needed to message someone later, I could look up their chat ID by their username from my own records. The telepot library works the same way as any other telegram bot library in this regard - it’s a limitation of the API itself, not the library. One thing to note is that even if you find someone’s username, there’s no official API endpoint to convert it to a chat ID without them having interacted with your bot first.