Please note that mod_python is based on python2 and is end of life as of January 1, 2020. Please use mod_python3.
Install & Configure
import os from freeswitch import * # WARNING: known bugs with hangup hooks, use with extreme caution def hangup_hook(session, what): consoleLog("info","hangup hook for %s!!\n\n" % what) return def input_callback(session, what, obj): if (what == "dtmf"): consoleLog("info", what + " " + obj.digit + "\n") else: consoleLog("info", what + " " + obj.serialize() + "\n") return "pause" def handler(session, args): session.answer() session.setHangupHook(hangup_hook) session.setInputCallback(input_callback) session.streamFile("/my/test/audio.wav") session.hangup() #hangup the call #Now run another python script in a thread. If we # don't do it this way all subsequent work will block # the hangup message from being sent to the client new_api_obj = API() new_api_obj.executeString( "pyrun foo.postprocessing " + session.getVariable('caller_id_number'))
The second module, "postprocessing", handles our post-processing needs and for convenience resides in the same package, "foo":
import os, sys, time from freeswitch import * # everything after the command (in this case pyrun) and # the module name (in this case foo.postprocessing) will # be interpreted as a string and handed to our 'runtime' # function where it will be accessible via the argument 'arg1' def runtime(arg1): time.sleep(10) # this is just to test that we are actually # running in a separate thread. consoleLog( "info", "Caller: %s hung up 10s ago!\n" % arg1 )
When running the above example, the client should receive a hangup immediately after streamFile returns. 10 seconds later the "Caller: xxxx hung up 10s ago!" message should be printed to the console.
record_file = row + '-' + row + '.wav' session.recordFile(record_file,120,500,2)
will throw the error mentioned above even though when printing the variable record_file will result in the expected value. Instead the values row and row need to be wrapped in the str() function when assigning a value to record_file:
It's not clear if it only happens in conjunction with SQLAlchemy, but removing the hangup hook definitely fixed the problem. Hangup hooks are buggy, please avoid or use with extreme caution.
mod_python error: mod_python.c:293 Error calling python script
Check your dtmf_process function. If you used "session.setInputCallback()" function, check your callback function. It should return one of these strings: "true", "false" or "pause". Forgetting to return, or using
return True or
return False (as a boolean) will cause the error above.