如何从松弛用户的输入继续进行?

时间:2017-12-19 00:55:18

标签: python slack slack-api

我在聊天机器人上工作,机器人询问用户名,然后机器人用Greeting +名称回复。当我在带有input()的终端上使用它时,这是有效的,但是无法弄清楚如何接受来自slack的输入并使用该输入。

def start(request, channel):
    response = ('\n\nHello!')
    send_response(response, channel)
    name = ('Please tell me your name.\n')
    send_response(name, channel)    
    name = request
    greet = "Hello" + name
    send_response(greet, channel)

def send_response(response,channel):
    slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=True)

def parse_slack_output(slack_rtm_output):
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                # return text after the @ mention, whitespace removed
                return output['text'].split(AT_BOT)[1].strip(), \
                    output['channel']
    return None, None

if __name__ == "__main__":

    READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
    if slack_client.rtm_connect():
        print ("connected and running!")
        while True:
            request, channel = parse_slack_output(slack_client.rtm_read())
            if request and channel:
                start(request, channel)
            time.sleep(READ_WEBSOCKET_DELAY)
    else:
        print("Connection failed. Invalid Slack token or bot ID?")

1 个答案:

答案 0 :(得分:1)

根据slack doc,dialog.open()方法是实现您的要求的方法。 https://api.slack.com/dialogs

编辑: RASA NLU-CORE为基于会话的ChatBot提供了更多选项。 http://rasa.com/docs/core/quickstart/

您需要查看插槽填充以存储名称或任何其他值,并在对话中进一步使用它。