Twilio error-90100:无效的自动驾驶操作 JSON

Twilio error-90100 : Invalid Autopilot Actions JSON

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

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

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

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

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

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

Google云函数代码:

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

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

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

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 改为:

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 支持的操作列表,请参阅“Actions Overview”。