View
7.635
Download
0
Category
Preview:
DESCRIPTION
2013/10/17 Twilio API 勉強会
Citation preview
2013/10/17 shin1x1
Twilio API 勉強会
Twilio API をPHP で触ってみよう
Agenda
(c) 2013 Masashi Shinbara @shin1x1
• Twilioライブラリ• インストール• 電話を受ける• 電話を受ける(簡易 IVR)• 電話をかける• セキュリティ
(c) 2013 Masashi Shinbara @shin1x1
Twilioライブラリ
(c) 2013 Masashi Shinbara @shin1x1
Twilioライブラリ• Twilio API と連携するライブラリ• 主な機能 (REST API, TwiML, バリデーション)
• 各言語版 (PHP, Ruby, Python, .NET, Java, Node.js, Salesforce, C++ 等々)
(c) 2013 Masashi Shinbara @shin1x1
Twilio PHP ライブラリ
• twilio-php• PHP 5.2 以上• PEAR, ZIP, Composer• Composer でインストール
(c) 2013 Masashi Shinbara @shin1x1
インストール
(c) 2013 Masashi Shinbara @shin1x1
コード
https://gist.github.com/shin1x1/7006593
#twilioapistudy
(c) 2013 Masashi Shinbara @shin1x1
Composer インストール
$ cd yourdir$ curl -s http://getcomposer.org/installer | php
$ lscomposer.phar
(c) 2013 Masashi Shinbara @shin1x1
twilio-php インストール
$ php composer.phar require twilio/sdk dev-master
$ lscomposer.json composer.lock composer.phar vendor
(c) 2013 Masashi Shinbara @shin1x1
インストール確認• サンプルスクリプト [sample.php]<?phprequire_once __DIR__.'/vendor/autoload.php';
$xml = new Services_Twilio_Twiml();$xml->say('Hello!');
header('Content-type: text/xml; charset=utf-8');echo $xml;
http://xxx/yourdir/sample.php
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
TwilioWebサーバ
050-xxxx-xxxx
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
HTTPリクエスト
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
処理を実行TwiML生成
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
TwiMLを返す
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
音声を返す電話を転送応答を待つ等々
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
HTTPリクエスト
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
�� ���������
�� ���������
TwiMLを返す
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける
• HTTPリクエストが来たら• 何か処理をして• TwiMLを返す
普通のWebシステムと一緒!
(c) 2013 Masashi Shinbara @shin1x1
作ってみよう!
• 電話をかけると• 「こんにちは!」と言ってくれる• TwiML 生成にライブラリを使う
(c) 2013 Masashi Shinbara @shin1x1
PHPコード•inbound.php<?phprequire_once __DIR__.'/vendor/autoload.php';
$xml = new Services_Twilio_Twiml();$xml->say('こんにちは!', array('language' => 'ja-jp'));
header('Content-type: text/xml; charset=utf-8');print $xml;
http://xxx/yourdir/inbound.php
(c) 2013 Masashi Shinbara @shin1x1
管理画面
URL を変更するhttp://hoge/yourdir/inbound.php
保存を忘れずに
(c) 2013 Masashi Shinbara @shin1x1
電話してみよう
050-xxxx-xxxx
「こんにちは!」が聞こえればok
(c) 2013 Masashi Shinbara @shin1x1
電話を受ける(簡易IVR)
(c) 2013 Masashi Shinbara @shin1x1
IVR
• プッシュフォンで操作• インタラクティブな操作を実現• 電話の転送などもできる
(c) 2013 Masashi Shinbara @shin1x1
IVR
• 宅配便の再配達依頼• コールセンター案内• チケット購入• などなど
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
• アナウンスが流れる • 1 なら「1を押しました」
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
TwilioWebサーバ
050-xxxx-xxxx
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
HTTPリクエスト
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
TwiMLを返す<Gather>
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
応答待ち
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
TwilioWebサーバ
1 をプッシュ
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
HTTPリクエスト「1が押された」Digits = 1
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
TwiMLを返す<Say>
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
�� ���������
�� ���������
「1を押しました」
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR<?phprequire_once __DIR__.'/vendor/autoload.php';
$xml = new Services_Twilio_Twiml();
if (empty($_POST['Digits'])) { $digit = null;} else { $digit = (integer)$_POST['Digits'];}
if ($digit == 1) { $xml->say('1を押しました。', array('language' => 'ja-jp'));
} else { $xml->say('こんにちは!1 を押して下さい。', array('language' => 'ja-jp'));
}
$xml->gather(array('numDigits' => 1, 'timeout' => 30));
header('Content-type: text/xml; charset=utf-8');echo $xml;
(c) 2013 Masashi Shinbara @shin1x1
簡易IVR
• アナウンスが流れる • 1 / 2 / 3 / 9 を受け付ける• 1-3 なら「Nを押しました」• 9 で終了
(c) 2013 Masashi Shinbara @shin1x1
電話をかける
(c) 2013 Masashi Shinbara @shin1x1
電話をかける
�� ���������
�� ���������
TwilioWebサーバ
HTTPリクエストREST API
電話
(c) 2013 Masashi Shinbara @shin1x1
電話をかける
�� ���������
�� ���������
TwilioWebサーバ 電話
通話に利用するTwiML取得
(c) 2013 Masashi Shinbara @shin1x1
電話をかける
�� ���������
�� ���������
TwilioWebサーバ 電話
050-xxxx-xxxxから電話!
(c) 2013 Masashi Shinbara @shin1x1
SID / Token
https://jp.twilio.com/user/account
ACCOUNT SID AUTH TOKEN
(c) 2013 Masashi Shinbara @shin1x1
電話をかける<?phprequire_once __DIR__.'/vendor/autoload.php';
$sid = 'xxx';$token = 'xxx';
$client = new Services_Twilio($sid, $token);
$ret = $client->account->calls->create( '+81-xx-xxxx-xxxx', // From number(Twilio) '+81-xx-xxxx-xxxx', // To number(Your phone number) 'http://demo.twilio.com/docs/voice.xml');
var_dump($ret->sid);
http://xxx/yourdir/outbound.php
(c) 2013 Masashi Shinbara @shin1x1
セキュリティ
(c) 2013 Masashi Shinbara @shin1x1
• SSL(自己証明書はNG)• Basic / Digest認証• リクエストのバリデーション (HMAC-SHA1形式署名検証) (ライブラリで対応可)
Twilio からのリクエスト検証
(c) 2013 Masashi Shinbara @shin1x1
セキュリティページ
https://jp.twilio.com/docs/security
(c) 2013 Masashi Shinbara @shin1x1
まとめ
(c) 2013 Masashi Shinbara @shin1x1
• Twilioとの連携は HTTP の世界• TwiML か REST API か• SMS には海外番号が必要• 既存システムとの連携から
まとめ
(c) 2013 Masashi Shinbara @shin1x1
参照
https://jp.twilio.com/docs
(c) 2013 Masashi Shinbara @shin1x1
参照
http://blog.twilio.kddi-web.com/%E3%82%BC%E3%83%AD%E3%81%8B%E3%82%89%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E3%81%9C-
twilio/
@shin1x1
(c) 2013 Masashi Shinbara @shin1x1
Recommended