I finally figured it out, this is my final DIAL plan with the desired logic:
[from-cloud]
exten => _X.,1,NoOp(Incoming call from cloud to ${EXTEN})
same => n,Verbose(1, *** Extracting DID from From Header ***)
same => n,Set(RAW_FROM_HEADER=${PJSIP_HEADER(read,From)}) ; Use PJSIP_HEADER to extract the From header
same => n,Verbose(1, From Header: ${RAW_FROM_HEADER})
; Extract only the phone number from the From header using regex
same => n,Set(CALLER_DID=${CUT(CUT(RAW_FROM_HEADER,"<sip:",2),@,1)})
same => n,Set(CALLER_DID=${CUT(CALLER_DID,:,2)}) ; Removes the 'sip:' prefix if it exists
same => n,Verbose(1, Extracted Caller DID: ${CALLER_DID})
; Route based on the extracted DID
same => n,GotoIf($["${CALLER_DID}" = "+256772100100"]?gsm_endpoint,${EXTEN},1)
same => n,GotoIf($["${CALLER_DID}" = "+256772100101"]?gsm2_endpoint,${EXTEN},1)
same => n,GotoIf($["${CALLER_DID}" = "+256772100111"]?gsm3_endpoint,${EXTEN},1)
same => n,GotoIf($["${CALLER_DID}" = "+256772100112"]?gsm4_endpoint,${EXTEN},1)
same => n,GotoIf($["${CALLER_DID}" = "+256772100115"]?gsm5_endpoint,${EXTEN},1)
same => n,GotoIf($["${CALLER_DID}" = "+256772100118"]?gsm6_endpoint,${EXTEN},1)
same => n,Verbose(1, No matching DID found. Hanging up.)
same => n,Hangup()
[gsm_endpoint]
exten => _X.,1,Dial(PJSIP/${EXTEN}@gsm_endpoint)
[gsm2_endpoint]
exten => _X.,1,Dial(PJSIP/${EXTEN}@gsm2_endpoint)
[gsm3_endpoint]
exten => _X.,1,Dial(PJSIP/${EXTEN}@gsm3_endpoint)
[gsm4_endpoint]
exten => _X.,1,Dial(PJSIP/${EXTEN}@gsm4_endpoint)
[gsm5_endpoint]
exten => _X.,1,Dial(PJSIP/${EXTEN}@gsm5_endpoint)
[gsm6_endpoint]
exten => _X.,1,Dial(PJSIP/${EXTEN}@gsm6_endpoint)
I had to work with data from the INVITE to extract the DID attached to a specific gsm endpoint then have the call processed accordingly.