Last active
August 29, 2015 14:26
-
-
Save dtolb/11ed3c2c6a998509f770 to your computer and use it in GitHub Desktop.
Call transfer using Flask and Bandwidth-SDK
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
# Example was written with ngrok.io, flask, and bandwidth-sdk | |
# Pre Reqs: | |
# pip install flask (http://flask.pocoo.org/) | |
# pip install bandwidth_sdk (https://github.com/bandwidthcom/python-bandwidth) | |
# To use: | |
# Step 1: Order a phone number and assign that to a catapult application with autoanswer | |
# Step 2: Set the callback URL to POST and the ngrok url (IE: http://eb111111.ngrok.io/callback) | |
# Step 3: Fill in your information for Catapult ID, Token, Secret | |
# Step 4: Fill in your information for 'transfer number' | |
# Step 5: Launch the app: python call_transfer.py | |
# Step 6: Call the number ordered in step 1 | |
# Step 7: Notice that the number set in Step 4 will ring with caller_id set to Bandwidth # | |
# Step 8: Talk! | |
# Step 9: Profit | |
import json | |
from flask import Flask, request | |
from bandwidth_sdk import Client, AnswerCallEvent, Call, Event | |
app = Flask(__name__) | |
# SERVER_NAME should be the ngrok URL WITHOUT http:// or port | |
# The name and port number of the server | |
SERVER_NAME = 'eb111111.ngrok.io' # SAMPLE ngrok SEVER_NAME | |
# Set to true to enable web debugging. Be careful as this feature will dump | |
# stack traces that may contain sensitive data when errors happen. | |
DEBUG = True | |
# Your Catapult API User | |
CATAPULT_USER_ID = 'u-asdtlaksdgjioe9253829i5' # SAMPLE USER_ID | |
# Your Catapult API Token | |
CATAPULT_API_TOKEN = 't-kelkasdf5riaokuoxenr5232' # SAMPLE API_TOKEN | |
# Your Catapult API Secret | |
CATAPULT_API_SECRET = 'j35239sna3ub2sapwe69scs3pudp4cz2qqtozry' # SAMPLE SECRET | |
# Hard coded example number, this is the number that will ring | |
# Transfer Number | |
TRANSFER_NUMBER = '+12223334444' | |
# Initialize bandwidth sdk with your credentials | |
Client(CATAPULT_USER_ID, | |
CATAPULT_API_TOKEN, | |
CATAPULT_API_SECRET) | |
# Note that the Callback URL will be POST to http://eb111111.ngrok.io/callback | |
@app.route('/callback', methods=['POST']) | |
def callback(): | |
""" | |
Route a catapult callback to the appropriate handler. These functions | |
control call flow. | |
:return: | |
""" | |
# process the event | |
event = Event.create(**request.json) | |
app.logger.debug('received callback event: %s' % event) | |
if isinstance(event, AnswerCallEvent): | |
transfer_call(event) | |
else: | |
app.logger.debug('event unhandled: %s' % event) | |
return 'ok' | |
def transfer_call(event): | |
""" | |
transfers an answered call event. | |
:param event: | |
:return: | |
""" | |
call = Call.get(event.call_id) | |
# Will set the caller ID to the Bandwidth Number, thus masking the original caller's phone number | |
call.transfer(TRANSFER_NUMBER, transfer_caller_id=event.to) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment