{"id":2704,"date":"2023-09-06T18:21:40","date_gmt":"2023-09-06T10:21:40","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2704"},"modified":"2023-09-06T18:21:40","modified_gmt":"2023-09-06T10:21:40","slug":"2704","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2704.html","title":{"rendered":"twilio python\u81ea\u52a8\u62e8\u6253\u7535\u8bdd,\u64ad\u653e\u81ea\u5b9a\u4e49mp3\u97f3\u9891\u7684\u65b9\u6cd5"},"content":{"rendered":"

\u6709\u4e2a\u5c0f\u9879\u76ee\uff0c\u9700\u6c42\u662f\u67d0\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u7ed9\u5ba2\u6237\u6253\u7535\u8bdd\uff0c\u63d0\u9192\u5ba2\u6237\u3002\u9700\u8981\u4e8b\u5148\u5f55\u5236\u597d\u4e00\u6bb5\u97f3\u9891\uff0c\u5ba2\u6237\u63a5\u901a\u7535\u8bdd\u540e\uff0c\u81ea\u52a8\u64ad\u653e\u3002<\/p>\n

\u8fd9\u91cc\u7528\u5230\u4e86twilio\uff1ahttps:\/\/www.twilio.com<\/a> \uff0c\u5206\u4eab\u4e0b\u3002<\/p>\n

1. \u6ce8\u518ctwilio\u8d26\u53f7<\/strong><\/p>\n

2. \u4e70\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801<\/strong><\/p>\n

\"\u81ea\u52a8\u8349\u7a3f\"<\/p>\n

3. \u5b89\u88c5twilio<\/strong><\/p>\n

pip install twilio<\/p>\n

4. trigger\u4ee3\u7801\uff08\u7ed9\u67d0\u4e2a\u7535\u8bdd\u53d1\u9001\u77ed\u4fe1\uff0c\u6216\u8005\u6253\u7535\u8bdd\uff09\uff1a<\/strong><\/p>\n

from twilio.rest import Client\r\n# Your Account Sid and Auth Token from twilio.com\/console\r\naccount_sid = 'XXX'\r\nauth_token = 'XXX'\r\n \r\n \r\ndef sendCall(mobile):\r\n client = Client(account_sid, auth_token)\r\n url='\u4f60\u9700\u8981\u81ea\u5df1\u5199\u4e00\u4e2aurl, \u540e\u9762\u6211\u4f1a\u5206\u4eab\u81ea\u5df1\u7684django\u4ee3\u7801'\r\n \r\n call = client.calls.create(\r\n  url=url,\r\n  to='+86{}'.format(mobile),\r\n  from_='\u4f60\u4e70\u7684\u7535\u8bdd\u53f7\u7801'\r\n )\r\n \r\n \r\ndef sendSMS(phone,content):\r\n client = Client(account_sid, auth_token)\r\n message = client.messages \\\r\n  .create(\r\n  body=content,\r\n  from_='\u4f60\u4e70\u7684\u7535\u8bdd\u53f7\u7801',\r\n  to='+86{}'.format(phone)\r\n )\r\n \r\n \r\nif __name__ == '__main__':\r\n sendCall("186xxxxxx")<\/pre>\n

5. \u5728django\u9879\u76ee\u4e2d\u51c6\u5907\u4e00\u4e2a\u8def\u7531\uff0c\u5904\u7406twilio\u53d1\u6765\u7684post\u8bf7\u6c42\uff0c\u8fd4\u56de\u4e00\u6bb5xml<\/strong><\/p>\n

url.py:<\/p>\n

 url(r'^notify\/call\/$',csrf_exempt(NotifyCall.as_view()), name='call'),\r\n view.py:\r\n\r\nclass NotifyCall(APIView):\r\n def post(self, request):\r\n  temp = """<?xml version="1.0" encoding="utf-8"?>\r\n  <Response>\r\n   <Play>\u4e00\u4e2a\u516c\u7f51\u53ef\u4ee5\u8bbf\u95ee\u7684mp3\u6587\u4ef6\u8def\u5f84,\u6bd4\u5982\u6211\u7528\u7684\u81ea\u5df1\u7684\u817e\u8baf\u4e91\u670d\u52a1\u5668\u4e0a\u7684\u67d0\u4e2a\u6587\u4ef6<\/Play>\r\n   <Hangup\/>\r\n  <\/Response>\r\n  """\r\n  return HttpResponse(temp)<\/pre>\n

6. \u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u5c31\u80fd\u5b8c\u6210\u6253\u7535\u8bdd\u4e86\u3002\u4f46\u662f\u6709\u4e2a\u95ee\u9898\uff0c\u5f53\u6211\u63a5\u8d77\u6765\u7535\u8bdd\u540e\uff0c\u6709\u5dee\u4e0d\u591a4~5\u79d2\u7684mute call\u65f6\u95f4\u3002<\/strong><\/p>\n

\u5206\u6790log\u53ef\u4ee5\u770b\u5230\uff1a<\/p>\n

\"\u81ea\u52a8\u8349\u7a3f\"<\/p>\n

\u770b\u6765\u662f\u56e0\u4e3atwilio\u670d\u52a1\u5668\u5728\u56fd\u5916\uff0c\u4ece\u817e\u8baf\u4e91\u83b7\u53d6\u6587\u4ef6\u82b1\u8d39\u65f6\u95f4\u592a\u957f\u3002<\/p>\n

\u4e8e\u662f\uff1a<\/p>\n

7. \u4e0a\u4f20mp3\u5230\u56fd\u5916\u67d0\u4e2a\u516c\u5171\u7f51\u7ad9\uff0c\u8ba9twilio\u4ece\u90a3\u91cc\u63d0\u53d6\u6587\u4ef6<\/strong><\/p>\n

\"\u81ea\u52a8\u8349\u7a3f\"<\/p>\n

1\u79d2\u5185\u5c31\u83b7\u53d6\u5230\u4e86\uff1a<\/p>\n

\"\u81ea\u52a8\u8349\u7a3f\"<\/p>\n

\u4ee5\u4e0a\u8fd9\u7bc7twilio python\u81ea\u52a8\u62e8\u6253\u7535\u8bdd,\u64ad\u653e\u81ea\u5b9a\u4e49mp3\u97f3\u9891\u7684\u65b9\u6cd5\u5c31\u662f\u5c0f\u7f16\u5206\u4eab\u7ed9\u5927\u5bb6\u7684\u5168\u90e8\u5185\u5bb9\u4e86<\/p>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6709\u4e2a\u5c0f\u9879\u76ee\uff0c\u9700\u6c42\u662f\u67d0\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u7ed9\u5ba2\u6237\u6253\u7535\u8bdd\uff0c\u63d0\u9192\u5ba2\u6237\u3002\u9700\u8981\u4e8b\u5148\u5f55\u5236\u597d\u4e00\u6bb5\u97f3\u9891\uff0c\u5ba2\u6237\u63a5\u901a\u7535\u8bdd\u540e\uff0c\u81ea\u52a8\u64ad\u653e\u3002 \u8fd9 […]<\/p>\n","protected":false},"author":1,"featured_media":2709,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2704"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2704"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2704\/revisions"}],"predecessor-version":[{"id":2713,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2704\/revisions\/2713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media\/2709"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}