Skip to content

Instantly share code, notes, and snippets.

@tnuki10
Created April 15, 2013 10:48
Show Gist options
  • Save tnuki10/5387275 to your computer and use it in GitHub Desktop.
Save tnuki10/5387275 to your computer and use it in GitHub Desktop.
Twilioのクイックスタート
# -*- coding: utf-8 -*-
import webapp2
import logging
from twilio.util import TwilioCapability
import twilio.twiml
from app.handlers.common import comJinja2
from app.handlers.message_config import *
__author__ = 't_nuki'
jinja2_templateEngine = comJinja2.TemplateEngine()
ACCOUNT_SID = "xxxxxxxxxxxxxxxxxxxx"
AUTH_TOKEN = "xxxxxxxxxxxxxxxxxxxxxx"
# This is a special Quickstart application sid - or configure your own
# at twilio.com/user/account/apps
APPLICATION_SID = "xxxxxxxxxxxxxxxxxxxxx"
#Twilioの電話番号
CALLER_ID = "+815011111111"
#ブラウザの識別に使用
CLIANT_ID = 'nukipon'
class VoiceHandler(webapp2.RequestHandler):
def post(self):
"""
電話を受信するクラス。
:return:
"""
resp = twilio.twiml.Response()
# Nest <Client> TwiML inside of a <Dial> verb
with resp.dial(callerId=CALLER_ID) as r:
r.client(CLIANT_ID)
logging.info(str(resp))
self.response.out.write(str(resp))
def get(self):
self.post()
class ClientHandler(webapp2.RequestHandler):
def get(self):
"""
電話画面を表示するクラス
:return:
"""
capability = TwilioCapability(ACCOUNT_SID, AUTH_TOKEN)
capability.allow_client_outgoing(APPLICATION_SID)
capability.allow_client_incoming(CLIANT_ID)
token = capability.generate()
twilio_values = {
'token': token,
}
#一応アドミン権限を必要としてみた
content = jinja2_templateEngine.render(twilio_values, 'admin/twilio/client.html')
self.response.out.write(content)
#postでも受け取れるように
def post(self):
self.get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment