Python:从Chatbot另一个函数的结果调用函数

时间:2018-12-31 08:25:57

标签: python python-3.x function

我正在用pymessenger和wit.at创建一个聊天机器人。我正在通过一个实体(假设问候语)来获取价值,并且正在为另一个函数设置参数,但是在调用另一个函数时却无法正常工作。

def get_message():
    sample_responses = ["Hey Welcome May I know your Roll Number?", "Hi Welcome to  May I know your Roll Number"]
    return random.choice(sample_responses)

def cust(value):
    if value =="111":
        t1 = "Thor"
        val_off = "1"
    elif value =="222":
        t1 = "Steve"
        val_off = "2"
    elif value =="333":
        t1 = "Tony"
        val_off = "3"
    else:
        t1= ""
        val_off =""

def get_offer(val_off):
    if val_off == "1":
        offer_test = ("1. Thor"+"\n"+"2. Thor: Ragnarok+"\n"+"3. Avenger Series")
    elif val_off == "2":
        offer_test = ("1. First Avenger"+"\n"+"2. Winter Soldier"+"\n"+"3. Civil War")
    elif val_off =="3":
        offer_test = ("1. Iron Man 1"+"\n"+"2. Iron Man 2"+"\n"+"3. Civil War")
    return offer_test


app.route('/', methods=['POST'])
def webhook():
    data = request.get_json()
    log(data)
    if data['object'] == 'page':
        for entry in data['entry']:
            for messaging_event in entry['messaging']:

                sender_id = messaging_event['sender']['id']
                recipient_id = messaging_event['recipient']['id']

                if messaging_event.get('message'):
                    if 'text' in messaging_event['message']:
                        messaging_text = messaging_event['message']['text']
                    else:
                        messaging_text = 'no text'

                    response = None

                    entity, value = wit_response(messaging_text)


                    if entity == 'cust_greet':
                        response = get_message()
                    elif entity == 'cust_id':
                        test = cust(str(value))
                        val_off = test[1]
                        offer_response = get_offer(str(val_off))
                        esponse = "Hey "+test[0] +". "+"\n"+"How Can I help you? Are you interested in"+"\n"+"1.Bio"+"\n"+"2. Movie list"
                    elif entity == 'cust_movie':
                        response = offer_response

                    bot.send_text_message(sender_id, response)

提供卷号时:例如。将调用111函数cust(),它将告诉您名称为Thor并设置val_off = 1并调用函数call_offer。但是,当我选择选项“ 2.电影列表”时,我得到了错误:

  

“响应= offer_response   UnboundLocalError:分配前已引用本地变量“ offer_response””

1 个答案:

答案 0 :(得分:1)

示例功能:

def cust(value):
offer_response ="" ## if it is a string
offer_response = 0 ## if it is a number
    if value =="111":
        t1 = "Thor"
        val_off = "1"
        offer_response = get_offer(str(val_off)) 

尝试一下:

elif entity == 'cust_movie':
    response = "Please let me know the type of movie by pressing only the digit"+"\n"+"1.Thor Series"+"\n"+"2. Avenger Series" +"\n"+"3.. Iron Man Series"
elif entity in( '1' , '2' ,'3'):
    response = get_offer(entity)        

因此,当您键入“ cust_movie”时,机器人会要求您按要观看的电影类型。它将存储值1,2,3

根据用户输入的1,2或3,机器人将检查另一个elif循环并转到get_offer函数,随后的电影列表将显示