松弛机器人不会在循环中暂停,而是在 while true 语句处重新启动



我正在开发一个松弛机器人。我的问题是,我希望当一个人用某些文本(@bot日志(ping机器人时,它要求输入电子邮件地址,然后重复电子邮件并要求确认。

这一切都有效,也是我的麻烦开始的地方。当您回复确认时,它会再次重复您刚刚说的话,并且不会继续说"知道了您的用户名"。

if command.startswith(LOG):
    slack_client.rtm_send_message(channel, "Please input your username:")
    while True:
        for slack_message in slack_client.rtm_read():
            message = slack_message.get("text")
            user = slack_message.get("user")
            if not message or not user:
                continue
            slack_client.rtm_send_message(channel, "You Wrote :  " + message +  "n is that your correct email?")
    else:
        print("That didnt work")
            #break  ##ask for confirmation, maybe the text in this line can reference in a different way
            #while True:
        respond = slack_message.get("text")
        while respond is 'yes' or 'Yes':##would this to true already, push in regex
                continue
        slack_client.rtm_send_message(channel, "Got it, your username is :" + message)#possibly grabbing newest text and not variable of message
                #if respond != 'yes' or 'Yes':#push from regex above
                    #slack_client.rtm_send_message(channel, "sorry!")                   
        time.sleep(.1)
while True

循环中无限期执行;您必须使用breakreturn以某种方式退出它。但是breakreturn都会在while后忽略你的else,所以你必须对代码进行重大重构。像这样:

while True:
    for slack_message in slack_client.rtm_read():
        message = slack_message.get("text")
        user = slack_message.get("user")
        if not message or not user:
            continue
        slack_client.rtm_send_message(…)
        break
# and no else here
print(…)

而且我认为你根本不需要首先while True。让我以这种方式重写代码:

while True:
    command = ??? # slack_client.rtm_read() perhaps?
    if command.startswith(LOG):
        slack_client.rtm_send_message(channel, "Please input your username:")
        for slack_message in slack_client.rtm_read():
            message = slack_message.get("text")
            user = slack_message.get("user")
            if not message or not user:
                continue
            slack_client.rtm_send_message(channel, "You Wrote :  " + message +  "n is that your correct email?")
        while True:
            respond = slack_message.get("text")
            if respond is 'yes' or 'Yes':##would this to true already, push in regex
                continue
            break
        slack_client.rtm_send_message(channel, "Got it, your username is :" + message)#possibly grabbing newest text and not variable of message

最新更新