Extract Display Name from SIP header

Hello,
I’m trying to extract the Display Name in the From URI from the SIP header in the INVITE and place it in an Asterisk variable, however it looks like the PJSIP functions I’m using are not working.

This is my setup until now:

extensions_custom.conf

[macro-dialout-trunk-predial-hook]
exten => s,1,Set(CUSTOM_FROM_SIP_HEADER=${PJSIP_HEADER(read,From)})
exten => s,n,Set(CUSTOM_FROM_DISPLAYNAME=${PJSIP_PARSE_URI_FROM(CUSTOM_FROM_SIP_HEADER,display)})

For example, I have the following From and Contact header in the INVITE:

From: "Test456" <sip:[email protected]>;tag=cb13bd4de9b3473da8c110e2ef3b5d68
Contact: <sip:[email protected]:62627;ob>;expires=299

However, when I try to place a call and see the result of my code, I get:

[2025-02-24 08:24:03] VERBOSE[5417][C-00000001] pbx.c: Executing [s@macro-dialout-trunk-predial-hook:1] Set("PJSIP/1000-00000000", "CUSTOM_FROM_SIP_HEADER="Test456" <sip:[email protected]>;tag=fe91110cd42840cb8798b65759226559") in new stack
[2025-02-24 08:24:03] WARNING[1907] pjsip/dialplan_functions.c: Failed to parse URI '"Test456" <sip:[email protected]>;tag=fe91110cd42840cb8798b65759226559'
[2025-02-24 08:24:03] VERBOSE[5417][C-00000001] pbx.c: Executing [s@macro-dialout-trunk-predial-hook:2] Set("PJSIP/1000-00000000", "CUSTOM_FROM_DISPLAYNAME=") in new stack

Could someone help me understand my mistake?
I would like to have the display name in a custom variable to be further used. I see there are also functions to work with the Contact header.

You haven’t dereferenced the variable name in the second line (no ${}).

Why does ${CALLERID(name)} not meet your requirements?

1 Like

I found it out: apparently the PJSIP_PARSE_URI_FROM function doesn’t like the “;tag=…” part of the From URI. This should fix it:

exten => s,n,Set(CUSTOM_FROM_SIP_HEADER=${PJSIP_HEADER(read,From)})
exten => s,n,Set(CUSTOM_FROM_SIP_HEADER_CUT=${CUT(CUSTOM_FROM_SIP_HEADER,\;,1)})
exten => s,n,Set(CUSTOM_FROM_DISPLAY=${PJSIP_PARSE_URI_FROM(CUSTOM_FROM_SIP_HEADER_CUT,display)})

Now I don’t have the issue anymore. My final aim was to provide a way extensions could modify their Caller ID on the fly via the Display Name variable that can be set on endpoints.

Thank you all

The tag is not part of the URI, nor is the display name. The URI is the bit in angle brackets.

True, but according to the documentation the Display Name is one of the parameters you can read using the function.