电报机器人等待用户回复 [英] Telegram bot to wait for user reply

查看:94
本文介绍了电报机器人等待用户回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码用于Telegram Bot,它基本上需要一个人用户名密码并验证它以提供他的平均支出.

The code below is for a Telegram Bot which basically takes a person username and password and verifies it to provide his average expenditures.

我们看到的问题是机器人等待用户发送他的用户名和密码 10 秒 要么浪费时间(或)没有足够的时间.我怎么能编程,让机器人等待用户消息,然后执行下一行(等到触发器)

The problem as we see is the bot waits for the user to send his username and password for 10 sec either waste of time (or) not sufficient time was given. How could I program such that the bot waits for user message and then executes the next lines ( wait till the trigger )

def highest(intent,chatid,text):
    seq=["What is your Username ?","Password?"]
    send_message(seq[0],chatid)
    time.sleep(6)
    name,chatid = reply_function()
    print name
    send_message(seq[1],chatid)
    time.sleep(6)
    pw,chatid = reply_function()
    print pw
    try:
        flag = obj.validate(name,pw)
        if flag=="Verified":
            for i in obj.avg_transactions():
                send_message(i,chatid)
        else:
            send_message("try again",chatid)
            highest(intent,chatid,text)
     except:
        send_message("try again",chatid)
        highest(intent,chatid,text)

推荐答案

你应该使用 ForceReply 标记您的请求并检查来自用户的回复 - 当回复在收到的 Message 然后你应该发送密码等请求.

You should use ForceReply markup with your requests and check replies from user - when reply contains Username in reply_to_message field of the received Message then you should send a request for password etc.

示例(伪代码):

// Asking user for username/password
Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
Bot.SendTextMessage(update.Message.Chat.Id, "Type your username, please");

// Checking incoming messages for replies
if (update.Message.ReplyToMessage.Text.Contains("your username"))
{
    if (!IsValidUsername(update.Message.ReplyToMessage.Text)) return;
    SaveUsernameToDb(update.Message.Chat.Id, update.Message.ReplyToMessage.Text);
    Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
    Bot.SendTextMessage(update.Message.Chat.Id, "Username has been successfully saved!");
}
else
{
    ...
}

<小时>

顺便说一句,在纯文本聊天中询问用户名/密码等私人数据是非常不安全的,也是非常糟糕的做法.


By the way, asking for private data such as username/password in plain-text-chat is quiet unsafe and very bad practice.

这篇关于电报机器人等待用户回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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