4
3

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 3 years have passed since last update.

Twilio Functionsを使って営業時間内だけ電話を転送する方法

Posted at

今回はTwilio Functionsを使って、営業時間内のみTwilioにかかってきた電話を任意の電話番号に転送する仕組みを実装します。

全体イメージ

スクリーンショット 2020-04-08 17.30.15.png

以下は取得済みとします

・Twilioアカウント
・Twilioの050番号

事前準備

Twilio Functions を使って電話を転送する方法を参考にTwilio Functionsを利用して電話を転送する仕組みを作ります。

営業時間のみ電話を転送する

以下コードをTwilio Functionsに貼り付けます。

exports.handler = function(context, event, callback) {

    let twiml = new Twilio.twiml.VoiceResponse();
    var date = new Date();
    // JSTにするため9時間進める
    date.setHours(date.getHours() + 9);
    var currentHour = date.getHours();
    // 日:0、月:1、火:2、水:3、木:4、金:5、土:6
    var currentDay = date.getDay();
    // 営業時間が月曜日〜金曜日の10:00 - 18:00までの場合
    if(currentHour >= 18 || currentHour < 10 || currentDay === 0 || currentDay == 6 ){
        //業務時間外用TwiMLを出力する
        twiml.say({
            voice: 'alice',
            language: 'ja-JP' // 言語を設定しないと、日本語は読まれません。
        }, 'お電話いただきありがとうございます。大変申し訳ございませんが、ただいまの時間わ営業時間外となります。営業時間内に再度お掛け直しをお願いいたします。');
    } else {
        //  営業時間内
        twiml.dial({
            callerId: '+8150xxxxxxxx' // Twilioで取得した電話番号を設定します。ここで設定しないと転送時に非通知となってしまいます。
        },'+8190xxxxxxxx'); // 転送先の電話番号を設定します。
    }
    callback(null, twiml);
};

最後に、設定した電話番号に電話してテストしてみてください。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?