Python中的Twilio跟踪系统



我正在尝试使用Twilio,这样用户就可以根据自己的物品发送带有不同跟踪代码的短信,Twilio手机号码会回复之前输入该号码的每个人的姓名和位置列表。如果物品以前没有被跟踪过(以前没有人发过短信),它会回复"这个垫子没有被跟踪"

我知道这是一个关键词应用程序,我正在尝试用Python实现它。然而,即使在查看了在线TwilioPython API和他们的其他资源后,我仍然对如何编写适当的代码感到困惑。提前感谢大家!

好吧,你最好使用Python/Django,但这里(在Python-Django中):

首先,您购买一个Twilio号码。在您购买的号码中,有几个字段,其中一个用于接收短信。你需要在那里设置一个url,当设备向该号码发送短信时,Twilio会点击该url(最初,它会转到演示链接)。将被击中的url在您的服务器上。所以,你设置了url路由,这样当Twilio点击url时就会调用它。然后,您用Python编写一些代码来读取请求变量,如下所示:

from twilio.rest import TwilioRestClient
from twilio import twiml  # Get these as pip packages from Twilio
def hello(request):  # This is the script that the url hits
    from_num = request.POST.get("From", None)  # The callers number, if known, in e164 format.
    our_num = request.POST.get("To", None)  # The twilio number called, in e164 format.
    SID = request.POST.get("MessagingServiceSid", None)  # The first part of the key 
    AccountSID = request.POST.get("AccountSid", None) # the second part of the key.
    key = "%s%s" % (SID, AccountSID)
    message_body = request.POST.get("Body", None) # Get the actual text that was sent.
    r = twiml.Response() # This allows the server to respond with the Twilio scripting language, twiml.
    ~~~~~~
    Do Parsing stuff here, and get the list to respond with as variable "body"
    ~~~~~~
    r.message(body) # This sends back your list as a text message to the sender.
    return str(r)  # Send the completed response to Twilio for forwarding.

就这么简单。变得极其复杂的是监视被阻塞的数字、捕捉错误、糟糕的utf-8文本转换等等。

链接到Twilio文档

此外,请注意,每160个字符就是一条消息,超过160个字符的消息会被分解并按每条消息收费。因此,要么将消息保持在160以下,要么准备支付额外的费用。

最新更新