Created
February 6, 2019 02:59
-
-
Save kawaz/462f79f6f2bb684e1fea91dd1ee92aaf to your computer and use it in GitHub Desktop.
iPhoneを探す的なことをするTwilioFunctions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const FUNC_PATH = '/search-iphone'; | |
| const FROM = '+8150XXXXXXXX'; //Twilioで購入済みの電話番号 | |
| //電話番号リスト。冗長だけどとりあえずキーワード複数に同じ番号を定義しとく | |
| const TO_NUMBERS = { | |
| 'お父さん': '+8190XXXXXXXX', | |
| '父さん': '+8190XXXXXXXX', | |
| 'お母さん': '+8190YYYYYYYY', | |
| '母さん': '+8190YYYYYYYY', | |
| }; | |
| //電話に出たときに再生されるメッセージ(適当) | |
| const messages = [ | |
| '今日のラッキーカラーはピンクです', | |
| '見つけてくれてありがとうございます。またのご利用をお待ちしております', | |
| '私リカちゃん。今グアムに居るの', | |
| ]; | |
| exports.handler = function(context, event, callback) { | |
| console.log({event, context}); | |
| const client = context.getTwilioClient(); | |
| //電話に出たとき用のTwiMLを返す | |
| if(event.say) { | |
| const message = messages[Math.floor(Math.random() * messages.length)]; | |
| const twiml = new Twilio.twiml.VoiceResponse(); | |
| twiml.say({language: 'ja-JP'}, message); | |
| return callback(null, twiml); | |
| } | |
| //電話番号が見つかったら電話を鳴らす | |
| to = TO_NUMBERS[event.TextField]; | |
| if(to) { | |
| client.calls.create({ | |
| url: `https://${context.DOMAIN_NAME}${FUNC_PATH}?say=1`, //電話に出たときに実行するTwiMlのURL | |
| from: FROM, | |
| to, | |
| endTime: new Date(Date.now() + 60*1000).toGMTString(), | |
| }, function(err, result) { | |
| console.log(`New phone call started... from: ${FROM} to:${to}`); | |
| console.log(result); | |
| return callback(err, result); | |
| }); | |
| } else { | |
| //電話番号が見つからなかった | |
| callback(); | |
| } | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
電話にでるのを拒否すると何度もかかってくるやつ、拒否されたらリトライしないのがベストだが出来るのかな?要調査。