【问题标题】:Telegram bot to wait for user reply电报机器人等待用户回复
【发布时间】:2017-06-19 21:02:44
【问题描述】:

下面的代码是一个Telegram Bot,它基本上需要一个人usernamepassword并验证它以提供他的平均支出。

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

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)

【问题讨论】:

    标签: python python-2.7 telegram telegram-bot python-telegram-bot


    【解决方案1】:

    您应该在您的请求中使用ForceReply 标记并检查用户的回复 - 当回复在收到的reply_to_message 字段中包含 用户名 时,您应该发送密码等请求.


    示例伪代码):

    // 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
    {
        ...
    }
    

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

    【讨论】:

      【解决方案2】:

      您可以使用对话处理程序来解决这个问题。 您需要做的就是创建一个提出问题的函数,返回下一个函数的状态并从新函数中获取回复。Update.message.text

      【讨论】:

        猜你喜欢
        • 2021-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-07
        • 1970-01-01
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        相关资源
        最近更新 更多