下面的代码是针对电报机器人这基本上需要一个人username and password并验证它以提供他的平均支出。
我们看到的问题是机器人等待用户发送他的用户名和密码10 sec要么浪费时间(要么)没有给予足够的时间。我怎样才能编程,让机器人等待用户消息,然后执行下一行(等到触发)
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)
你应该使用强制回复标记您的请求并检查用户的回复 - 当回复包含时Username in reply_to_message
接收到的字段Message那么您应该发送密码等请求。
Example (伪代码):
// 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
{
...
}
顺便说一句,在纯文本聊天中询问用户名/密码等私人数据是非常不安全且非常糟糕的做法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)