警告 - 12200 模式验证警告提供的 XML 不符合 Twilio 标记 XML 模式

Warning - 12200 Schema validation warning The provided XML does not conform to the Twilio Markup XML schema

我正在按照本教程设置 Twilio-Dialogflow WhatsApp 聊天机器人: https://www.youtube.com/watch?v=r5EMHIQiGWE

代码如下:

from http.client import responses
from flask import Flask, request, jsonify
import os, dialogflow
from flask.globals import session
from google.api_core.exceptions import InvalidArgument
import requests

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'private_key.json'
DIALOGFLOW_PROJECT_ID = 'whatsapp-twilio-flask-dia-svau'
DIALOGFLOW_LANGUAGE_CODE = 'en'
SESSION_ID = 'me'

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/')
def root():
    return "Hello World"

@app.route('/api/getMessage', methods=['POST'])
def home():
    message = request.form.get('Body')
    mobnum = request.form.get('From')
    session_client = dialogflow.SessionsClient()
    session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
    text_input = dialogflow.types.TextInput(text=message, language_code = DIALOGFLOW_LANGUAGE_CODE)
    query_input = dialogflow.types.QueryInput(text=text_input)
    try:
        response = session_client.detect_intent(session=session, query_input=query_input)
    except InvalidArgument:
        raise
    sendMessage(mobnum,response.query_result.fulfillment_text)
    return response.query_result.fulfillment_text

def sendMessage(mobnum,message):
    url = "https://api.twilio.com/2010-04-01/Accounts/XXXXXXXXXX/Messages.json"
    payload = {'From': 'whatsapp:+14155238886',  'Body': message,   'To': mobnum}
    headers = {'Authorization': 'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX'  }
    response = requests.request("POST", url, headers = headers, data=payload)
    print(response.text.encode('utf8'))
    return ""

if __name__ == '__main__':
    app.run()

运行 代码在 Twilio 中给出了这个错误,因此我没有在 WhatsApp 中收到回复

Warning - 12200
Schema validation warning
The provided XML does not conform to the Twilio Markup XML schema. 

response.text returns 这个:

print(response.text.encode('utf8')) :

b'{"sid": "XXX", "date_created": "XXX", "date_updated": "XXX", "date_sent": null, "account_sid": "XXX", "to": "whatsapp:+XXX", "from": "whatsapp:+XXX", "messaging_service_sid": null, "body": "Good day! What can I do for you today?", "status": "queued", "num_segments": "1", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": null, "error_code": null, "error_message": null, "uri": "XXX", "subresource_uris": {"media": "XXX"}}'

有人可以帮忙吗?我是否因为没有使用 twilio.twiml 库而从 Twilio 收到上述错误?但是从教程视频中,我没有看到这个库被使用。

这里是 Twilio 开发人员布道者。

您(和视频教程)设置响应的方式不太正确。您将 WhatsApp 沙箱网络钩子指向您的 /api/getMessage 端点,这是正确的,但您响应该请求的方式不适用于 Twilio。

当 Twilio 向您的应用程序发出 webhook 请求时,它期望响应是 XML 响应,其中包含 TwiML。您甚至可以使用 TwiML 来响应消息,因此您无需向 REST API 发出请求来发送消息。

为了让事情变得更简单,您可以做的另一件事是 install and use the Twilio Python helper library。您可以使用它向 API 发出请求,也可以构建 TwiML 响应。

因此,我建议您安装 Twilio Python 库:

pip install twilio

然后在您的应用中使用该库来构建 return TwiML 响应,如下所示:

from http.client import responses
from flask import Flask, request, jsonify, Response
import os, dialogflow
from flask.globals import session
from google.api_core.exceptions import InvalidArgument
from twilio.twiml.messaging_response import MessagingResponse
import requests

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'private_key.json'
DIALOGFLOW_PROJECT_ID = 'whatsapp-twilio-flask-dia-svau'
DIALOGFLOW_LANGUAGE_CODE = 'en'
SESSION_ID = 'me'

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/')
def root():
    return "Hello World"

@app.route('/api/getMessage', methods=['POST'])
def home():
    message = request.form.get('Body')
    mobnum = request.form.get('From')
    session_client = dialogflow.SessionsClient()
    session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
    text_input = dialogflow.types.TextInput(text=message, language_code = DIALOGFLOW_LANGUAGE_CODE)
    query_input = dialogflow.types.QueryInput(text=text_input)
    try:
        intent = session_client.detect_intent(session=session, query_input=query_input)
    except InvalidArgument:
        raise
    response = MessagingResponse()
    response.message(intent.query_result.fulfillment_text)
    return Response(str(response), mimetype="application/xml")

if __name__ == '__main__':
    app.run()

在这段代码中,我们从 twilio.twiml.messaging_response 导入 MessagingResponse 并创建一个我们 return 作为 XML 的响应,而不是调用原始 sendMessage 函数, 使用 Response 对象设置内容和 mimetype。

Response(str(response), mimetype="application/xml")

这既可以简化您的代码,也可以消除模式验证警告。查看 building with Twilio and Flask 上的博客 post 了解更多信息。