51

Click here to load reader

Twilio API を PHP で触ってみよう

Embed Size (px)

DESCRIPTION

2013/10/17 Twilio API 勉強会

Citation preview

Page 1: Twilio API を PHP で触ってみよう

2013/10/17 shin1x1

Twilio API 勉強会

Twilio API をPHP で触ってみよう

Page 2: Twilio API を PHP で触ってみよう

Agenda

(c) 2013 Masashi Shinbara @shin1x1

• Twilioライブラリ• インストール• 電話を受ける• 電話を受ける(簡易 IVR)• 電話をかける• セキュリティ

Page 3: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

Twilioライブラリ

Page 4: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

Twilioライブラリ• Twilio API と連携するライブラリ• 主な機能 (REST API, TwiML, バリデーション)

• 各言語版 (PHP, Ruby, Python, .NET, Java, Node.js, Salesforce, C++ 等々)

Page 5: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

Twilio PHP ライブラリ

• twilio-php• PHP 5.2 以上• PEAR, ZIP, Composer• Composer でインストール

Page 6: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

インストール

Page 7: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

コード

https://gist.github.com/shin1x1/7006593

#twilioapistudy

Page 8: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

Composer インストール

$ cd yourdir$ curl -s http://getcomposer.org/installer | php

$ lscomposer.phar

Page 9: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

twilio-php インストール

$ php composer.phar require twilio/sdk dev-master

$ lscomposer.json composer.lock composer.phar vendor

Page 10: Twilio API を PHP で触ってみよう

(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

Page 11: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

Page 12: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

TwilioWebサーバ

050-xxxx-xxxx

Page 13: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

HTTPリクエスト

Page 14: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

処理を実行TwiML生成

Page 15: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

TwiMLを返す

Page 16: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

音声を返す電話を転送応答を待つ等々

Page 17: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

HTTPリクエスト

Page 18: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

�� ���������

�� ���������

TwiMLを返す

Page 19: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける

• HTTPリクエストが来たら• 何か処理をして• TwiMLを返す

普通のWebシステムと一緒!

Page 20: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

作ってみよう!

• 電話をかけると• 「こんにちは!」と言ってくれる• TwiML 生成にライブラリを使う

Page 21: Twilio API を PHP で触ってみよう

(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

Page 22: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

管理画面

URL を変更するhttp://hoge/yourdir/inbound.php

保存を忘れずに

Page 23: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話してみよう

050-xxxx-xxxx

「こんにちは!」が聞こえればok

Page 24: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話を受ける(簡易IVR)

Page 25: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

IVR

• プッシュフォンで操作• インタラクティブな操作を実現• 電話の転送などもできる

Page 26: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

IVR

• 宅配便の再配達依頼• コールセンター案内• チケット購入• などなど

Page 27: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

• アナウンスが流れる • 1 なら「1を押しました」

Page 28: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

TwilioWebサーバ

050-xxxx-xxxx

Page 29: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

HTTPリクエスト

Page 30: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

TwiMLを返す<Gather>

Page 31: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

応答待ち

Page 32: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

TwilioWebサーバ

1 をプッシュ

Page 33: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

HTTPリクエスト「1が押された」Digits = 1

Page 34: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

TwiMLを返す<Say>

Page 35: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

�� ���������

�� ���������

「1を押しました」

Page 36: Twilio API を PHP で触ってみよう

(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;

Page 37: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

簡易IVR

• アナウンスが流れる • 1 / 2 / 3 / 9 を受け付ける• 1-3 なら「Nを押しました」• 9 で終了

Page 38: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話をかける

Page 39: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話をかける

�� ���������

�� ���������

TwilioWebサーバ

HTTPリクエストREST API

電話

Page 40: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話をかける

�� ���������

�� ���������

TwilioWebサーバ 電話

通話に利用するTwiML取得

Page 41: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

電話をかける

�� ���������

�� ���������

TwilioWebサーバ 電話

050-xxxx-xxxxから電話!

Page 42: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

SID / Token

https://jp.twilio.com/user/account

ACCOUNT SID AUTH TOKEN

Page 43: Twilio API を PHP で触ってみよう

(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

Page 44: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

セキュリティ

Page 45: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

• SSL(自己証明書はNG)• Basic / Digest認証• リクエストのバリデーション (HMAC-SHA1形式署名検証) (ライブラリで対応可)

Twilio からのリクエスト検証

Page 46: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

セキュリティページ

https://jp.twilio.com/docs/security

Page 47: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

まとめ

Page 48: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

• Twilioとの連携は HTTP の世界• TwiML か REST API か• SMS には海外番号が必要• 既存システムとの連携から

まとめ

Page 49: Twilio API を PHP で触ってみよう

(c) 2013 Masashi Shinbara @shin1x1

参照

https://jp.twilio.com/docs

Page 51: Twilio API を PHP で触ってみよう

@shin1x1

(c) 2013 Masashi Shinbara @shin1x1