Python 如何使用我的电报机器人使用他们的用户名向某人发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41664810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send a message to someone with my telegram bot using their Username
提问by ATheCoder
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.
我正在使用 Telepot python 库,我知道当你有某人的用户 ID(这是一个数字)时你可以发送消息。我想知道是否可以在没有用户 ID 的情况下向某人发送消息,而只能使用他们的用户名(以“@”开头的用户名),另外是否有办法将用户名转换为用户 ID。
采纳答案by Vahid Msm
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:
您不能使用形式为 的用户名向用户发送消息@username
,您只能向您的机器人是其管理员的频道用户名发送消息。Telegram bot api 使用chat_id
标识符来发送消息。如果你想实现chat_id
用户,你可以使用telegram-cli,但是这并不容易,因为那个项目已经停产了,你应该自己调试它。在您的情况下,您应该执行以下命令:
> resolve_username vahid_mas
and the output will be something like this:
输出将是这样的:
{
"user": {
"username": "Vahid_Mas",
"id": "0000006459670b02c0c7fd66d44708",
"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"
}
回答by Andrey Radomanov
- Post one message from User to the Bot.
- Open
https://api.telegram.org/bot<Bot_token>/getUpdates
page. - Find this message and navigate to the result->message->chat->id key.
- Use this ID as the [chat_id] parameter to send personal messages to the User.
- 从用户向机器人发布一条消息。
- 打开
https://api.telegram.org/bot<Bot_token>/getUpdates
页面。 - 找到此消息并导航到 result->message->chat->id 键。
- 使用此 ID 作为 [chat_id] 参数向用户发送个人消息。
回答by Resin Drake
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.
只能向已经在您的机器人上使用 /start 的用户发送消息。当他们启动你的机器人时,你可以直接从他们发送的消息中找到 update.message.from.user_id ,你可以使用相同的方法找到 update.message.from.username 。为了向“@Username”发送消息,您需要它们来启动您的机器人,然后使用 user_id 存储用户名。然后,您可以在每次要向他们发送消息时输入用户名以找到正确的 user_id。
回答by German Lashevich
According to Telegram Bot API documentationyou should be able to specify @channelusername
.
根据Telegram Bot API 文档,您应该能够指定@channelusername
.
UPDATE
更新
There is no possibility to achieve this. I found this notein Telegram Bot API Changelog:
没有可能实现这一目标。我在 Telegram Bot API Changelog 中发现了这个注释:
For this to work, the bot must be an administrator in the channel
为此,机器人必须是频道中的管理员