Hi Inxjenn,
I will try to explain some of the features you can use.
Dial plan sends the event to xmpp plugin script
;read 4 digits customer id, send request and continue execution
exten => s,1,Answer()
same => n,Read(customer_id,,4)
same => n,UserEvent(ScriptUser,script: "custom_support",data: "{\"customer_id\":\"${customer_id}\"}")
same => n,Return()
Dial plan sends the HTTP POST request to xmpp plugin script
;read 4 digits customer id, send request and wait for response
exten => s,1,Answer()
same => n,Read(customer_id,,4)
same => n,GoSub(sub_curl,s,1(curl,post,http://localhost/services/pluginscript/custom_support/action,"{\"customer_id\":\"${customer_id}\"}"))
same => n,GoToIf($ "${HASH(curl,HTTP_CODE)}" != "200" ]?skip)
same => n,GoToIf($ "${AGISTATUS}" = "FAILURE" ]?skip)
same => n,Set(CURL_RESPONSE=${HASH(curl,CONTENT)})
;in my example CURL_RESPONSE will look like 0049112233^PLATINUM
same => n,Set(CUSTOMER_NUMBER=${CUT(CURL_RESPONSE,^,1)})
same => n,Set(CUSTOMER_VIP=${CUT(CURL_RESPONSE,^,2)})
same => n,Return()
same => n(skip),Return()
Server plugin script (Advanced/Scripts) handles dial plan requests and notfies third party API
import groovy.json.JsonSlurper
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.*
import net.pascom.ahab.cmd.script.ScriptEvent
import javax.ws.rs.core.*
/*
Dialplan invokes custom script and waits for response. Sync method
*/
script.onRestPost = { httpRequest ->
def jsonBody = new JsonSlurper().parseText(httpRequest.getInputStream().getText())
def customerId = jsonBody.get('customer_id')
if (!customerId) return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity('{"error":"customer_id param is missing"}').build()
def customer = findCustomer(customerId)
if (!customer) return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity('{"error":"invalid customer with id $customerId"}').build()
def customerNumber = customer'number']
def customerSLA = customer'sla']
String customerDetails = "$customerNumber^$customerSLA"
return Response.ok(customerDetails).build();
}
/*
ScriptUserEvent received from the dialplan. Dialplan does not block. Async method
*/
script.onManagerEvent_ScriptUserEvent = { event ->
if(event.script!=getName()) return
def jsonBody = new JsonSlurper().parseText(event.data)
def customerId = jsonBody.get('customer_id')
if (!customerId) return
def customer = findCustomer(customerId)
if (!customer) return
pushCustomerDetails(customer)
}
/*
User REST to fetch details from third part system
*/
def findCustomer(String customerId) {
def result
try {
def client = new HTTPBuilder("http://apitest.com")
client.request(POST,JSON) { req->
uri.path = "/api/test"
uri.query = "customer":customerId]
response.success = { resp,json ->
result = json'customer']
}
response.failure= { resp ->
script.log.error " ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
}
}
} catch (Exception e) {
log.error e.message
}
return result
}
/*
Push the data to all xmpp clients with proper subscription
Xmpp client should add the subscription like specified. User need to have supervisor role
<iq id="7HY4b-73" type="get">
<cmd xmlns="http://www.pascom.net/mobydick" module="event">
<AddSubscription>
<Subscription module="script" type="ScriptEvent" scope="supervisor">
<AttributeFilter name="script" equals="custom_support"/>
</Subscription>
</AddSubscription>
</cmd>
</iq>
*/
void pushCustomerDetails(def customer) {
ScriptEvent event = new ScriptEvent()
event.setScript(script.getName())
event.setBody(customer)
try {
script.getBean("eventManager").publishEvent('', event)
} catch (Exception e) {
log.error "Can't send script event ${e.getMessage()}"
}
}
The provided code is not tested. I hope those code snippets basically satisfies your requirements.
I look forward your feedback. If you require any further information, feel free to contact me
Kind regards,
Stefan Tosic