Guest User

Untitled

a guest
Jan 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. from flask import Flask
  2. from flask import jsonify
  3. #from flask import render_template
  4. #from flask import request
  5. from flask import url_for
  6.  
  7. from twilio.twiml.voice_response import VoiceResponse
  8. from twilio.rest import Client
  9.  
  10. app = Flask(__name__)
  11.  
  12. # Voice Request URL
  13. @app.route('/call')
  14. def call():
  15. # Get phone number we need to call
  16. phone_number = request.form.get('phoneNumber', None)
  17.  
  18. try:
  19. twilio_client = Client(app.config['TWILIO_ACCOUNT_SID'],
  20. app.config['TWILIO_AUTH_TOKEN'])
  21. except Exception as e:
  22. msg = 'Missing configuration variable: {0}'.format(e)
  23. return jsonify({'error': msg})
  24.  
  25. try:
  26. twilio_client.calls.create(from_=app.config['TWILIO_CALLER_ID'],
  27. to=phone_number,
  28. url=url_for('.outbound',
  29. _external=True))
  30. except Exception as e:
  31. app.logger.error(e)
  32. return jsonify({'error': str(e)})
  33.  
  34. return jsonify({'message': 'Call incoming!'})
  35.  
  36.  
  37. @app.route('/outbound', methods=['POST'])
  38. def outbound():
  39. response = VoiceResponse()
  40.  
  41. response.say("Thank you for contacting our sales department. If this "
  42. "click to call application was in production, we would "
  43. "dial out to your sales team with the Dial verb.",
  44. voice='alice')
  45.  
  46. response.number("+16518675309")
  47.  
  48. return str(response)
  49.  
  50. if __name__ == '__main__':
  51. app.run()
Add Comment
Please, Sign In to add comment