我正在尝试使用Slack API构建聊天机器人,但我的机器人似乎一次又一次地回答相同的问题。该代码似乎卡在同一位置。请帮忙 以下是代码:
import slack_utility
import time
from HeuristicParser.HeuristicParser import kwdRB
import pandas as pd
from itertools import izip
tagger = kwdRB.HCTagger()
"""citydf = pd.read_csv("city_dump.csv")
locdf = pd.read_csv("locality_dump.csv")"""
def chat(response=None, stage=0):
if stage == 0:
return stage0(stage)
if stage == 1:
return stage1(stage, response)
if stage == 2:
return stage2(stage, response)
if stage == 3:
return stage3(stage, response)
if stage == -2:
return stage4(stage, response)
if stage == -1:
return stage5(stage, response)
def stage0(stage):
return ("please select one of the following options :", stage)
def stage1(stage, response):
if response['choice'] == 'doc':
return ("please enter the speciality or ailment you are suffering from :", stage, response)
elif response['choice'] == 'lab':
return ("please enter the name of the lab test :", stage, response)
else:
stage=-2
return stage4(stage, response)
def get_entity(response, tagger=tagger):
text = response['entity']
tag = tagger.tag(text)[0][0]
# print tag[0][1]
if tag:
tag_ = tag[0][1][0]
type = tag[0][1][1]
response['tag'] = tag_
response['type'] = type
return response
else:
return None
def stage2(stage, response):
res = get_entity(response)
if res:
response = res
return ("Please select the city in which you would want to book your appointment :", stage, response)
else:
stage1(stage, response)
def stage3(stage, response):
return ("Please select your locality in which or nearby you want to book your appointment :", stage, response)
def stage4(stage, response):
return ("please enter your phone number where we can reach you :", stage, response)
def stage5(stage, response):
return ("please enter your name :", stage, response)
def main():
"""
Initiate the bot and call appropriate handler functions
"""
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
slack_api = slack_utility.connect()
if slack_api.rtm_connect():
print 'SLACK_BOT connected and running'
while True:
stage = 0
command, channel = slack_utility.parse_slack_response(slack_api.rtm_read())
if command and channel:
fn_call = chat(response=None, stage=stage)
slack_api.rtm_send_message(channel,fn_call[0])
choice,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response = {'choice': choice}
stage = stage + 1
fn_call = chat(response=response, stage=stage)
if fn_call[1] != -2:
slack_api.rtm_send_message(channel,fn_call[0])
entity,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['entity'] = entity
stage = stage + 1
fn_call = chat(response=response, stage=stage)
slack_api.rtm_send_message(channel,fn_call[0])
city,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['city'] = city
stage = stage + 1
fn_call = chat(response=response, stage=stage)
slack_api.rtm_send_message(channel,fn_call[0])
loc,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['loc'] = loc
stage = -2
fn_call = chat(response=response, stage=stage)
slack_api.rtm_send_message(channel,fn_call[0])
num,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['number'] = num
stage = stage + 1
fn_call = chat(response=response, stage=stage)
slack_api.rtm_send_message(channel,fn_call[0])
name,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['name'] = name
stage = stage + 1
slack_api.rtm_send_message(channel,"Thanks {}, you are being directed to our appointment page.".format(name))
slack_api.rtm_send_message(channel,response)
else:
fn_call = chat(response=response, stage=fn_call[1])
stage = fn_call[1]
slack_api.rtm_send_message(channel,fn_call[0])
num,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['number'] = num
stage = stage + 1
fn_call = chat(response=response, stage=stage)
slack_api.rtm_send_message(channel,fn_call[0])
name,channel = slack_utility.parse_slack_response(slack_api.rtm_read())
response['name'] = name
stage = stage + 1
slack_api.rtm_send_message(channel,"Thanks {}, our customer representative will get back to you shortly.".format(name))
slack_api.rtm_send_message(channel,response)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print 'Connection failed. Invalid Slack token or bot ID?'
if __name__ == '__main__':
main()
它总是答复: 请选择以下选项之一: 如何克服这个问题,我已经定义了机器人应逐步给出的不同答案的阶段。