在网站上发送 Twilio SMS 后重定向到网页/但如果通过 phone 发送短信则不重定向
Redirect To WebPage after Twilio SMS sent on website / but do not redirect if sms sent through phone
我正在使用单个 twilio 号码触发通过 django 函数发送的短信。
如果有人通过我的网站访问我的应用程序,那么该应用程序将从网站的 post 请求中解析 sendphonenum
。
如果有人通过向 twilio 号码发送短信来访问我的应用程序,它将从短信中解析 sendphonenum
。
问题发生在 Twilio 向 sendphonenum
发送消息后。
如果消息是从网站触发的,它应该重定向到发送消息的用户的仪表板页面。但如果消息是通过初始短信触发的,请不要这样做。
@csrf_exempt
def sms_response(request):
if request.method == "POST":
# parse sendphonenum from text message sent from mobile phone to twilio number +14545552222
# or parse sendphonenum from a post request from website
message_body = request.POST['Body']
sendnum_ary = re.findall('[0-9]+', message_body)
sendnum = "".join(sendnum_ary)
sendphonenum = "+1" + sendnum
mytwilionum = "+14545552222"
# put your own credentials here
ACCOUNT_SID = "123434234234"
AUTH_TOKEN = "abcsdasda"
client = Client(ACCOUNT_SID, AUTH_TOKEN)
client.messages.create(
to= sendphonenum,
from_= mytwilionum,
body='someone told me to message you'
)
## there is no platform variable, just doing some mock code to show you what I'd want to happen
## note sms_response function url must be same for both website and mobile phone, as both are using same webhook A MESSAGE COMES IN for +14545552222
if platform == 'web':
return HttpResponseRedirect(reverse('dashboard'))
return HttpResponse('')
此处为 Twilio 开发人员布道师。
Twilio 将发送一堆 header 以及您可以检查的请求。例如,所有请求都已签名并包含 X-Twilio-Signature
header。所以你可以检查 request.META
字典是否存在那个 header。为了更加确定这是来自 Twilio 的请求,您可以 verify that the signature is correct.
if ‘HTTP_X_TWILIO_SIGNATURE’ in request.META:
# An XML response with an empty `<Response/>` element
resp = MessagingResponse()
return HttpResponse(str(resp))
else:
return HttpResponseRedirect(reverse('dashboard'))
如果有帮助请告诉我。
我正在使用单个 twilio 号码触发通过 django 函数发送的短信。
如果有人通过我的网站访问我的应用程序,那么该应用程序将从网站的 post 请求中解析 sendphonenum
。
如果有人通过向 twilio 号码发送短信来访问我的应用程序,它将从短信中解析 sendphonenum
。
问题发生在 Twilio 向 sendphonenum
发送消息后。
如果消息是从网站触发的,它应该重定向到发送消息的用户的仪表板页面。但如果消息是通过初始短信触发的,请不要这样做。
@csrf_exempt
def sms_response(request):
if request.method == "POST":
# parse sendphonenum from text message sent from mobile phone to twilio number +14545552222
# or parse sendphonenum from a post request from website
message_body = request.POST['Body']
sendnum_ary = re.findall('[0-9]+', message_body)
sendnum = "".join(sendnum_ary)
sendphonenum = "+1" + sendnum
mytwilionum = "+14545552222"
# put your own credentials here
ACCOUNT_SID = "123434234234"
AUTH_TOKEN = "abcsdasda"
client = Client(ACCOUNT_SID, AUTH_TOKEN)
client.messages.create(
to= sendphonenum,
from_= mytwilionum,
body='someone told me to message you'
)
## there is no platform variable, just doing some mock code to show you what I'd want to happen
## note sms_response function url must be same for both website and mobile phone, as both are using same webhook A MESSAGE COMES IN for +14545552222
if platform == 'web':
return HttpResponseRedirect(reverse('dashboard'))
return HttpResponse('')
此处为 Twilio 开发人员布道师。
Twilio 将发送一堆 header 以及您可以检查的请求。例如,所有请求都已签名并包含 X-Twilio-Signature
header。所以你可以检查 request.META
字典是否存在那个 header。为了更加确定这是来自 Twilio 的请求,您可以 verify that the signature is correct.
if ‘HTTP_X_TWILIO_SIGNATURE’ in request.META:
# An XML response with an empty `<Response/>` element
resp = MessagingResponse()
return HttpResponse(str(resp))
else:
return HttpResponseRedirect(reverse('dashboard'))
如果有帮助请告诉我。