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

后端 未结 7 995
执笔经年
执笔经年 2020-12-15 17:30

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 messag

相关标签:
7条回答
  • 2020-12-15 18:13

    According to Telegram Bot API documentation you should be able to specify @channelusername.

    UPDATE

    There is no possibility to achieve this. I found this note in Telegram Bot API Changelog:

    For this to work, the bot must be an administrator in the channel

    0 讨论(0)
  • 2020-12-15 18:18

    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"
    }
    
    0 讨论(0)
  • 2020-12-15 18:20

    It's really simple with TaaS

    1. Sign up, create an API key for phone number or bot token
    2. Make a POST request to https://api.t-a-a-s.ru/client with JSON body:
    {
      "api_key": "YOUR_API_KEY",
      "@type": "searchPublicChat",
      "username": "@username"
    }
    

    You will receive chat_id of a user in response.

    0 讨论(0)
  • 2020-12-15 18:21

    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:

    0 讨论(0)
  • 2020-12-15 18:22

    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.

    0 讨论(0)
  • 2020-12-15 18:25
    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.
    0 讨论(0)
提交回复
热议问题