0

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.

KGhatak
  • 6,995
  • 1
  • 27
  • 24
  • Can you include in your question how you are handling things in the webhook now? We can easily give you general information, but unless we see the webhook code you're currently using, it makes it very difficult to give you specific help. – Prisoner Jan 28 '19 at 21:40
  • Edited my original query to add my current code - which is really a temporary solution involving 'lifespan' param of Dialogflow. – KGhatak Jan 29 '19 at 03:55
  • How are you sending the reply itself? It looks like this determines what the reply is - but not how you're sending it. (Are you sending JSON? Using a library?) – Prisoner Jan 29 '19 at 08:56
  • i am confused by the question title, description and the code. what exactly do you want to achieve? do you want to handle question id's or you want to modify params or something else? – sid8491 Jan 29 '19 at 09:17
  • sid8491 - I want to modify the value of a param inside a context. I added the sample code (see "outputContexts" and "hint_index" for easy reference) in the original question. I want to set "hint_index" to say 1 or 5 – KGhatak Jan 29 '19 at 10:35
  • Prisoner - I've added the main webhook code along with all imports. Let me know if any more details you'd like to know. – KGhatak Jan 29 '19 at 10:44
  • Python sample is available here: https://stackoverflow.com/questions/54197528/set-parameters-in-eventinput-in-dialogflow-v2-api – TVK Jan 29 '19 at 14:19

0 Answers0