如何在电报机器人中获得身份验证?

Kim*_*cks 30 python authentication telegram telegram-bot

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

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

电报聊天室就像网站一样.

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

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

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

我将在下面复制它:

  1. 使用合适的用户名创建机器人,例如@ExampleComBot
  2. 为传入消息设置webhook
  3. 生成足够长度的随机字符串,例如$ memcache_key ="vCH1vGWJxfSeofSAs0K5PA"
  4. 将带有$ memcache_key键的值123放入Memcache中3600秒(一小时)
  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在应用程序上交谈时,各种电报客户端与Telegram Server进行通信.沟通是双向的.

步骤2建议Telegram Server将通过webhook更新ExampleBot服务器.webhook只是一个URL.

到目前为止,我是对的?

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

Pet*_*ete 44

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

https://github.com/pieham/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)
Run Code Online (Sandbox Code Playgroud)

注意:在电报客户端中,unique_code不会显示为'/ start unique_code',仅显示为'/ start',但您的机器人仍会收到'/ start unique_code'.


Gro*_*sha 6

自 2018 年 2 月起,您可以使用Telegram 登录小工具通过 Telegram 向您网站上的人员授权。


小智 5

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

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

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


小智 4

到目前为止你是正确的。

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

请注意,仅当用户在“隐私模式”(机器人的默认模式)下与机器人通信时,您才有权访问用户 chat_id。