python - 如果消息是回复,机器人应该获取用户的 ID

标签 python bots telegram telegram-bot py-telegram-bot-api

我正在做一个机器人主持人。我正在执行“禁止”命令。如果我通过输入 /ban 回复某人的消息,机器人应该获取其 id 并限制他。 我现在的代码:

@bot.messsage_handler(commands=['ban'])

def ban(message):

    #here I should get the id of a user which I replied in Telegram
    #then bot should restrict him.

PyTelegramBotApi

Python 3.7

谢谢

最佳答案

Message类中,您有一个reply_to_message,它返回一个Message对象,您可以从该对象中找到用户ID。如果用户没有回复任何消息,则等于None。这就是您想要的: message.reply_to_message.from_user

如果您想了解更多信息,可以阅读 Telegram bot API或查看types.py在 github 库中。

关于python - 如果消息是回复,机器人应该获取用户的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63557507/

相关文章:

node.js - 如何在没有机器人的情况下在 node.js 中接收我自己的 Telegram 消息

java - TDLib - 通过他的电话号码向其他人发送消息

python - 字节可以安全地从 UUID 中删除并仍然期望它保持其唯一性吗?

php - 在外部网站上自动填写和提交表格

javascript - Slack/Botkit : How to make bot in slack listen better?

c# - 如何使用 C# 在 Web 嵌入式 bot 中的 bot 框架中发送附件

python - 如何在 Telegram Bot 中获取用户名?

python - 将 ndarray 保存到文本文件时出现问题

python - 如何使用 python 的 ziplib 文件压缩巨大的纯文件 (+20GB)

python - 如何在Python 3中将具有ascii十进制数的字节数组转换为整数