Search code examples
pythontwiliodialogflow-eschatbot

I only get one whatsapp answer via dialogflow instead of multiple replies


How do I get multiple 'text answers' in whatsApp? When I add more than one 'text response' in intents they work normally in the dialogFlow console.

But when I repeat the same question on whatsapp I get only one answer box instead of 3, for example, that I had created.

I am using twilio to communicate with the whatsapp API. I also use Horoku cloud services to host the application.

Everything works normal. But I received only one message box instead of multiple in whatsapp. I think the problem is my python code 'app.py'.

app.py

@app.route("/") #just to test Heroku cloud services
def hello():
    return "Hello, World!"

@app.route("/sms", methods=['POST'])
def sms_reply():
    """Respond to incoming calls with a simple text message."""
    # Fetch the message
    msg = request.form.get('Body')
    phone_no = request.form.get('From')
    reply = fetch_reply(msg, phone_no)

    # Create reply
    resp = MessagingResponse()
    resp.message(reply)
    enter code here
    return str(resp)

utils.py

import dialogflow_v2 as dialogflow
dialogflow_session_client = dialogflow.SessionsClient()
PROJECT_ID = "weather-husgcf"

def detect_intent_from_text(text, session_id, language_code='pt-BR'):
    session = dialogflow_session_client.session_path(PROJECT_ID, session_id)
    text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
    query_input = dialogflow.types.QueryInput(text=text_input)
    response = dialogflow_session_client.detect_intent(session=session, query_input=query_input)
    return response.query_result

def fetch_reply(query, session_id):
    response = detect_intent_from_text(query, session_id)
    return response.fulfillment_text

https://i.imgur.com/a/b2QSYUB "ScreenShots"


Solution

  • Old-ish question but here's the answer (Dialogflow v2).

    Assuming you have some sort of sendMessage(mobile_num, text) function, you iterate over the fulfillment_messages like this:

    for message in response.query_result.fulfillment_messages:
        sendMessage(mobile_num, message.text.text[0])
    

    From the webhook you get a json like this:

    {
      "queryText": string,
      "languageCode": string,
      "speechRecognitionConfidence": number,
      "action": string,
      "parameters": {
        object
      },
      "allRequiredParamsPresent": boolean,
      "cancelsSlotFilling": boolean,
      "fulfillmentText": string,
      "fulfillmentMessages": [
            {
                "text": {
                    "text": [
                        "Some text"
                    ]
                }
            },
            {
                "text": {
                    "text": [
                        "Some more text"
                    ]
                }
            },
      ],
      "webhookSource": string,
      "webhookPayload": {
        object
      },
      "outputContexts": [
        {
          object (Context)
        }
      ],
      "intent": {
        object (Intent)
      },
      "intentDetectionConfidence": number,
      "diagnosticInfo": {
        object
      },
      "sentimentAnalysisResult": {
        object (SentimentAnalysisResult)
      }
    }
    

    where fulfillmentMessages is an array you have to iterate over.

    Hope this helps.