【问题标题】:Schema validation warning from forwarding SMS with Twilio/Flask使用 Twilio/Flask 转发 SMS 的架构验证警告
【发布时间】:2016-11-17 03:46:28
【问题描述】:

我的代码接收传入的 SMS 文本消息,提取正文,然后将正文转发到另一个号码。 (tPhone 是 Twilio 号码,ePhone 是我希望将消息转发到的号码)

import twilio.twiml
from flask import Flask, request, redirect
from twilio.rest import TwilioRestClient
from passwords import *

client = TwilioRestClient(account_sid, auth_token)

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def AlertService():
    TheMessage=request.form.get("Body")
    if (TheMessage != None):
        print(TheMessage)
        client.messages.create(to=ePhone, from_=tPhone, body=TheMessage)
    return str(TheMessage)

if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0")

代码有效(消息被转发)但 Twilio 调试器告诉我

prolog 中不允许有内容。

警告 - 12200

架构验证警告

提供的 XML 不符合 Twilio Markup XML 架构。

如何修复发送到 Twilio 的 XML?


编辑:我发现了一些东西。即使我将“TheMessage”设置为预定义的字符串(例如TheMessage="hello"),我也会从 Twilio 收到相同的错误。

此外,如果我尝试生成并发送 XML,我仍然会收到同样的错误。

    resp = twiml.Response()
    XML = resp.message(TheMessage)
    print(XML)
    client.messages.create(body=str(XML),to=ePhone,from_=tPhone)

如果我尝试body=XML,代码发送失败,如果我尝试body=str(XML),它只会以纯文本形式发送XML。

【问题讨论】:

  • 确保您的电话号码是作为字符串而不是整数传递的。
  • @JakeConway 它们以字符串形式存储在我的 passwords.py 文件中。

标签: python sms twilio


【解决方案1】:

这里是 Twilio 开发者宣传员。

目前看起来您正在使用REST API to forward the SMS message。虽然您可以这样做,但使用TwiML 在使用<Message> verb 的请求中更容易做到这一点。在您的第二个示例中,您似乎正在尝试同时使用 TwiML 和 REST API,但这是行不通的。

因此,您只想构建一个 TwiML 响应,如果有传入消息,则将消息添加到其中,然后使用 <Message> attributes to and from 将其转发到您的电话号码。像这样:

import twilio.twiml
from flask import Flask, request, redirect
from passwords import *

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def AlertService():
    TheMessage=request.form.get("Body")
    resp = twiml.Response()
    if (TheMessage != None):
        resp.message(TheMessage, to=ePhone)
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0")

让我知道这是否有帮助。

【讨论】:

  • 我不得不删除“from_=tPhone”(属性“from_”不允许出现在元素“消息”中。),但之后一切正常,谢谢。
  • 呸,对不起。没时间测试。将编辑我的答案。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多