【问题标题】:If the message is a reply, bot should get id of a user如果消息是回复,机器人应该获取用户的 id
【发布时间】:2020-12-12 21:14:54
【问题描述】:

我正在做一个机器人版主。我正在执行命令“禁止”。如果我通过输入/ban 回复某人的消息,bot 应该获取它的 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

谢谢

【问题讨论】:

  • 先到这里 - stackoverflow.com/help/how-to-ask。然后,当您尝试过自己并遇到特定问题时,请分享您的代码并就特定问题提出特定问题。 Stack Overflow 可以帮助您解决遇到的问题,而不是让您可以让其他人为您编写代码。

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


【解决方案1】:

您可以将用户限制在至少 30 秒。 限制代码:

bot.restrict_chat_member(message.chat.id, 
                         message.from_user.id, 
                         can_send_messages=False, 
                         until_date=int(time.time())+30)
#you can replace 30(seconds) by more.

unix时间

【讨论】:

  • 谢谢你,user390489!但我还需要获取我回复的用户 ID
【解决方案2】:

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

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

【讨论】:

  • 你能告诉更多 Mehdi Mostafavi 吗?
  • @dayod 如果用户A回复用户B。message.from_user包含UserA信息,message.reply_to_message.from_user包含UserB。
猜你喜欢
  • 2020-08-01
  • 2019-05-10
  • 2018-10-05
  • 2018-06-05
  • 1970-01-01
  • 2021-10-13
  • 2017-06-23
  • 2021-02-06
  • 2021-02-01
相关资源
最近更新 更多