跟踪聊天机器人的列表分支“问题”序列

时间:2019-12-14 22:20:37

标签: python twilio chatbot

我正在构建一个简单的聊天机器人,对于保险公司来说,它主要有3个步骤(在step_lists中),但是它也应该理解一些关键词池(如果用户说给我您的电话,那么聊天机器人会发送电话号码并返回到上一个“分支”,)。问题是,这仅适用于一个简单的答案问题,我想添加一个“分支”(由诸如紧急事件或询问付款方式之类的关键字触发的一组问题),其中包含两个以上的问题。

在要检查的代码中(processQuestions()),如果用户输入意图触发“分支”列表问题,则PROX变量将为False并转到另一个分支,如果该分支问题不再存在问题(并且只有在分支没有选择结束对话的情况下),它将返回True并转到上一个分支(它可以是主列表或另一个分支)

我同时使用了模拟列表,然后我们将使用NLP处理器(例如对话框或watson)

from twilio.twiml.messaging_response import MessagingResponse
from flask import request

class Bot():

    def __init__(self):
        self.step_list = {
            'setupInsurance': self.setup_insurance_questions,
            'registerUser': self.register_user_questions,
            'askPayment': self.ask_payment_questions
        }
        self.intent_branch = {
            'emergencia': self.emergency_flow_questions,
            'cementerio': self.cementerio,
            'telefone': self.telefone
        }


        self.currentStep = self.steps[0]
        self.currentList = self.step_list[self.currentStep]



    prox = True
    conversation = []

    steps = [
            'setupInsurance',
            'registerUser',
            'askPayment',
    ]


    emergency_flow_questions = [
            'Ok voce pode me mandar o teu cpf',
            'Onde voce esta localizado',
            'Ok, a linha de telefone e 0800 9090 453',

    ]
    #cementerios
    cementerio = [
            'cementary'
    ]

    telefone = [
            'phone XXXX XXXXX'
    ]



    register_user_questions= [
            'Qual e o teu CPF',
            'Qual e o teu endereco?',
            'E o teu email?',
            'E o teu numero de telefone?',
        ]

    ask_payment_questions = [
            'Que forma de pagamento voce desejaria?',
        ]

    setup_insurance_questions= [
            'O seguro incluira os teus pais?',
            'E os sogros?',
            'Quantos dependentes tem entre 81 e 85',
            'E Quantos dependentes entre 86 e 90?'
        ]



    currentStep = None

    def getQuestion(self, list):
        if list: 
            pergunta = list[0]
            del list[0]
            return pergunta
        return "Muito Obrigado"

    def processQuestions(self):
       #print(self.prox)
       msg = request.form.get('Body')
       intent = self.HasIntentBranchKey(msg)
       print(intent)
       if intent:
           iter_items = len(intent)
           self.prox = False

       if self.prox == True: 
            list = self.updateCurrentList(self.prox)
            message = self.getQuestion(list)
            print(self.prox)
            #keep track of the user's questions path 
            self.conversation.append(message)
            print(self.conversation)
            print(message)      
            return message
       else: 
            self.prox = False
            # intent = self.getCurrentList(self.prox)
            # print(self.currentList)
            message = self.getQuestion(intent)
            print(self.conversation)
            print(message)
            #  )
            if message == None:
                self.prox == True
                self.conversation.append(message) 
                #it will return to the last branch in the question where it stopped
                last_item = self.conversation[-iter_items]
                return last_item
            return message



    def getCurrentStep(self):
        step = self.currentStep
        list = self.currentList
        if len(list) == 0:
            try:
                del self.steps[0]
                self.currentStep = step = self.steps[0]
            except:
                return None
#this function is used to determine the steps on the "main flow"
    def updateCurrentList(self, prox):
            self.getCurrentStep()
            try:
                self.currentList = self.step_list[self.currentStep]
                return self.currentList
            except:
                return []


    def HasIntentBranchKey(self, msg):
        msgLst = msg.split()
        keyLst = list(self.intent_branch.keys())
        for wordIntent in msgLst:
            wordLwr = wordIntent.lower()
            if wordLwr in keyLst:
                listIntentBranch = self.intent_branch.get(wordLwr)
                return listIntentBranch
            else:                
                return None

0 个答案:

没有答案