Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import twilio.twiml
from flask import Flask, render_template, request
from twilio.util import TwilioCapability
from werkzeug.contrib.fixers import ProxyFix
from werkzeug.middleware.proxy_fix import ProxyFix

import twilio_wrapper as twrapper

Expand All @@ -26,12 +25,31 @@
@app.route('/', methods=['POST', 'GET'])
def main_page():
"""Respond to incoming requests."""
capability = TwilioCapability(account_sid, auth_token)
return render_template('index.html')


capability.allow_client_outgoing(application_sid)
token = capability.generate()
@app.route('/makecall', methods=['POST'])
def make_call():
"""Initiate the prank call."""
number1 = request.form.get('PhoneNumber1', None)
number2 = request.form.get('PhoneNumber2', None)
state = int(request.form.get('State', 0))

if not number1 or not number2:
return "Error: Both phone numbers required", 400

global room
if room > 100000:
room = 0
room += 1

result = twrapper.setUpCall(state, [number1, number2], 'pr' + str(room))

if result == -1:
return "Error setting up call", 500

return {"status": "success", "message": "Calls initiated"}

return render_template('index.html', token=token)


@app.route('/voice', methods=['POST', 'GET'])
Expand All @@ -51,7 +69,7 @@ def voice():
theNums = [number1, number2]

for num in theNums:
if num is not None and re.search('^[\d\(\)\- \+]+$', num):
if num is not None and re.search(r'^[\d\(\)\- \+]+$', num):
numbers.append(num)
if len(numbers) < 2:
return error
Expand Down
19 changes: 8 additions & 11 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3
heroku==0.1.2
httplib2==0.7.7
python-dateutil==1.5
requests==1.1.0
six==1.2.0
twilio==3.4.3
unittest2==0.5.1
wsgiref==0.1.2
Flask==2.3.0
Jinja2==3.1.2
Werkzeug==2.3.0
httplib2==0.22.0
python-dateutil==2.8.2
requests==2.31.0
six==1.16.0
twilio==8.10.0
45 changes: 22 additions & 23 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/interactivity.js"></script>
<script type="text/javascript" src="/static/js/ga.js"></script>
<script type="text/javascript" src="//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"></script>
<script type="text/javascript">
/* Create the Client with a Capability Token */
Twilio.Device.setup("{{ token }}");

Twilio.Device.ready(function (device) {
$("#log").text("Ready");
});

Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});

Twilio.Device.connect(function (conn) {
$("#log").text("Successfully established call, have fun! ;)");
});

Twilio.Device.disconnect(function (conn) {
$("#log").text("Call ended");
});
$(document).ready(function() {
$("#log").text("Ready");
});

function call() {
//get the phone number to connect the call to
params = {"PhoneNumber1": $("#caller1").val(), "PhoneNumber2": $("#caller2").val(), "State": $("#states").val()};
Twilio.Device.connect(params);
$("#log").text("Initiating prank call...");
var params = {
"PhoneNumber1": $("#caller1").val(),
"PhoneNumber2": $("#caller2").val(),
"State": $("#states").val()
};

$.ajax({
url: '/makecall',
type: 'POST',
data: params,
success: function(response) {
$("#log").text("Calls initiated! Both parties will receive calls.");
},
error: function(xhr, status, error) {
$("#log").text("Error: " + error);
}
});
}

function hangup() {
Twilio.Device.disconnectAll();
$("#log").text("Hangup not implemented in simplified version");
}
</script>
</head>
Expand Down
37 changes: 23 additions & 14 deletions twilio_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
# Download the library from twilio.com/docs/libraries
from twilio.rest import TwilioRestClient
from twilio.rest import Client
import os

account_sid = os.environ.get('ACCOUNT_SID', None)
auth_token = os.environ.get('AUTH_TOKEN', None)
application_sid = os.environ.get('APP_SID', None)

# Get these credentials from http://twilio.com/user/account
client = TwilioRestClient(account_sid, auth_token)
client = Client(account_sid, auth_token)
room_number = 0

states = ['+14158771437', '+14846794074']
# Your Twilio phone number
twilio_number = os.environ.get('TWILIO_PHONE_NUMBER', None)


def setUpCall(from_state, numbers, room):
if len(numbers) < 2 or not 0 <= from_state < 2:
print "error: not enough numbers for conference"
if len(numbers) < 2:
print("error: not enough numbers for conference")
return -1
global client
global twilio_number

for num in numbers:
call = client.calls.create(to="+1" + num,
from_=states[from_state],
url="http://twimlets.com/conference?Name=" +
room + "&Message=%20")
# Call both people using your Twilio number as caller ID
call1 = client.calls.create(
to="+1" + numbers[0],
from_=twilio_number,
url="http://twimlets.com/conference?Name=" + room + "&Message=%20"
)

call2 = client.calls.create(
to="+1" + numbers[1],
from_=twilio_number,
url="http://twimlets.com/conference?Name=" + room + "&Message=%20"
)

return call.sid
return call2.sid


def killCall(call_id):
call = client.calls.update(call_id, status="completed")
print call.start_time
call = client.calls(call_id).update(status="completed")
print(call.start_time)


def cleanUpNum(num):
num = num.translate(None, '()-')
num = num.translate(str.maketrans('', '', '()-'))
return num