<div dir="auto"><div>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).</div><div><br></div><div>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.</div><div><br></div><div><br>Sent from my Huawei P40 5G phone</div></div><div style="line-height:1.5"><br><br>-------- Original message --------<br>From: Voip support <voipexpert0@gmail.com><br>Date: Tue, 20 Jul 2021, 00:20<br>To: "Kamailio (SER) - Users Mailing List" <sr-users@lists.kamailio.org><br>Subject: [SR-Users] Kamailio python_exec getting randomly TypeError<br><blockquote><div dir="ltr">Dear Kamailio users,<div>I am trying to use a python script using the module app_python3 to do some billing calculations.<br /><br />The only argument i want to pass to the python script is duration which i calculate in </div><div><br /></div><div>event_route[dialog:start] {<!-- --><br />        $dlg_var('call_start') = $TS;<br /></div><div><br /></div><div>event_route[dialog:end] {<!-- --></div>        $var('v_duration') = $TS - $dlg_var('call_start');<div><br /></div><div>So my $var('v_duration') has the duration of the call in seconds.<br /><br />Then i execute from my kamailio.cfg: </div><div>python_exec("calculateCost","$var('v_duration')");<br /></div><div><br /></div><div>The script works randomly - sometimes all is good and sometimes it throw error : <br /><br />app_python3 [python_support.c:156]: python_handle_exception(): apy_exec: calculateCost(4): Unhandled exception in the Python code:<br />                                                   TypeError: an integer is required (got type NoneType)<br /><br />                                                   The above exception was the direct cause of the following exception:<br /><br />                                                   Traceback (most recent call last):<br />                                                     File "/etc/kamailio/kamailio-python.py", line 282, in calculateCost<br />                                                       duration = int(duration)<br /></div><div><br /></div><div>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.<br /><br />I cannot figure this out why this randomly happens - maybe it's an app_python3 module issue?<br /><br />Looking to hear back from you!<br /><br /></div><div>BR,<br />Tom<br /><br /></div></div>
</blockquote></div>