如何在一个电报机器人认证? [英] how to have authentication in a telegram bot?

查看:2604
本文介绍了如何在一个电报机器人认证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

电报聊天室都喜欢的网站。

假设我们有一些信息,我们只希望限制某些用户,在网站上,我们将验证。

我们

如何实现对电报机器人一样的效果?

更新:

有人告诉我,我可以使用深层链接。查看描述这里

我将重现它如下:


  

      
  1. 用合适的用户名创建一个机器人,例如@ExampleComBot

  2.   
  3. 设置了一个网络挂接传入的消息

  4.   
  5. 生成一个足够长的随机串,例如$ memcache_key =vCH1vGWJxfSeofSAs0K5PA

  6.   
  7. 把值123与键$ memcache_key入内存缓存3600秒(1小时)

  8.   
  9. 显示我们的用户按钮<一个href=\"https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA\">https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA

  10.   
  11. 配置网络挂接处理器Memcached的查询与在与/ start开始传入邮件传递的参数。
      如果密钥存在,记录传递给网络挂接的chat_id
      telegram_chat_id为用户123取下内存缓存的关键。

  12.   
  13. 现在,当我们想通知发送给用户123,检查他们是否有外地telegram_chat_id。如果是的话,使用的sendMessage
      机器人API中的方法向他们发送消息的电报。

  14.   

我知道怎么做的步骤1。

我想了解的其余部分。

这是我的形象在脑海,当我试图破译第2步。

于是各种电报客户提供服务器的电报交谈ExampleBot对自己的应用时,通信。沟通是2路。

步骤2表明,电报服务器将通过网络挂接更新ExampleBot服务器。一个网络挂接只是一个网址。

到目前为止,我是正确的?

什么是对使用此认证的下一步是什么?


解决方案

忘记网络挂接啄。
深联解释说:


  1. 让用户登录,在实际的网站的实际用户名密码认证。

  2. 生成一个唯一的哈希code(我们称之为unique_ code)

  3. 保存unique_ code->用户名到数据库或键值存储。

  4. 显示用户的网址<一个href=\"https://telegram.me/YOURBOTNAME?start=unique_$c$c\">https://telegram.me/YOURBOTNAME?start=unique_$c$c

  5. 现在,只要用户打开这个网址电报和presses'开始',你的机器人将收到包含/启动unique_ code',其中unique_ code是当然的短信通过实际的哈希值code取代。

  6. 让机器人通过查询unique_ code数据库或关键值存储检索的用户名。

  7. 保存chat_id->用户名到数据库或键值存储。

现在,当你的机器人收到另一条消息,它可以在数据库中查询message.chat.id检查,如果该消息是从这个特定的用户。 (和相应的处理)

有些code(使用 pyTelegramBotAPI ):

 进口telebot
进口时间BOT = telebot.TeleBot('令牌')高清extract_unique_ code(文本):
    #提取从发送/ start命令unique_ code。
    返回text.split()[1]如果len(text.split())&GT; 1其他无高清in_storage(unique_ code):
    #应该检查是否在存储中存在一个独特的code
    返回True高清get_username_from_storage(unique_ code):
    #请问查询到存储,检索相关的用户名
    #应该由一个真正的数据库的查找替换。
    返回ABC如果in_storage(unique_ code)其他无高清save_chat_id(chat_id,用户名):
    #保存chat_id-&GT;用户名存储
    #应该由真正的数据库查询所取代。
    通过@ bot.message_handler(命令= ['开始'])
高清send_welcome(消息):
    unique_ code = extract_unique_ code(message.text)
    如果unique_ code:#如果'/启动命令包含unique_ code
        用户名= get_username_from_storage(unique_ code)
        如果用户名:如果用户名存在于我们的数据库#
            save_chat_id(message.chat.id,用户名)
            回复=你好{0},你怎么样?格式(用户名)
        其他:
            回复=我不知道你是谁......
    其他:
        回复=请通过在网站上提供的网址来看我。
    bot.reply_to(消息,回复)bot.polling()而真正的:
    time.sleep(0)

注:unique_ code将不被显示为'/启动unique_ code',只有'/启动,在电报客户端,但你的机器人仍会收到'/启动unique_ code'。

我能想到把我的头顶部的另一种方式是通过/ AUTH用户名密码僵尸之内直接认证,但请记住,你的用户名和密码这种方式保存聊天记录。

编辑1:
请记住,你不这样做的有无的使用网络挂接来处理消息。事实上,pyTelegramBotAPI使用getUpdates方法而不是一个网络挂接

Telegram Bots are ready now.

If we use the analogy of web browser and websites, the telegram client apps are like the browser clients.

The Telegram Chatrooms are like websites.

Suppose we have some information we only want to restrict to certain users, on the websites, we will have authentication.

How do we achieve the same effect on the Telegram Bots?

UPDATE:

I was told that I can use deep linking. See description here

I will reproduce it below:

  1. Create a bot with a suitable username, e.g. @ExampleComBot
  2. Set up a webhook for incoming messages
  3. Generate a random string of a sufficient length, e.g. $memcache_key = "vCH1vGWJxfSeofSAs0K5PA"
  4. Put the value 123 with the key $memcache_key into Memcache for 3600 seconds (one hour)
  5. Show our user the button https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA
  6. Configure the webhook processor to query Memcached with the parameter that is passed in incoming messages beginning with /start. If the key exists, record the chat_id passed to the webhook as telegram_chat_id for the user 123. Remove the key from Memcache.
  7. Now when we want to send a notification to the user 123, check if they have the field telegram_chat_id. If yes, use the sendMessage method in the Bot API to send them a message in Telegram.

I know how to do step 1.

I want to understand the rest.

This is the image I have in mind when I try to decipher step 2.

So the various telegram clients communicate with the Telegram Server when talking to ExampleBot on their apps. The communication is 2-way.

Step 2 suggests that the Telegram Server will update the ExampleBot Server via a webhook. A webhook is just a url.

So far, am I correct?

What's the next step towards using this for authentication?

解决方案

Forget about the webhook thingy. The deep linking explained:

  1. Let the user log in on an actual website with actual username-password authentication.
  2. Generate a unique hashcode (we will call it unique_code)
  3. Save unique_code->username to a database or key-value storage.
  4. Show the user the URL https://telegram.me/YOURBOTNAME?start=unique_code
  5. Now as soon as the user opens this URL in Telegram and presses 'Start', your bot will receive a text message containing '/start unique_code', where unique_code is of course replaced by the actual hashcode.
  6. Let the bot retrieve the username by querying the database or key-value storage for unique_code.
  7. Save chat_id->username to a database or key-value storage.

Now when your bot receives another message, it can query message.chat.id in the database to check if the message is from this specific user. (And handle accordingly)

Some code (using 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)

Note: the unique_code will not be shown as '/start unique_code', only '/start', in the Telegram client, but your bot will still receive '/start unique_code'.

Another way I can think of off the top of my head is direct authentication within the bot via '/auth username password', but keep in mind that your username and password will be saved in chat history this way.

Edit 1: Keep in mind that you do not have to use WebHooks to process messages. In fact, pyTelegramBotAPI uses the getUpdates method rather than a webhook.

这篇关于如何在一个电报机器人认证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆