How can I send a message to someone with my telegram bot using their Username
Asked Answered
Y

7

53

I am using the telepot python library, I know that you can send a message when you have someone's UserID(Which is a number). I wanna know if it is possible to send a message to someone without having their UserID but only with their username(The one which starts with '@'), Also if there is a way to convert a username to a UserID.

Yoohoo answered 15/1, 2017 at 18:38 Comment(2)
Possible duplicate of How to obtain Telegram chat_id for a specific user?Salicin
I don't think this is a duplicate - this question is formulated more generally, to find the other question you have to know that you need a "chat_id" in the first place.Grenada
P
19

You can't send message to users using their username that is in form of @username, you can just send messages to channel usernames which your bot is administrator of it. Telegram bot api uses chat_id identifier for sending messages. If you want to achieve chat_id of users, you can use telegram-cli, but it's not easy at all because that project is discontinued and you should debug it yourself. in your case you should do following command:

> resolve_username vahid_mas

and the output will be something like this:

{
  "user": {
    "username": "Vahid_Mas",
    "id": "$010000006459670b02c0c7fd66d44708",
    "last_name": "",
    "peer_type": "user",
    "print_name": "Vahid",
    "flags": 720897,
    "peer_id": 191322468,
    "first_name": "Vahid",
    "phone": "xxxxxxx"
  },
  "online": false,
  "event": "online-status",
  "state": -1,
  "when": "2017-01-22 17:43:16"
}
Provincetown answered 22/1, 2017 at 14:18 Comment(1)
Yo, as of 2022 is there any other methods? I couldnt get this to work.Punishment
P
95
  1. Post one message from User to the Bot.
  2. Open https://api.telegram.org/bot<Bot_token>/getUpdates page.
  3. Find this message and navigate to the result->message->chat->id key.
  4. Use this ID as the [chat_id] parameter to send personal messages to the User.
Pasha answered 7/6, 2018 at 8:14 Comment(2)
Note: the first message NEEDS to be sent via phone not desktop :( #43880743Tion
The privacy of the bot much be changed to disabled so it works with desktop messages.Objurgate
P
21

It is only possible to send messages to users whom have already used /start on your bot. When they start your bot, you can find update.message.from.user_id straight from the message they sent /start with, and you can find update.message.from.username using the same method. In order to send a message to "@Username", you will need them to start your bot, and then store the username with the user_id. Then, you can input the username to find the correct user_id each time you want to send them a message.

Packer answered 24/3, 2017 at 3:18 Comment(0)
P
19

You can't send message to users using their username that is in form of @username, you can just send messages to channel usernames which your bot is administrator of it. Telegram bot api uses chat_id identifier for sending messages. If you want to achieve chat_id of users, you can use telegram-cli, but it's not easy at all because that project is discontinued and you should debug it yourself. in your case you should do following command:

> resolve_username vahid_mas

and the output will be something like this:

{
  "user": {
    "username": "Vahid_Mas",
    "id": "$010000006459670b02c0c7fd66d44708",
    "last_name": "",
    "peer_type": "user",
    "print_name": "Vahid",
    "flags": 720897,
    "peer_id": 191322468,
    "first_name": "Vahid",
    "phone": "xxxxxxx"
  },
  "online": false,
  "event": "online-status",
  "state": -1,
  "when": "2017-01-22 17:43:16"
}
Provincetown answered 22/1, 2017 at 14:18 Comment(1)
Yo, as of 2022 is there any other methods? I couldnt get this to work.Punishment
P
9

As the user you want to learn the ID of, send a message of any content to @JsonDumpBot. It will reply the whole JSON element that it receives from Telegram, including the ID of the user:

enter image description here

Pen answered 24/3, 2020 at 9:41 Comment(0)
I
1

Maybe you can try telethon:

from telethon import TelegramClient

def send_msg(name, msg):
    with TelegramClient(session_file, app_id, app_hash, proxy=my_proxy) as client:
        # client.loop.run_until_complete(client.send_message('me', 'hello')) # send 'hello' to saved messages
        client.loop.run_until_complete(client.send_message(name, msg))

"name" can be "@xxxxx".

Then you can call send_msg(name, msg) in your bot.

Ionic answered 2/11, 2022 at 3:34 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Inexperienced
E
0

It's totally not safe to use other telegram versions available on internet, but I've seen that Telegram Plus has a ability to show you the chat_id of the user in their profile, even tho you don't have their contact.

Another way to extract chat_id of that particular user is that you have the phone number of that account, save it as your contact, then share it to this bot. It's easy to code it yourself but you can forward something from that user to this bot too, if you want to recieve the chat_id.

urmurmur has also mentioned another way. I haven't checked it yet but seems to be interesting.

Epicycle answered 26/11, 2020 at 18:42 Comment(0)
L
0

you can send bulk messages to people in telegram using by v-user telegram bulk message sender

Linoel answered 1/5, 2023 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.