Last active
August 29, 2015 14:06
-
-
Save dfee/dcc4203a34c4e3978683 to your computer and use it in GitHub Desktop.
FreeSwitch mod_python Directories Example [modern]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import freeswitch | |
sound_path_prefix = "/usr/local/freeswitch/sounds/custom/" | |
def text_to_digit(text, len): | |
"""Map text to digits""" | |
atoi_d = { | |
"a": 2, "b": 2, "c": 2, | |
"d": 3, "e": 3, "f": 3, | |
"g": 4, "h": 4, "i": 4, | |
"j": 5, "k": 5, "l": 5, | |
"m": 6, "n": 6, "o": 6, | |
"p": 7, "q": 7, "r": 7, "s": 7, | |
"t": 8, "u": 8, "v": 8, | |
"w": 9, "x": 9, "y": 9, "z": 9, | |
} | |
return ''.join([str(atoi_d[c]) for c in text[0:len]]) | |
def normalize_name(name): | |
freeswitch.consoleLog("alert", "Normalizing name: {}\n".format(name)) | |
l_name, f_name = [w.strip() for w in name.split(', ')] | |
normalized = "{} {}".format(f_name, l_name) | |
freeswitch.consoleLog("alert", "Normalized as: {}\n".format(normalized)) | |
return normalized | |
def speak_text(session, text): | |
freeswitch.consoleLog("alert", "Speaking text: {}\n".format(text)) | |
for word in text.split(): | |
session.execute("phrase", "spell,{}".format(word)) | |
session.execute("sleep", "250") | |
def confirm_match(session, name, extension): | |
freeswitch.consoleLog("alert", "Matched: {}\n".format(name)) | |
speak_text(session, normalize_name(name)) | |
speak_text(session, extension) | |
session.execute("sleep", "1000") | |
# "Press 1# if correct, otherwise press * to start over" | |
digits_keyed = session.playAndGetDigits( | |
1, # min_digits | |
1, # max_digits | |
3, # max_attempts | |
2000, # timeout | |
"#", # terminators | |
"{}dir-instr.wav".format(sound_path_prefix), # prompt_audio_file | |
"", # input_error_audio_file | |
"1|\*", # digit_regex | |
) | |
freeswitch.consoleLog("alert", "digits_keys: {}\n".format(digits_keyed)) | |
return digits_keyed == "1" | |
def ivr_switch(session): | |
names = { | |
# "last_name, first_name": "extension", | |
"Allen, Larry": "5000", | |
"Monroe, Beckey": "1001", | |
} | |
names = {k.lower(): v for k, v in names.items()} | |
freeswitch.consoleLog("alert", "Names formatted") | |
code_to_name = defaultdict(list) | |
for k, v in [(text_to_digit(k, 3), k) for k in names]: | |
code_to_name[k].append(v) | |
# Get digits | |
digits_keyed = session.playAndGetDigits( | |
3, # min_digits | |
3, # max_digits | |
10, # max_attempts | |
5000, # timeout | |
"*#", # terminators | |
"{}dir-intro.wav".format(sound_path_prefix), # prompt_audio_file | |
"", # input_error_audio_file | |
"\d{3}", # digit_regex | |
) | |
freeswitch.consoleLog("alert", "digits_keyed: {}\n".format(digits_keyed)) | |
# Confirm name with caller | |
for name in code_to_name.get(digits_keyed, []): | |
freeswitch.consoleLog("alert", "Matched on {}".format(digits_keyed)) | |
extension = names[name] | |
# Transfer upon confirmation | |
if confirm_match(session, name, extension): | |
freeswitch.consoleLog("alert", "I am here") | |
return session.transfer(extension, "XML", "default") | |
# No name matched, start over | |
freeswitch.consoleLog("alert", "No match on {}".format(digits_keyed)) | |
session.streamFile("{}dir-nomatch.wav".format(sound_path_prefix)) | |
ivr_switch(session) | |
def handler(session, args): | |
session.setAutoHangup(False) | |
session.answer() | |
ivr_switch(session) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment