From c806a20d65dc281925c1962f8ed2e5b1bb126fb5 Mon Sep 17 00:00:00 2001 From: Traun Leyden Date: Mon, 20 Aug 2007 16:33:28 +0000 Subject: [PATCH] 1. add collectInput method. 2. modify the dtmf callback handlers to better deal with event inputs, as opposed to dtmf inputs. currently returns a dictionary where one key contains the body, and another contains the headers as a subdictionary. git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5624 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_cpp.h | 15 +++++- src/mod/languages/mod_python/freeswitch.py | 1 + .../mod_python/freeswitch_python.cpp | 50 +++++++++++++++++-- .../languages/mod_python/freeswitch_python.h | 13 ++++- .../languages/mod_python/mod_python_wrap.cpp | 32 ++++++++++++ src/switch_cpp.cpp | 16 ++++-- 6 files changed, 119 insertions(+), 8 deletions(-) diff --git a/src/include/switch_cpp.h b/src/include/switch_cpp.h index 13e2c19b3b..4cff416a3d 100644 --- a/src/include/switch_cpp.h +++ b/src/include/switch_cpp.h @@ -158,7 +158,7 @@ class CoreSession { * * The DTMF callback function will be set and persist * for the life of the session, and be called when a dtmf - * is pressed by user during playFile(), streamfile(), and + * is pressed by user during streamfile(), collectDigits(), and * certain other methods are executing. * */ @@ -168,6 +168,19 @@ class CoreSession { int speak(char *text); void set_tts_parms(char *tts_name, char *voice_name); + /** + * For timeout milliseconds, call the dtmf function set previously + * by setDTMFCallback whenever a dtmf or event is received + */ + int collectDigits(int timeout); + + /** + * Collect up to maxdigits digits worth of digits + * and store them in dtmf_buf. In the case of mod_python, the + * dtmf_buf parameter is configured to act as a _return_ value, + * (see mod_python.i). This does NOT call any callbacks upon + * receiving dtmf digits. For that, use collectDigits. + */ int getDigits(char *dtmf_buf, int buflen, int maxdigits, diff --git a/src/mod/languages/mod_python/freeswitch.py b/src/mod/languages/mod_python/freeswitch.py index 422ae14afb..7b5d39cdca 100644 --- a/src/mod/languages/mod_python/freeswitch.py +++ b/src/mod/languages/mod_python/freeswitch.py @@ -118,6 +118,7 @@ class CoreSession(_object): def setDTMFCallback(*args): return _freeswitch.CoreSession_setDTMFCallback(*args) def speak(*args): return _freeswitch.CoreSession_speak(*args) def set_tts_parms(*args): return _freeswitch.CoreSession_set_tts_parms(*args) + def collectDigits(*args): return _freeswitch.CoreSession_collectDigits(*args) def getDigits(*args): return _freeswitch.CoreSession_getDigits(*args) def transfer(*args): return _freeswitch.CoreSession_transfer(*args) def playAndGetDigits(*args): return _freeswitch.CoreSession_playAndGetDigits(*args) diff --git a/src/mod/languages/mod_python/freeswitch_python.cpp b/src/mod/languages/mod_python/freeswitch_python.cpp index 254497c8cd..efc5c41a49 100644 --- a/src/mod/languages/mod_python/freeswitch_python.cpp +++ b/src/mod/languages/mod_python/freeswitch_python.cpp @@ -105,10 +105,12 @@ void PySession::check_hangup_hook() { } switch_status_t PySession::run_dtmf_callback(void *input, - switch_input_type_t itype) { + switch_input_type_t itype) { PyObject *func, *arglist; PyObject *pyresult; + PyObject* headerdict; + char *resultStr; char *funcargs; switch_file_handle_t *fh = NULL; @@ -137,7 +139,47 @@ switch_status_t PySession::run_dtmf_callback(void *input, funcargs = (char *) cb_state.funcargs; - arglist = Py_BuildValue("(sis)", input, itype, funcargs); + if (itype == SWITCH_INPUT_TYPE_DTMF) { + + arglist = Py_BuildValue("(sis)", input, itype, funcargs); + } + else if (itype == SWITCH_INPUT_TYPE_EVENT) { + // DUNNO if this is correct in the case we have an event + // will be of type switch_event_t *event; + // http://www.freeswitch.org/docs/structswitch__event.html + switch_event_t *event = (switch_event_t *) input; + arglist = Py_BuildValue("({s:s}is)", + "body", event->body, + itype, + funcargs); + + // build a dictionary with all the headers + + switch_event_header_t *hp; + headerdict = PyDict_New(); + for (hp = event->headers; hp; hp = hp->next) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding event header to result"); + PyDict_SetItem(headerdict, + Py_BuildValue("s", hp->name), + Py_BuildValue("s", hp->value)); + + } + + // add it to the main event dictionary (first arg in list) + // under key 'headers' + PyObject *dict = PyTuple_GetItem(arglist, 0); + PyDict_SetItemString(dict, + "headers", + headerdict); + + //arglist = Py_BuildValue("(sis)", event->body, itype, funcargs); + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown input type: %d\n", itype); + return SWITCH_STATUS_FALSE; + } + + if (!arglist) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error building arglist"); return SWITCH_STATUS_FALSE; @@ -150,13 +192,15 @@ switch_status_t PySession::run_dtmf_callback(void *input, Py_XDECREF(arglist); // Trash arglist + Py_XDECREF(headerdict); + if (pyresult && pyresult != Py_None) { resultStr = (char *) PyString_AsString(pyresult); switch_status_t cbresult = process_callback_result(resultStr, &cb_state, session); return cbresult; } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error calling python callback\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Python callback\n returned None"); PyErr_Print(); PyErr_Clear(); } diff --git a/src/mod/languages/mod_python/freeswitch_python.h b/src/mod/languages/mod_python/freeswitch_python.h index 66ca177f1a..6db4946d96 100644 --- a/src/mod/languages/mod_python/freeswitch_python.h +++ b/src/mod/languages/mod_python/freeswitch_python.h @@ -40,8 +40,19 @@ class PySession : public CoreSession { bool begin_allow_threads(); bool end_allow_threads(); + /** + * Run DTMF callback + * + * A static method in CoreSession is the first thing called + * upon receving a dtmf/event callback from fs engine, and then + * it gets the PySession instance and calls this method with + * dtmf/event object. + * + * @param input - a dtmf char buffer, or an event 'object' (not sure..) + * @param itype - a SWITCH_INPUT_TYPE_DTMF or a SWITCH_INPUT_TYPE_EVENT + */ switch_status_t run_dtmf_callback(void *input, - switch_input_type_t itype); + switch_input_type_t itype); }; diff --git a/src/mod/languages/mod_python/mod_python_wrap.cpp b/src/mod/languages/mod_python/mod_python_wrap.cpp index ab07cf73ee..dc539bf1b3 100644 --- a/src/mod/languages/mod_python/mod_python_wrap.cpp +++ b/src/mod/languages/mod_python/mod_python_wrap.cpp @@ -4080,6 +4080,37 @@ fail: } +SWIGINTERN PyObject *_wrap_CoreSession_collectDigits(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + int arg2 ; + int result; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:CoreSession_collectDigits",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_collectDigits" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "CoreSession_collectDigits" "', argument " "2"" of type '" "int""'"); + } + arg2 = static_cast< int >(val2); + result = (int)(arg1)->collectDigits(arg2); + resultobj = SWIG_From_int(static_cast< int >(result)); + return resultobj; +fail: + return NULL; +} + + SWIGINTERN PyObject *_wrap_CoreSession_getDigits(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; CoreSession *arg1 = (CoreSession *) 0 ; @@ -5228,6 +5259,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"CoreSession_setDTMFCallback", _wrap_CoreSession_setDTMFCallback, METH_VARARGS, NULL}, { (char *)"CoreSession_speak", _wrap_CoreSession_speak, METH_VARARGS, NULL}, { (char *)"CoreSession_set_tts_parms", _wrap_CoreSession_set_tts_parms, METH_VARARGS, NULL}, + { (char *)"CoreSession_collectDigits", _wrap_CoreSession_collectDigits, METH_VARARGS, NULL}, { (char *)"CoreSession_getDigits", _wrap_CoreSession_getDigits, METH_VARARGS, NULL}, { (char *)"CoreSession_transfer", _wrap_CoreSession_transfer, METH_VARARGS, NULL}, { (char *)"CoreSession_playAndGetDigits", _wrap_CoreSession_playAndGetDigits, METH_VARARGS, NULL}, diff --git a/src/switch_cpp.cpp b/src/switch_cpp.cpp index 3863a51502..d6cc87dda8 100644 --- a/src/switch_cpp.cpp +++ b/src/switch_cpp.cpp @@ -92,7 +92,7 @@ void CoreSession::execute(char *app, char *data) const switch_application_interface_t *application_interface; sanity_check_noreturn; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CoreSession::execute. data:%s\n", data); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CoreSession::execute. app: %s data:%s\n", app, data); if ((application_interface = switch_loadable_module_get_application_interface(app))) { begin_allow_threads(); switch_core_session_exec(session, application_interface, data); @@ -158,6 +158,15 @@ void CoreSession::set_tts_parms(char *tts_name_p, char *voice_name_p) voice_name = strdup(voice_name_p); } + + +int CoreSession::collectDigits(int timeout) { + sanity_check(-1); + begin_allow_threads(); + switch_ivr_collect_digits_callback(session, ap, timeout); + end_allow_threads(); +} + int CoreSession::getDigits(char *dtmf_buf, int buflen, int maxdigits, @@ -214,7 +223,8 @@ int CoreSession::playAndGetDigits(int min_digits, terminators, audio_files, bad_input_audio_files, - dtmf_buf, 128, + dtmf_buf, + 128, digits_regex); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "playAndGetDigits dtmf_buf: %s\n", dtmf_buf); @@ -521,7 +531,7 @@ switch_status_t dtmf_callback(switch_core_session_t *session_cb, CoreSession *coresession = NULL; switch_status_t result; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dtmf_callback called\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dtmf_callback called\n"); fflush(stdout); channel = switch_core_session_get_channel(session_cb);