LoginSignup
1
1

More than 5 years have passed since last update.

今さらTwilio

Last updated at Posted at 2016-07-19

「出るまで、順番に電話をかける」というお題で、今更Twilioを使ってみました。
TROPOの方が情報が少なくて手探りで面白そうとでしたが、そこにはスケジュールがの壁が。。

PHP版で良いサンプルがなかったので、備忘録も兼ねてソースをアップします。
構成は電話をかけるPHPと喋らす内容を記述したXMLの2つのファイルです。

Twilioの情報と、電話番号の別ファイルでの管理部分と、電話後のメッセージの取り扱いは環境によりけりなので割愛しています。
割愛しすぎて間違えているところがあるかも知れませんが。。

■call.php

<?php
  require "Twilio.phpのパス";

  // パラメーター
  $status = $_REQUEST["CallStatus"];
  $count = $_REQUEST["count"];
  $total = $_REQUEST["total"];

  // 連絡先用番号の処理
  if($count == ''){
    $currentcount = 0;
  }else{
    $currentcount = $count + 1;
  }

  // 電話をかけた回数の処理
  $total++;

  // Twilio用設定
  //$sid       ACCOUNT SID
  //$token     AUTH TOKEN
  //$timeout   電話を鳴らす秒数
  //$maxcall   電話をかける回数
  //$from      発信元
  //$xml       Twilio動作用XML
  //$url       コールバックのURL

  // 電話番号
  // コールバックのURLに何人目かを仕込んでいるので順々に電話
  $number = array('***********','***********','***********');
  if(count($number) == $currentcount){
    $currentcount = 0;
  }

  // Call back用URL の生成
  $url = str_replace('COUNT',$currentcount,$url);
  $url = str_replace('TOTAL',$total,$url);

  // 電話の処理
  $exec = 0;
  if($status != 'completed'){
    if($total <= $maxcall){
      $msg = $number[$count].' に電話をします。';
      $client = new Services_Twilio($sid,$token);
      $call = $client->account->calls->create($from,$number[$count],
          $xml,array(
            "StatusCallback" => "$url",
            "Timeout" => "$timeout"
          ));
    }else{
      $msg = '誰も電話に出てくれない。';
    }
  }else{
    $msg = $number[$count].' が電話に出ました。';
  }
?>

■call.xml

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say language="ja-jp">喋らせたい文言</Say>
</Response>
1
1
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
1
1