The dlg_var is string, make sure you cast them to int for duration calculations. Also manually cast var(v_duration) to int as well, because python_exec method treats them as string (this shouldn't be a problem since python vars are typeless but just to be sure).

Now about the Null values in v_duration. Where in script, i.e. in which routes you are calling python? Seems for some call cases you try to call python BEFORE dlg:end event route runs, therefore you get Null value. Simply log the argument value before calling python, this should help identify the cases.


Sent from my Huawei P40 5G phone


-------- Original message --------
From: Voip support <voipexpert0@gmail.com>
Date: Tue, 20 Jul 2021, 00:20
To: "Kamailio (SER) - Users Mailing List" <sr-users@lists.kamailio.org>
Subject: [SR-Users] Kamailio python_exec getting randomly TypeError
Dear Kamailio users,
I am trying to use a python script using the module app_python3 to do some billing calculations.

The only argument i want to pass to the python script is duration which i calculate in 

event_route[dialog:start] {
        $dlg_var('call_start') = $TS;

event_route[dialog:end] {
        $var('v_duration') = $TS - $dlg_var('call_start');

So my $var('v_duration') has the duration of the call in seconds.

Then i execute from my kamailio.cfg: 
python_exec("calculateCost","$var('v_duration')");

The script works randomly - sometimes all is good and sometimes it throw error : 

app_python3 [python_support.c:156]: python_handle_exception(): apy_exec: calculateCost(4): Unhandled exception in the Python code:
                                                   TypeError: an integer is required (got type NoneType)

                                                   The above exception was the direct cause of the following exception:

                                                   Traceback (most recent call last):
                                                     File "/etc/kamailio/kamailio-python.py", line 282, in calculateCost
                                                       duration = int(duration)

It looks like the kamailio log show calculateCost(4) so argument 4 is passed to python function but once it executes it appear like there is no string/integer but NoneType argument.

I cannot figure this out why this randomly happens - maybe it's an app_python3 module issue?

Looking to hear back from you!

BR,
Tom