首页 > 解决方案 > Twilio 错误 90100:无效的自动驾驶操作 JSON

问题描述

我参考了Mwangi Kabiru发布的Twilio 博客

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

我对代码进行了必要的更改,以从 Google Drive 中提取 Google Sheets 数据并通过 webhook 将其发送到 Twilio 聊天机器人(自动驾驶仪)。根据 Google Cloud Function 日志,webhook 根据其请求成功地将信息传输到 Twilio 聊天机器人(自动驾驶仪)。但是,Twilio 正在抛出“错误 - 90100”:

无效的自动驾驶操作 JSON:无效的自动驾驶操作可能的原因操作 JSON 不符合操作架构 ( https://carnelian-neanderthal-8008.twil.io/assets/ActionsSchema.json )

可能的解决方案 根据 Actions Schema ( https://carnelian-neanderthal-8008.twil.io/assets/ActionsSchema.json )测试您的 JSON 响应

如何下载在 Google Cloud Function 上创建的 webhook 的 JSON 文件并针对 Twilio Actions Schema 进行测试?有人可以帮我解决这个问题吗?

谷歌云功能代码:

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)
    

谷歌云函数日志

Twilio 错误日志

Twilio 的任务选项 1 请求

标签: jsongoogle-cloud-functionstwiliochatbottwilio-api

解决方案


当您通过 Twilio Autopilot 调用 URL 时,redirect您需要为 Twilio Autopilot 而不是 TwiML 返回 JSON。

您需要更改构建返回消息的部分:

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)

在此处返回 JSON:

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

有关 Twilio Autopilot 支持的操作列表,请参阅“操作概述”。


推荐阅读