Greet Caller by Name in Python
import urllib.parse
from cgi import escape
def application(environ, start_response):
result = receiveCall(environ)
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(result)))]
start_response('200 OK', response_headers)
return [result]
def receiveCall(environ):
try:
length= int(environ.get('CONTENT_LENGTH', '0'))
if(length != 0):
body = environ['wsgi.input'].read(length)
receivedData = urllib.parse.parse_qs(body.decode("utf-8"))
caller = escape(receivedData['Caller'][0])
phoneBook = {'+12345671':"John", '1001':"Kevin", '+12345675':"Jack"}
callerName= "Sir or Madame"
if caller in phoneBook:
callerName= phoneBook[caller]
return """<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Speak>Hello {0}</Speak>
</Response>""".format(callerName)
except IOError:
return ""
Code example 1
- Greeting the caller by using a phonebook
IN MORE DETAILS
- When your application receives a call, you will get a Request with all of the call details.
- Fill an associative array with telephone numbers and names.
- In this associative array (phonebook) we are looking for the Caller parameter of the Request.
- After that, send back the response OzML to the caller.