【发布时间】: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 文件中。