Skip to main content
 首页 » 编程设计

python之如何从松弛的用户那里获取输入以进一步进行

2025年01月19日852php

我正在开发聊天机器人,其中机器人询问用户姓名,然后机器人用问候语 + 姓名回复。当我在带有 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?") 

请您参考如下方法:

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

编辑: RASA NLU-CORE 为基于对话的聊天机器人提供了更多选择。 http://rasa.com/docs/core/quickstart/

插槽填充是您需要研究的内容,以存储名称或任何其他值并在对话中进一步使用它。