tSIP: using custom fields from SIP INVITE

Lua function GetCallInitialRxInvite() returns full text of initial INVITE received for incoming call. This allows to extend funcionality by extracting and using custom INVITE fields like passing extra phone numbers (call transfer or redirection). Extracted value can be presented e.g. as programmable button caption ("on call state" event script):

function string.starts(String, Start)
   return string.sub(String,1,string.len(Start))==Start
end

function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

callState = GetCallState()
if callState == 1 then	-- CALL_INCOMING
	local invite = GetCallInitialRxInvite()
	local needle = "USER-AGENT:"
	
	-- clear button caption in case line is not found
	SetButtonCaption(0, "")	-- first, fixed column on dialpad
	SetButtonCaption(15, "")	-- first column of the sidecar	
	
	-- print("\nSCRIPT RX INVITE:\n" .. invite)
	for line in invite:gmatch"[^\n]+" do	-- extract each line
		if string.starts(string.upper(line), needle) then
			lineVal = trim(line:sub(string.len(needle)+1))
			SetButtonCaption(0, lineVal)	-- first, fixed column on dialpad
			SetButtonCaption(15, lineVal)	-- first column of the sidecar
			break
		end
	end
end
Extracting number from Remote-Party-ID and displaying number and description as button captions ("on call state" event script):
function string.starts(String, Start)
   return string.sub(String,1,string.len(Start))==Start
end

function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

callState = GetCallState()
if callState == 1 then	-- CALL_INCOMING
	local invite = GetCallInitialRxInvite()
	local needle = "REMOTE-PARTY-ID:"
	
	-- clear button caption in case line is not found
	SetButtonCaption(0, "")	-- first, fixed column on dialpad
	SetButtonCaption(1, "")
	
	-- print("\nSCRIPT RX INVITE:\n" .. invite)
	for line in invite:gmatch"[^\n]+" do	-- extract each line
		if string.starts(string.upper(line), needle) then
			lineVal = trim(line:sub(string.len(needle)+1))
			local number = string.match(lineVal, "sip:(.-)@")
			if number == nil then
				break
			end
			local description = GetContactName(number)
			SetButtonCaption(0, number)	-- first, fixed column on dialpad
			SetButtonCaption(1, description)
			break
		end
	end
end

Back to howto list.


 "Cookie monsters": 3150005    Parse time: 0.000 s