Search code examples
jsongoogle-cloud-functionstwiliochatbottwilio-api

Twilio error-90100 : Invalid Autopilot Actions JSON


I referred to the Twilio blog published by Mwangi Kabiru:

https://www.twilio.com/blog/serverless-whatsapp-chatbot-python-google-cloud-functions-twilio

I made the necessary changes to the code to extract Google Sheets data from Google Drive and send it to Twilio chatbot(autopilot) via webhook. According to Google Cloud Function logs, the webhook successfully transmitted the information to the Twilio chatbot(autopilot) based on its request. But, Twilio is throwing 'error - 90100':

Invalid Autopilot Actions JSON: Invalid Autopilot Action Possible Causes Actions JSON does not comply with the Actions Schema (https://carnelian-neanderthal-8008.twil.io/assets/ActionsSchema.json)

Possible Solutions Test your JSON response against the Actions Schema (https://carnelian-neanderthal-8008.twil.io/assets/ActionsSchema.json)

How to download the JSON file of the webhook created on Google Cloud Function and test it against the Twilio Actions Schema? Can someone please help me to resolve this issue?

Google Cloud Function code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime
import requests
from twilio.twiml.messaging_response import MessagingResponse


def whatsapp_webhook(request):
    '''
    HTTP Cloud Function.
    Parameters
    ----------
    request (flask.Request) : The request object.
        <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns
    -------
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    '''

    # google drive json dictionary
    data = {
    #json details generated by Google Cloud Platform
    }

        
    # use creds to create a client to interact with the Google Drive API
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
  
    creds = ServiceAccountCredentials.from_json_keyfile_dict(data, scope)
  
    client = gspread.authorize(creds)
  
    # Find a workbook by name and open the first sheet
    # Make sure you use the right name here.

    wb = client.open("Covid_DB")
    sheet = wb.worksheet('Oxygen')
    
    #Checking whether Google Cloud is able to access Google Sheets

    row = ['Google Sheet Accessed successfully', ':)']
    index = 9
    sheet.insert_row(row, index)

    #Sending the required info to the user

    twilio_response = MessagingResponse()
    msg = twilio_response.message()
    msg.body('1) ' + ', '.join(sheet.row_values(2)) + ' 2) ' + ', '.join(sheet.row_values(3)))

    return str(twilio_response)
    

Google Cloud Function logs

Twilio Error Log

Twilio's-Task-Option-1-Request


Solution

  • When you're calling a URL from Twilio Autopilot via redirect you need to return a JSON for Twilio Autopilot not TwiML.

    You need to change the part where you're constructing your return message:

    twilio_response = MessagingResponse()
    msg = twilio_response.message()
    msg.body('1) ' + ', '.join(sheet.row_values(2)) + ' 2) ' + ', '.join(sheet.row_values(3)))
    return str(twilio_response)
    

    Return JSON instead here:

    from flask import jsonify
    body = {
        'actions': [
            {
                'say': '1) ' + ', '.join(sheet.row_values(2)) + ' 2) ' + ', '.join(sheet.row_values(3))
            },
            {
                'listen': True
            }
        ]
    }
    return jsonify(body)
    

    For a list of actions Twilio Autopilot supports see "Actions Overview".