Telegram 机器人现已准备就绪。
如果我们使用网络浏览器和网站进行类比,那么电报客户端应用程序就像浏览器客户端。
Telegram 聊天室就像网站。
假设我们有一些信息,我们只想限制某些用户,在网站上,我们将进行身份验证。
我们如何在 Telegram 机器人上达到同样的效果?
有人告诉我可以使用深层链接。参见说明here https://core.telegram.org/bots#deep-linking
我将在下面重现它:
- 使用合适的用户名创建一个机器人,例如@ExampleComBot
- 为传入消息设置 Webhook
- 生成足够长度的随机字符串,例如$memcache_key =“vCH1vGWJxfSeofSAs0K5PA”
- 将带有 $memcached 键的值 123 放入 Memcache 中 3600 秒(一小时)
- 向我们的用户显示按钮https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA
- 配置 Webhook 处理器以使用在以 /start 开头的传入消息中传递的参数查询 Memcached。
如果该密钥存在,则将传递给 webhook 的 chat_id 记录为
用户 123 的 telegram_chat_id。从 Memcache 中删除密钥。
- 现在,当我们想要向用户 123 发送通知时,检查他们是否具有字段 telegram chat_id。如果是,请使用 Bot API 中的 sendMessage 方法在 Telegram 中向他们发送消息。
我知道怎么做第 1 步。
我想了解其余的内容。
当我尝试解读步骤 2 时,我脑海中浮现出这样的画面。
因此,各种 Telegram 客户端在其应用程序上与 ExampleBot 通信时与 Telegram Server 进行通信。通信是双向的。
步骤 2 建议 Telegram 服务器通过 Webhook 更新 ExampleBot 服务器。 Webhook 只是一个 URL。
到目前为止,我的说法正确吗?
使用它进行身份验证的下一步是什么?
Update:我使用一个非常简单的 PHP 应用程序创建了一个 GitHub 存储库来说明下面解释的概念:
https://github.com/pevdh/telegram-auth-example https://github.com/pevdh/telegram-auth-example
是否使用 webhook 实际上并不重要。
“深层链接”解释道:
- 让用户通过实际的用户名密码身份验证登录实际网站。
- 生成唯一的哈希码(我们将其称为 unique_code)
- 将 unique_code->username 保存到数据库或键值存储中。
- 向用户显示 URLhttps://telegram.me/YOURBOTNAME?start=unique_code https://telegram.me/YOURBOTNAME?start=unique_code
- 现在,一旦用户在 Telegram 中打开此 URL 并按“开始”,您的机器人就会收到一条包含“/start unique_code”的短信,其中 unique_code 当然会被实际的哈希码替换。
- 让机器人通过查询数据库或键值存储中的 unique_code 来检索用户名。
- 将 chat_id->username 保存到数据库或键值存储中。
现在,当您的机器人收到另一条消息时,它可以查询数据库中的 message.chat.id 以检查该消息是否来自该特定用户。 (并进行相应处理)
一些代码(使用pyTelegramBotAPI https://github.com/eternnoir/pyTelegramBotAPI):
import telebot
import time
bot = telebot.TeleBot('TOKEN')
def extract_unique_code(text):
# Extracts the unique_code from the sent /start command.
return text.split()[1] if len(text.split()) > 1 else None
def in_storage(unique_code):
# Should check if a unique code exists in storage
return True
def get_username_from_storage(unique_code):
# Does a query to the storage, retrieving the associated username
# Should be replaced by a real database-lookup.
return "ABC" if in_storage(unique_code) else None
def save_chat_id(chat_id, username):
# Save the chat_id->username to storage
# Should be replaced by a real database query.
pass
@bot.message_handler(commands=['start'])
def send_welcome(message):
unique_code = extract_unique_code(message.text)
if unique_code: # if the '/start' command contains a unique_code
username = get_username_from_storage(unique_code)
if username: # if the username exists in our database
save_chat_id(message.chat.id, username)
reply = "Hello {0}, how are you?".format(username)
else:
reply = "I have no clue who you are..."
else:
reply = "Please visit me via a provided URL from the website."
bot.reply_to(message, reply)
bot.polling()
while True:
time.sleep(0)
注意:在 Telegram 客户端中,unique_code 不会显示为“/start unique_code”,而只会显示为“/start”,但您的机器人仍会收到“/start unique_code”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)