~singpolyma/jmp-dialplan

26c1e3244c03914aadfc10934310598927a561cf — Stephen Paul Weber 4 months ago 9d8a4ff
Ask sgx-jmp what to do instead of reading Redis directly
1 files changed, 23 insertions(+), 14 deletions(-)

M extensions.lua
M extensions.lua => extensions.lua +23 -14
@@ 1,6 1,5 @@
local redis = (require "redis").connect("127.0.0.1", 6380)

local JMP_CATAPULT_U_V2 = "REDACTED"
local http_request = require "http.request"
local cjson = require "cjson"

function hex_to_char(x)
	return string.char(tonumber(x, 16))


@@ 10,6 9,21 @@ function uri_decode(uri)
	return uri:gsub("%%(%x%x)", hex_to_char)
end

function call_sgx_jmp(from_jid, to)
	local req = http_request.new_from_uri(
		"https://bandwidth-voice.jmp.chat/outbound/calls"
	)
	req.headers:upsert(":method", "POST")
	req.headers:upsert("Content-Type", "application/json")
	req.headers:upsert("Accept", "application/json")
	req:set_body(cjson.encode({
		from = "xmpp:" .. from_jid,
		to = to
	}))
	local headers, stream = assert(req:go())
	return cjson.decode(stream:get_body_as_string())
end

extensions = {
	jmp = {
		["_+1NXXNXXXXXX"] = function(context, extension)


@@ 19,18 33,13 @@ extensions = {
				local from_header = channel.SIP_HEADER("From"):get()
				local from = from_header:gsub("^[^<]*<sip:", ""):gsub(">.*$", "")
				local from_jid = uri_decode(from:sub(0, from:find("@sip.cheogram.com") - 1))
				local customer_id = redis:get("jmp_customer_id-" .. from_jid)
				local catapult_cred = redis:lrange("catapult_cred-" .. from_jid, 0, 3)
				if customer_id then
					channel.CDR("customer_id"):set(customer_id)

					if not catapult_cred or not catapult_cred[1] then
						catapult_cred = redis:lrange("catapult_cred-customer_" .. customer_id .. "@jmp.chat", 0, 3)
					end
				end
				if catapult_cred and catapult_cred[1] and catapult_cred[1] == JMP_CATAPULT_U_V2 then
					channel.CALLERID("all"):set(catapult_cred[4] .. " <" .. catapult_cred[4] .. ">")
					app.dial("SIP/bandwidth/" .. extension)
				local result = call_sgx_jmp(from_jid, extension)
				channel.CDR("customer_id"):set(result.customer_id)

				if result.from and result.to then
					channel.CALLERID("all"):set(result.from .. " <" .. result.from .. ">")
					app.dial("SIP/bandwidth/" .. result.to)
				else
					app.log("NOTICE", "JMP rejected call from " .. from .. " (" .. from_jid .. "): no caller id found")
				end