{"id":403,"date":"2017-09-22T14:00:00","date_gmt":"2017-09-22T05:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/09\/22\/twilio_escalation_lambda"},"modified":"2017-09-22T14:00:00","modified_gmt":"2017-09-22T05:00:00","slug":"twilio_escalation_lambda","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/09\/22\/twilio_escalation_lambda\/","title":{"rendered":"AWS Lambda\u304b\u3089Twilio\u3092\u4f7f\u3063\u3066\u30a8\u30b9\u30ab\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u73fe\u3059\u308b"},"content":{"rendered":"

MMM\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u67f3\u6cbc\u3067\u3059\u3002\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002
\n\u5317\u6d77\u9053\u306f\u3082\u3046\u3060\u3044\u3076\u5bd2\u3044\u3067\u3059\u3002<\/p>\n

\u5f0a\u793e\u3067\u306f\u30b7\u30b9\u30c6\u30e0\u76e3\u8996\u7528\u30df\u30c9\u30eb\u30a6\u30a7\u30a2Datadog\u3092\u6d3b\u7528\u3057\u305f\u76e3\u8996\u30b5\u30fc\u30d3\u30b9\u3092\u5c55\u958b\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n

\u30fb\u30af\u30e9\u30a6\u30c9\u904b\u7528\u76e3\u8996(Datadog)<\/a><\/p>\n

\u305d\u306e\u4e2d\u3067\u3001\u7570\u5e38\u767a\u751f\u6642\u306b\u96fb\u8a71\u901a\u77e5\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u7e4b\u304c\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u306b\u6b21\u306e\u62c5\u5f53\u8005\u306b\u96fb\u8a71\u3092\u3057\u305f\u3044\u3001\u305f\u3060\u3057\u7e4b\u304c\u3063\u305f\u5834\u5408\u306b\u306f\u305d\u3053\u307e\u3067\u3067\u96fb\u8a71\u3092\u30b9\u30c8\u30c3\u30d7\u3057\u305f\u3044\u3068\u3044\u3046\u8981\u4ef6\u304c\u3042\u308a\u307e\u3059\u3002
\n\u4eca\u56de\u306fAWS Lambda\u4e0a\u306ePython2.7\u3068\u3001
Twilio<\/a>\u3092\u4f7f\u3063\u3066\u3053\u308c\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u3084\u308a\u65b9\u3092\u7d39\u4ecb\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n

\u30b7\u30f3\u30d7\u30eb\u306b\u96fb\u8a71\u3092\u304b\u3051\u308b<\/h3>\n

\u516c\u5f0f<\/a>\u306e\u901a\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u67b6\u96fb\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

from twilio.rest import Client\n\naccount_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"\nauth_token = "your_auth_token"\nclient = Client(account_sid, auth_token)\ncall = client.calls.create(\n    to="+81XXXXXXXXX",\n    from_="+81ZZZZZZZZZ",\n    url="http:\/\/demo.twilio.com\/docs\/voice.xml"\n)<\/code><\/pre>\n

\u3053\u308c\u3067\u96fb\u8a71\u304c\u639b\u3051\u3089\u308c\u307e\u3059\u3002(account_sid\u3084\u3001token\u306f\u3001Twilio\u306b\u767b\u9332\u3057\u305f\u4e0a\u3067\u30b3\u30f3\u30bd\u30fc\u30eb\u304b\u3089\u53d6\u5f97\u3057\u3066\u304f\u3060\u3055\u3044\u3002)
\nTwilio\u306f TwiML<\/code> \u3068\u3044\u3046\u72ec\u81ea\u30de\u30fc\u30af\u30a2\u30c3\u30d7\u8a00\u8a9e\u3092\u5143\u306b\u96fb\u8a71\u3092\u639b\u3051\u307e\u3059\u3002(\u72ec\u81ea\u30de\u30fc\u30af\u30a2\u30c3\u30d7\u8a00\u8a9e\u3068\u8a00\u3063\u3066\u3082\u3001\u5f62\u5f0f\u306f\u666e\u901a\u306eXML\u3067\u3059\u3002)
\nurl="http:\/\/demo.twilio.com\/docs\/voice.xml"<\/code> \u306e\u90e8\u5206\u306f\u3001\u305d\u306eTwiML\u306eURL\u3067\u3059\u3002
\n\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u5f0f\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

<Response>\n  <Say voice="alice">\u7570\u5e38\u4e8b\u614b\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002\u7570\u5e38\u4e8b\u614b\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002<\/Say>\n<\/Response><\/code><\/pre>\n

\u3053\u306e\u3088\u3046\u306axml\u30d5\u30a1\u30a4\u30eb\u3092\u3069\u3053\u304b\u306e\u30b5\u30fc\u30d0\u306b\u7f6e\u3044\u3066\u3001\u305d\u306eURL\u3092Python\u3067\u6307\u5b9a\u3059\u308c\u3070\u3001 <Say><\/code> \u3067\u56f2\u307e\u308c\u305f\u90e8\u5206\u3092\u81ea\u52d5\u97f3\u58f0\u304c\u558b\u3063\u3066\u304f\u308c\u307e\u3059\u3002
\n\u4eca\u56de\u306f\u3001\u558b\u308b\u5185\u5bb9\u3092\u96fb\u8a71\u3054\u3068\u306b\u5e30\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u305f\u3081\u3001Lambda\u95a2\u6570\u306e\u4e2d\u3067TwiML\u3092\u751f\u6210\u3057\u3001S3\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3001\u305d\u306eURL\u3092\u5143\u306b\u67b6\u96fb\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

TwiML\u3092\u751f\u6210\u3057\u3001\u96fb\u8a71\u3092\u304b\u3051\u308b<\/h3>\n

TwiML\u306e\u751f\u6210\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n

import boto3\nfrom twilio.twiml.voice_response import VoiceResponse\n\nAWS_S3_BUCKET_NAME = 'sample_bucket'\n\n# TwiML\u306e\u751f\u6210\ntwiml = VoiceResponse()\ntwiml.say('\u25ef\u25ef\u30db\u30b9\u30c8\u3067\u7570\u5e38\u4e8b\u614b\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002', language='ja-JP', voice='alice')\nfilename = 'sample_twiml.xml'\n\n# S3\u3078\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\ns3 = boto3.client('s3')\ns3.put_object(Key=filename, Bucket=AWS_S3_BUCKET_NAME, Body=str(twiml), ContentType='text\/xml', ACL='public-read')\n\n# TwiML\u306eURL\u3092\u5143\u306b\u96fb\u8a71\u3092\u304b\u3051\u308b\nurl = 'https:\/\/s3-ap-northeast-1.amazonaws.com\/' + AWS_S3_BUCKET_NAME + '\/' + filename\ncall = client.calls.create(\n    to='+81XXXXXXXXX',\n    from_='+81ZZZZZZZZZ',\n    url=url<\/code><\/pre>\n

S3\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3068\u304d\u306fACL\u3092public-read\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u30a8\u30b9\u30ab\u30ec\u30fc\u30b7\u30e7\u30f3\u3057\u3066\u307f\u308b<\/h3>\n

\u30a8\u30b9\u30ab\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u3001 call<\/code> \u95a2\u6570\u306e\u623b\u308a\u5024\u3092\u898b\u308b\u3053\u3068\u3067\u3001\u901a\u8a71\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u53d6\u5f97\u3057\u3001\u901a\u8a71\u304c\u5b8c\u4e86\u3057\u3066\u3044\u306a\u3051\u308c\u3070\u3001\u6b21\u306e\u4eba\u306b\u67b6\u96fb\u3059\u308b\u3001\u3068\u3044\u3046\u3084\u308a\u65b9\u3092\u3068\u308a\u307e\u3059\u3002
\n\u30b9\u30c6\u30fc\u30bf\u30b9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3068\u308a\u307e\u3059\u3002<\/p>\n

call_result = client.calls.create(\n    to='+81XXXXXXXXX'\n    from_='+81ZZZZZZZZZ'\n    url=url,\n    method='GET')\n\nsid = call_result.sid\ncall_status = client.calls(sid).fetch().status\nprint call_status<\/code><\/pre>\n

\u6700\u5f8c\u306e call_status<\/code> \u304c\u901a\u8a71\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u683c\u7d0d\u3057\u305f\u6587\u5b57\u5217\u3067\u3059\u3002
\n\u4e2d\u306e\u5024\u306f
\u516c\u5f0f<\/a>\u306b\u8f09\u3063\u3066\u307e\u3059\u3002<\/p>\n

\u3053\u308c\u3092\u5143\u306b\u3001\u30a8\u30b9\u30ab\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u3053\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002<\/p>\n

phone_numbers = ['+81XXXXXXXXX', '+81YYYYYYYYY', '+81ZZZZZZZZZ']\nfor phone_number in phone_numbers:\n    print '{to}\u3078\u306e\u67b6\u96fb\u3092\u958b\u59cb\u3057\u307e\u3059\u3002'.format(to=phone_number)\n    call_result = call(phone_number)\n    time.sleep(10)\n    if call_succeeded(call_result):\n        print '{to}\u3078\u306e\u67b6\u96fb\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002'.format(to=phone_number)\n        return\n    print '{to}\u3078\u306e\u67b6\u96fb\u306b\u5fdc\u7b54\u304c\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002'.format(to=phone_number)\nprint '\u5168\u3066\u306e\u67b6\u96fb\u306b\u5fdc\u7b54\u304c\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002'\n\ndef call(to):\n    call_result = client.calls.create(\n        to=to\n        from_=TWILIO_PHONE_NUMBER_FROM,\n        url=url\n        method='GET')\n    return call_result\n\ndef call_succeeded (call_result):\n    sid = call_result.sid\n    call_status = client.calls(sid).fetch().status\n    if call_status != 'in-progress' and call_status != 'completed':\n        # completed\u306b\u3059\u308b\u3053\u3068\u3067\u96fb\u8a71\u304c\u5207\u308c\u308b\n        client.calls(sid).update(status='completed')\n        return False\n    return True<\/code><\/pre>\n

\u5b9f\u969b\u306b\u30c6\u30b9\u30c8\u3057\u305f\u6240\u3001\u304d\u3061\u3093\u3068\u30a8\u30b9\u30ab\u30ec\u30fc\u30b7\u30e7\u30f3\u304c\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n

\u307e\u3068\u3081<\/h3>\n

Twilio\u306f\u4fbf\u5229\u3002\u3053\u308c\u304b\u3089\u3082\u4f7f\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

MMM\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u67f3\u6cbc\u3067\u3059\u3002\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002 \u5317\u6d77\u9053\u306f\u3082\u3046\u3060\u3044\u3076\u5bd2\u3044\u3067\u3059\u3002 \u5f0a\u793e\u3067\u306f\u30b7\u30b9\u30c6\u30e0\u76e3\u8996\u7528\u30df\u30c9\u30eb\u30a6\u30a7\u30a2Datadog\u3092\u6d3b\u7528\u3057\u305f\u76e3\u8996\u30b5\u30fc\u30d3\u30b9\u3092\u5c55\u958b\u3057\u3066\u304a\u308a\u307e\u3059\u3002 \u30fb\u30af\u30e9\u30a6\u30c9\u904b\u7528\u76e3\u8996(Datadog) \u305d\u306e\u4e2d\u3067\u3001\u7570\u5e38\u767a\u751f\u6642\u306b\u96fb\u8a71\u901a\u77e5\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u7e4b\u304c\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u306b\u6b21\u306e\u62c5\u5f53\u8005\u306b\u96fb\u8a71\u3092\u3057\u305f\u3044\u3001\u305f\u3060\u3057\u7e4b\u304c\u3063\u305f\u5834\u5408\u306b\u306f\u305d\u3053\u307e\u3067\u3067\u96fb\u8a71\u3092\u30b9\u30c8\u30c3\u30d7\u3057\u305f\u3044\u3068\u3044\u3046\u8981\u4ef6\u304c\u3042\u308a\u307e\u3059 […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,21],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/403"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=403"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}