Created
December 4, 2015 00:31
-
-
Save yasuharu519/77d47db0ecaf43335e5a to your computer and use it in GitHub Desktop.
Twilio Conference call のサンプル
This file contains 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
#!/usr/bin/env python | |
# coding: utf-8 | |
from flask import Flask, request, url_for | |
from twilio import twiml as Twiml | |
from twilio.rest import TwilioRestClient | |
import redis | |
# アプリケーション開始 | |
app = Flask(__name__, static_url_path='/static') | |
ACCOUNT_SID = '<YOUR SID>' | |
AUTH_TOKEN = '<YOUR AUTH_TOKEN>' | |
CONFERENCE_ID = "<CONFERENCE NAME>" | |
ENDPOINT_ROOT = "http://localhost" | |
# const values | |
TWILIO_TEL_NUM = '<PHONE NUMBER PROVIDED BY TWILIO>' | |
DIAL_NUM = "<YOUR CLIENT NAME>" | |
REDIS_HOST = "<HOSTNAME OF REDIS>" | |
REDIS_PORT = 14375 | |
REDIS_PASS = "<REDIS PASSOWRD>" | |
# クライアントセットアップ | |
twilio_cli = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) | |
redis_cli = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=0, | |
password=REDIS_PASS) | |
def join_conference_twiml(mute=False): | |
res = Twiml.Response() | |
with res.dial() as dial: | |
dial.conference(CONFERENCE_ID, muted=mute, beep=False, waitUrl="", | |
startConferenceOnEnter=True, endConferenceOnExit=True) | |
return str(res) | |
@app.route("/", methods=['GET', 'POST']) | |
def index(): | |
from_number = request.values.get('From', None) | |
to_number = request.values.get('To', None) | |
if (from_number == to_number): | |
# twilio 自身が電話をかけた場合 | |
res = Twiml.Response() | |
with res.dial() as dial: | |
dial.conference(CONFERENCE_ID, beep=False) | |
return str(res) | |
twilio_cli.calls.create(from_=TWILIO_TEL_NUM, to=DIAL_NUM, | |
url=url_for('conference_joined', | |
_external=True)) | |
return join_conference_twiml() | |
@app.route("/conference_joined", methods=['GET', 'POST']) | |
def conference_joined(): | |
return join_conference_twiml(True) | |
@app.route("/message_req", methods=['POST']) | |
def message_req(): | |
message = request.form.get('message', "") | |
# Redis に一旦保存 | |
redis_cli.rpush('message', message) | |
print "pushed_message: ", message | |
if message: | |
twilio_cli.calls.create(from_=TWILIO_TEL_NUM, to=TWILIO_TEL_NUM, | |
url=url_for('message_res', _external=True)) | |
return "" | |
@app.route('/message_res', methods=['POST']) | |
def message_res(): | |
message = redis_cli.lpop('message') | |
print "poped_message: ", message | |
res = Twiml.Response() | |
say_option = {"language": "ja-jp", "voice": Twiml.Say.MAN} | |
res.append(Twiml.Say('dummy', **say_option)) | |
res.append(Twiml.Say(message.decode('utf-8'), **say_option)) | |
return str(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment