Skip to content

Instantly share code, notes, and snippets.

@ceberous
Created April 27, 2020 11:33
Show Gist options
  • Select an option

  • Save ceberous/759d4b7224276e0dbe56eb2150e05fce to your computer and use it in GitHub Desktop.

Select an option

Save ceberous/759d4b7224276e0dbe56eb2150e05fce to your computer and use it in GitHub Desktop.
Call Someone via Twilio
#!/usr/bin/env python3
import time
from twilio.rest import Client
# For $TWILIO_VOICE_RESPONSE_ENDPOINT , you have to setup some https POST endpoint ,
# that can return a "twilio ml" "voice response"
# These are expressjs examples
# const VoiceResponse = require( "twilio" ).twiml.VoiceResponse;
# app.post( "/twiliocall" , function( req , res ) {
# // if ( !req.body.ckey ) { console.log( "No CKEY" ); sendJSONResponse( res , 200 , { result: "" } ); return; }
# // if ( req.body.ckey.length !== ckey_length ) { console.log( "CKEY Length === " + req.body.ckey.length.toString() ); sendJSONResponse( res , 200 , { result: "" } ); return; }
# // if ( req.body.ckey !== ckey ) { console.log( "CKEY Sent === " + req.body.ckey ); console.log( "CKEY ===" + ckey ); sendJSONResponse( res , 200 , { result: "" } ); return; }
# const twiml = new VoiceResponse();
# twiml.say( "This is the text you want them to hear when they answer the phone call" );
# res.writeHead( 200 , { "Content-Type": "text/xml" });
# res.end( twiml.toString() );
# });
# Or , you could just have it play an mp3
# app.post( "/twiliobirthdaycall" , function( req , res ) {
# const twiml = new VoiceResponse();
# twiml.play( { loop: 1 } , personal.twilio.calls.birthday.play_url );
# res.writeHead( 200 , { "Content-Type": "text/xml" });
# res.end( twiml.toString() );
# });
TwilioClient = Client( "$ACCOUNT_SID" , "$AUTH_TOKEN" )
new_call = TwilioClient.calls.create( url="$TWILIO_VOICE_RESPONSE_ENDPOINT" , to="$NUMBER_TO_CALL" , from_="$YOUR_TWILIO_NUMBER" , method="POST" )
answered = False
for i in range( 500 ):
time.sleep( 1 )
new_call = new_call.update()
status = new_call.status
print( status )
if status == "in-progress":
answered = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment