7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS LambdaとTwilioで電話をならしてメッセージを流してみる

Last updated at Posted at 2015-06-24

SNSからLambda経由でtwilioを利用して電話するのを試してみた。
これでSNS通知で電話をかけるということができるようになった。

試した環境

以下のようにSNSを実行するとLambdaでTwilioを利用して電話しメッセージを流すようにした。

  1. SNS実行
  2. Lambda呼び出される
  3. S3にTwilioメッセージ用のSNSメッセージのタイトルを含むXML配置
  4. Twilio APIを呼び出して電話かける

やったこと

  • twilioモジュールインストール
$ npm install twilio
  • index.js作成
// Twilio Credentials 
var accountSid = '[account sid]'; 
var authToken = '[auth token]'; 
 
//require the Twilio module and create a REST client 
var client = require('twilio')(accountSid, authToken); 
var AWS = require('aws-sdk')
var S3Bucket = '[S3 バケット名]'
var XMLKey = 'voice.xml'

exports.handler = function(event, context) {
  var s3 = new AWS.S3({region: '[s3 リージョン]'});
  var params = {
    Bucket: S3Bucket,
    Key: XMLKey,
    ACL: 'public-read',
    ContentType: 'text/xml',
    Body: '<?xml version="1.0" encoding="UTF-8"?>\n' +
          '<Response>\n' +
          '<Say voice="alice" language="ja-JP">' +
          event.Records[0].Sns.Subject +
          '</Say>\n' +
          '</Response>'
  }
  s3.putObject(params, function(err, data) {
    if (err) console.log(err, err.stack);
    else console.log(data);
  });  

  client.calls.create({  
    from: "[twilio電話番号]",   
    to: "[掛け先電話番号]",
    method: "GET", 
    url: "https://s3.amazonaws.com/" + S3Bucket + "/" + XMLKey 
  }, function(err, call) { 
    console.log(call.sid); 
  });  
}   
  • zipにまとめる
$ zip -r twilio.zip index.js node_modules/
  • lambdaにzipを上げる
  • lambdaで利用するロールにはS3バケットの権限をつけておく
  • SNSトピックを作成してサブスクリプションにlambdaを指定する
  • SNSトピックをPublishしてタイトルを指定しておくと、電話でタイトルの内容を流してくれる

参照サイト

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?