forked from plivo/plivo-examples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplivo_ivr_xml_using_flask.py
70 lines (63 loc) · 2.34 KB
/
plivo_ivr_xml_using_flask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## Above line is to confirm that unicode Non-ASCII charcters would be included
## in this file
#
from flask import Flask, request, make_response
import plivo
import os
import sys
app = Flask(__name__)
@app.route('/getdigits/', methods=['GET', 'POST'])
def getdigits():
if request.method == 'GET':
digits = request.args.get('Digits', '')
elif request.method == 'POST':
digits = request.form.get('Digits', '')
resp = plivo.Response()
if digits:
if digits == '1':
text = u'This is a demo app'
resp.addSpeak(body=text, language='en-US')
elif digits == '2':
text = u'Dies ist eine Demo-Anwendung.'
resp.addSpeak(body=text, language='de-DE')
else:
text = u'Άγνωστη εισόδου'
resp.addSpeak(body=text, language='el-GR')
else:
resp.addSpeak('No input received')
ret_response = make_response(resp.to_xml())
ret_response.headers["Content-type"] = "text/xml"
return ret_response
@app.route('/digits/', methods=['GET'])
def digits():
#import ipdb; ipdb.set_trace()
action = request.args.get('action', 'http://' + request.environ['SERVER_NAME'] + '/getdigits/')
method = request.args.get('method', 'GET')
timeout = request.args.get('timeout', '')
retries = request.args.get('retries', '')
finishonkey = request.args.get('finishOnKey', '')
numdigits = request.args.get('numDigits', 0)
validdigits = request.args.get('validDigits', '0123456789#*')
params = {'action': action, 'method': method}
if timeout:
params['timeout'] = timeout
if retries:
params['retries'] = retries
if finishonkey:
params['finishOnKey'] = finishonkey
if numdigits:
params['numDigits'] = numdigits
response = plivo.Response()
getdigits = plivo.GetDigits(**params)
getdigits.addSpeak(body="Press one for English.")
getdigits.addSpeak(body="Press two for German.")
response.add(getdigits)
response.addSpeak(body="Input not received. Thank you.")
ret_response = make_response(response.to_xml())
ret_response.headers["Content-type"] = "text/xml"
return ret_response
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)