How to modify a parameter's value from webhook using python - Dialogflow chatbot

时间:2019-01-28 19:59:38

标签: dialogflow webhooks chatbot dialogflow-fulfillment

I’m creating a bot for puzzle. User will be presented with a puzzle and then if they need hint, bot will provide one at a time. I decided to model this with two intents – ‘Supply_puzzle’ and a follow-up intent ‘Supply_puzzle – get_a_hint’. When user asks for puzzle, Supply_puzzle will contact a webhook and a puzzle will be provided. Subsequent request for hint will be taken care of by the follow-up intent by calling a webhook. Since multiple users may play with the same puzzle at the same time, it’s important that we keep track of the hint-index for each session. Further, if I add a param say, hint_index then each time webhook receives a req from follow-up intent, the webhook gets the last hint_index. Then the webhook can supply the next hint as well as can modify the param value so that for next call from this intent, the previously set value hint_index is returned to the webhook. Hope this provides the context for my query.

Current code: I'm using lifespan for the moment

import urllib
import json
import os
import re
from flask import Flask
from flask import request
from flask import make_response

    @app.route('/webhook',methods=['POST'])

def webhook():
    req = request.get_json(silent=True, force=True)

    identified_entity=extract_identified_entity(req)
    parent_of_followup_intent=extract_parent_of_followup_intent(req)
    ret_txt=processFollowUpIntent(parent_of_followup_intent, identified_entity, req)

    x = {
    "fulfillmentText": "ABC1", 
    "fulfillmentMessages": [{
       "text": {
          "text": [ ret_txt
     ]}}]
    }
    x = json.dumps(x, indent=4)
    r = make_response(x)
    r.headers['Content-type'] = 'application/json'
    return r

    def processFollowUpIntent(parent_of_followup_intent, identified_entity, req):
        print("inside processFollowUpIntent key [%s]" %(identified_entity))

        LIFESPAN_INITIAL_VALUE=10 #Currently I set this to 10 in 'Supply_puzzle' intent
        current_lifespan=req['queryResult']['outputContexts'][0]['lifespanCount']
        stepIndx=LIFESPAN_INITIAL_VALUE - 1 - current_lifespan

        puzzleId=getPuzzleId(req)
        return hint_map[puzzleId][stepIndx]

Looking for sample python code - any help is much appreciated. More specifically, I want to modify the 'hint_index' param value (see below) to another value and return to the bot.

"outputContexts": [{
                "name": "Supply_puzzle – followup"
                "lifespanCount": 10,
                "parameters": {
                    "hint_index": "0",
                    ...
                }}],

P.S. Instead of the followup-intent, we can use that intent in the top level and can still be chained by setting appropriate input context. However, the need for using a param and to be able to modify it from a webhook still remain.

0 个答案:

没有答案