json - 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 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 支持的操作列表,请参阅“操作概述”。
推荐阅读
- windows - 在 VSCode (Windows) 中调试时出错
- java - 构建 org.hibernate.hql.internal.ast.QuerySyntaxException 时出错:table_name 未映射
- python - 声明一个未知数量列表的空数组,然后访问它的项目?
- .net - PowerShell 获取文件 MD5 作为 base64 字符串
- c - /proc/{pid}/stack 在 linux 中是如何工作的?
- python - 如何修复 python 3.8 中的 pyttsx3 模块错误
- api - 如何结合 url 和 string 进行 api 调用
- weaviate - 对字典列表的 CRUD 支持
- r - 交互图的平均线
- gojs - 如何在拖动链接时将装饰或工具提示添加到 (Re-)LinkingTool 的临时ToPort 对象