47

电报机器人现在准备好了。

如果我们使用 Web 浏览器和网站的类比,电报客户端应用程序就像浏览器客户端。

电报聊天室就像网站。

假设我们有一些我们只想限制某些用户的信息,在网站上,我们将进行身份验证。

我们如何在 Telegram Bots 上实现相同的效果?

有人告诉我我可以使用深度链接。请参阅此处的说明

我将在下面重现它:

  1. 创建一个具有合适用户名的机器人,例如@ExampleComBot
  2. 为传入消息设置 webhook
  3. 生成足够长的随机字符串,例如 $memcache_key = "vCH1vGWJxfSeofSAs0K5PA"
  4. 将值 123 和键 $memcache_key 放入 Memcache 360​​0 秒(一小时)
  5. 向我们的用户显示按钮https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA
  6. 配置 webhook 处理器以使用以 /start 开头的传入消息中传递的参数查询 Memcached。如果密钥存在,则将传递给 webhook 的 chat_id 记录为用户 123 的 telegram_chat_id。从 Memcache 中删除密钥。
  7. 现在,当我们要向用户 123 发送通知时,检查他们是否有字段 telegram_chat_id。如果是,请使用 Bot API 中的 sendMessage 方法在 Telegram 中向他们发送消息。

我知道如何执行第 1 步。

我想了解其余的。

这是我尝试破译步骤 2 时想到的图像。

在此处输入图像描述

因此,各种电报客户端在与其应用程序上的 ExampleBot 通信时与电报服务器通信。沟通是双向的。

第 2 步建议 Telegram 服务器将通过 webhook 更新 ExampleBot 服务器。Webhook 只是一个 URL。

到目前为止,我是对的吗?

使用它进行身份验证的下一步是什么?

4

4 回答 4

68

更新:我用一个非常简单的 PHP 应用程序创建了一个 GitHub 存储库来说明下面解释的概念:

https://github.com/pevdh/telegram-auth-example


是否使用 webhook无关紧要。 “深度链接”解释说:

  1. 让用户使用实际的用户名密码身份验证登录实际网站。
  2. 生成唯一的哈希码(我们将其称为 unique_code)
  3. 将 unique_code->username 保存到数据库或键值存储。
  4. 向用户显示 URL https://telegram.me/YOURBOTNAME?start=unique_code
  5. 现在,只要用户在 Telegram 中打开此 URL 并按下“开始”,您的机器人就会收到一条包含“/start unique_code”的文本消息,其中 unique_code 当然会被实际的哈希码替换。
  6. 让机器人通过在数据库或键值存储中查询 unique_code 来检索用户名。
  7. 将 chat_id->username 保存到数据库或键值存储。

现在,当您的机器人收到另一条消息时,它可以查询数据库中的 message.chat.id 以检查该消息是否来自该特定用户。(并相应地处理)

一些代码(使用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”。

于 2015-07-03T18:33:26.020 回答
6

自 2018 年 2 月起,您可以使用Telegram Login Widget通过 Telegram 在您的网站上授权用户。

于 2018-02-27T08:14:40.323 回答
5

我刚刚使用Django的深度链接实现了一个身份验证解决方案

此解决方案生成身份验证令牌以关联 Telegram 聊天和 Django 用户。当一些电报用户想要访问受限区域时,它会收到一条电报消息,其中包含登录网络的链接。一个登录的网站提供了一个链接,以使用深度链接开始新的经过身份验证的聊天。

还有一个投票示例的演示,只有登录的用户不能通过电报投票。

于 2016-02-03T18:34:07.110 回答
4

到目前为止,您是正确的。

但是,您的要求有点模糊。让我们以另一种方式来看待它。如果您想向特殊用户发送受限信息,您应该要求用户开始与您的机器人直接聊天,或者简单地使用用户群聊中的 chat_id 开始向他们发送消息。

请注意,只有当用户以“隐私模式”(这是机器人的默认模式)与机器人通信时,您才能访问用户 chat_id。

于 2015-07-02T11:20:54.883 回答