问题描述
当我向 Telegram Bot 发送消息时,它会毫无问题地响应.
When I send a message to my Telegram Bot, it responses with no problems.
我想限制访问权限,以便我和只有我可以向它发送消息.
I wanna limit the access such that me and only me can send message to it.
我该怎么做?
推荐答案
由于这个问题与python-telegram-bot有关,以下信息与之相关:
As this question is related to python-telegram-bot, information below is related to it:
当您向机器人的调度程序添加处理程序时,您可以指定各种预先构建的过滤器(在 docs, github) 或者您可以创建自定义过滤器以过滤传入的更新.
When you add handlers to your bot's dispatcher, you can specify various pre-built filters (read more at docs, github) or you can create custom ones in order to filter incoming updates.
要限制对特定用户的访问,您需要在初始化处理程序时添加Filters.user(username="@telegramusername")
,例如:
To limit access to a specific user, you need to add Filters.user(username="@telegramusername")
when initializing handler, e.g.:
dispatcher.add_handler(CommandHandler("start", text_callback, Filters.user(username="@username")))
此处理程序仅接受来自用户名为 @username
的用户的 /start
命令.
This handler will accept /start
command only from user with username @username
.
您还可以指定用户 ID 而不是用户名,我强烈建议您这样做,因为后者是非常数的,并且可以随着时间的推移而更改.
You can also specify user-id instead of username, which I would highly recommend, as latter is non-constant and can be changed over time.
这篇关于如何限制对电报机器人的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!