AGI Script: modify extension

Hi, Iam trying to set, in external AGI script new extension (on current channel) based on some conditions.
From log i can see is updated:

ENV LINE: agi_request: parse_csv.py
ENV LINE: agi_channel: SIP/carrier-00000000
ENV LINE: agi_language: en
ENV LINE: agi_type: SIP
ENV LINE: agi_uniqueid: 1713451836.0
ENV LINE: agi_version: 18.22.0
ENV LINE: agi_callerid: +390523375511
ENV LINE: agi_calleridname: CARRIER
ENV LINE: agi_callingpres: 0
ENV LINE: agi_callingani2: 0
ENV LINE: agi_callington: 0
ENV LINE: agi_callingtns: 0
ENV LINE: agi_dnid: +390471547200
ENV LINE: agi_rdnis: unknown
ENV LINE: agi_context: advanced_routing
ENV LINE: agi_extension: start
ENV LINE: agi_priority: 4
ENV LINE: agi_enhanced: 0.0
ENV LINE: agi_accountcode:
ENV LINE: agi_threadid: 140489937413888
ENV LINE: agi_arg_1: /var/lib/asterisk/agi-bin/numeri.csv
ENV LINE:
class AGI: self.env = {‘agi_accountcode’: ‘’,
‘agi_arg_1’: ‘/var/lib/asterisk/agi-bin/numeri.csv’,
‘agi_callerid’: ‘+390523375511’,
‘agi_calleridname’: ‘CARRIER’,
‘agi_callingani2’: ‘0’,
‘agi_callingpres’: ‘0’,
‘agi_callingtns’: ‘0’,
‘agi_callington’: ‘0’,
‘agi_channel’: ‘SIP/carrier-00000000’,
‘agi_context’: ‘advanced_routing’,
‘agi_dnid’: ‘+390476547200’,
‘agi_enhanced’: ‘0.0’,
‘agi_extension’: ‘start’,
‘agi_language’: ‘en’,
‘agi_priority’: ‘4’,
‘agi_rdnis’: ‘unknown’,
‘agi_request’: ‘parse_csv.py’,
‘agi_threadid’: ‘140489937413888’,
‘agi_type’: ‘SIP’,
‘agi_uniqueid’: ‘1713451836.0’,
‘agi_version’: ‘18.22.0’}
COMMAND: VERBOSE “#### python agi started ####” 1
RESULT_LINE: 200 result=1
RESULT_DICT: {‘result’: (‘1’, ‘’)}
COMMAND: SET VARIABLE “NEW_CALLERIDNAME” “XXXXXXX”
RESULT_LINE: 200 result=1
RESULT_DICT: {‘result’: (‘1’, ‘’)}
COMMAND: SET VARIABLE “NEW_CALLEDNUM” “+390523999111”
RESULT_LINE: 200 result=1
RESULT_DICT: {‘result’: (‘1’, ‘’)}
COMMAND: SET EXTENSION +390523999111
RESULT_LINE: 200 result=0
RESULT_DICT: {‘result’: (‘0’, ‘’)}

But in asterisk console a get following error:

[Apr 18 17:02:44] == Using SIP RTP CoS mark 5
[Apr 18 17:02:44] > 0x7fc66c020750 – Strict RTP learning after remote address set to: 192.168.25.20:7088
[Apr 18 17:02:44] – Executing [+390471547200@from-trunk-sip-carrier:1] NoOp(“SIP/carrier-00000001”, "Chiamata entrante dal TRUNK SIP: *** carrier *** ") in new stack
[Apr 18 17:02:44] – Executing [+390471547200@from-trunk-sip-carrier:2] Goto(“SIP/carrier-00000001”, “from-trunk,+390471547200,1”) in new stack
[Apr 18 17:02:44] – Goto (from-trunk,+390471547200,1)
[Apr 18 17:02:44] – Executing [+390471547200@from-trunk:1] Set(“SIP/carrier-00000001”, “CHANNEL(tonezone)=it”) in new stack
[Apr 18 17:02:44] – Executing [+390471547200@from-trunk:2] Set(“SIP/carrier-00000001”, “CHANNEL(musicclass)=none”) in new stack
[Apr 18 17:02:44] – Executing [+390471547200@from-trunk:3] Goto(“SIP/carrier-00000001”, “advanced_routing,start,1”) in new stack
[Apr 18 17:02:44] – Goto (advanced_routing,start,1)
[Apr 18 17:02:44] – Executing [start@advanced_routing:1] NoOp(“SIP/carrier-00000001”, “*** ADVANCED ROUTING STARTED ***”) in new stack
[Apr 18 17:02:44] – Executing [start@advanced_routing:2] Answer(“SIP/carrier-00000001”, “”) in new stack
[Apr 18 17:02:44] > 0x7fc66c020750 – Strict RTP switching to RTP target address 192.168.25.20:7088 as source
[Apr 18 17:02:44] – Executing [start@advanced_routing:3] Set(“SIP/carrier-00000001”, “ORIG_CALLED=+390471547200”) in new stack
[Apr 18 17:02:44] – Executing [start@advanced_routing:4] AGI(“SIP/carrier-00000001”, “parse_csv.py,/var/lib/asterisk/agi-bin/numeri.csv”) in new stack
[Apr 18 17:02:44] – Launched AGI Script /var/lib/asterisk/agi-bin/parse_csv.py
[Apr 18 17:02:44] parse_csv.py,/var/lib/asterisk/agi-bin/numeri.csv: #### python agi started ####
[Apr 18 17:02:44] – <SIP/carrier-00000001>AGI Script parse_csv.py completed, returning 0
[Apr 18 17:02:44] WARNING[557][C-00000002]: pbx.c:4507 __ast_pbx_run: Channel ‘SIP/carrier-00000001’ sent to invalid extension but no invalid handler: context,exten,priority=advanced_routing,+390523999111,5

Here snippet code that do it:

[if name and internal_number:
    # Set the caller ID name and number
    agi.set_variable('NEW_CALLERIDNAME', name)
    agi.set_variable('NEW_CALLEDNUM', internal_number)
    agi.set_extension(internal_number)
else:
    agi.verbose("CALLERID not updated: Caller number not found in the CSV file.")]

What’s wrong?

regards

You haven’t shown us the dialplan for that extension, but I suspect it doesn’t have a priority 5. As usual, the documentation is a bit too terse, and I don’t know if you need to set it to 0 or 1, to get 1 as the next priority after the AGI execution (GoTo sets it to one less than you specify).

Although it looks, now, as though there there are mechanisms to update the “program counter” information within AGI, I’d suggest it is bad practice to use them as you, basically, have a GoTo as a side effect of the AGI application.

Does

dialplan show +390523999111@advanced_routing

yield a clue?

In generral, I agree. Having AGIs vector off to strange places makes it difficult to maintain and debug.

A ‘better practice’ would be to set a channel variable and then do something with it in the dialplan.

For example, in the AGI:

set variable new_extension "foo"

and then in the dialplan:

goto(${new_extension})

I think that should either be:

set variable new_extension "foo,1"

or

goto(${new_extension},1)

1 Like

Hi guys, I solved it by simply setting a variable in the AGI script with the new number to call, and in the dial plan I set a DIAL with the new variable.
Very simple.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.