Re: create an app instance and read parameters
Hello Daniel,
there is no current official API provided by AP Manager for this use case.
You could still use the UI API available in the AP Manager that is used the user interface.
You app must know the manager password, to be able to authenticate, or it could use the Services API in the PBX to obtain login data for the Manager via GetServiceLogin. For this case, your app must have access to the "apps" app.
Below you can find an example that connects to manager API via Services API. However it uses not UI API, but one for PBX Manager Plugins.
Best Regards
Andreas Fink
var servicesApi = null;
var managerUiApi = null; // TODO
var managerApi = null;
new PbxApi("Services").onconnected(function (conn) {
log("Services API connected");
servicesApi = conn;
servicesApi.send(JSON.stringify({ api: "Services", mt: "SubscribeServices" }));
servicesApi.onclose(function () {
log("Services API connection closed");
});
servicesApi.onmessage(function (msg) {
var obj = JSON.parse(msg);
if (obj.mt === "ServicesInfo") {
// TODO check for empty array
var url = obj.services.filter(function (v) { return v.name === "ap" })[0].url; // alternative "apps" for managerUiApi
if (url) {
log(url);
var wsurl = url.startsWith("http") ? url.replace("http", "ws") : url;
// "/manager/manager" for managerUiApi
// "/manager/manager-api" backend API (used by client API com.innovaphone.manager via innovaphone.manager.api.js)
wsurl = wsurl.endsWith("manager-domain-api") ? wsurl.replace("/manager/manager-domain-api", "/manager/manager-api") : wsurl;
log("Connecting to AP Manager via URL " + wsurl);
managerApi = AppWebsocketClient.connect(wsurl, null, "ap");
managerApi.onauth(function (apconn, app, challenge) {
// get login parameters asynchronously (e.g. using Services API) and then call the auth function
servicesApi.send(JSON.stringify({ api: "Services", mt: "GetServiceLogin", app: "ap", challenge: challenge }));
});
managerApi.onopen(function (conn) {
log("Manager Connection established");
managerApi.send(JSON.stringify({ mt: "ServiceControls" }));
});
}
}
if (obj.mt === "GetServiceLoginResult") {
if (obj.error) {
log("login failed");
managerApi.close();
}
else {
// pass received login parameters to library
var key = servicesApi.decrypt(obj.salt, obj.key);
var info = JSON.stringify(obj.info);
managerApi.auth(obj.domain, obj.sip, obj.guid, obj.dn, obj.pbxObj, obj.app, info, obj.digest, key);
}
}
});
});
var managerUiApi = null; // TODO
var managerApi = null;
new PbxApi("Services").onconnected(function (conn) {
log("Services API connected");
servicesApi = conn;
servicesApi.send(JSON.stringify({ api: "Services", mt: "SubscribeServices" }));
servicesApi.onclose(function () {
log("Services API connection closed");
});
servicesApi.onmessage(function (msg) {
var obj = JSON.parse(msg);
if (obj.mt === "ServicesInfo") {
// TODO check for empty array
var url = obj.services.filter(function (v) { return v.name === "ap" })[0].url; // alternative "apps" for managerUiApi
if (url) {
log(url);
var wsurl = url.startsWith("http") ? url.replace("http", "ws") : url;
// "/manager/manager" for managerUiApi
// "/manager/manager-api" backend API (used by client API com.innovaphone.manager via innovaphone.manager.api.js)
wsurl = wsurl.endsWith("manager-domain-api") ? wsurl.replace("/manager/manager-domain-api", "/manager/manager-api") : wsurl;
log("Connecting to AP Manager via URL " + wsurl);
managerApi = AppWebsocketClient.connect(wsurl, null, "ap");
managerApi.onauth(function (apconn, app, challenge) {
// get login parameters asynchronously (e.g. using Services API) and then call the auth function
servicesApi.send(JSON.stringify({ api: "Services", mt: "GetServiceLogin", app: "ap", challenge: challenge }));
});
managerApi.onopen(function (conn) {
log("Manager Connection established");
managerApi.send(JSON.stringify({ mt: "ServiceControls" }));
});
}
}
if (obj.mt === "GetServiceLoginResult") {
if (obj.error) {
log("login failed");
managerApi.close();
}
else {
// pass received login parameters to library
var key = servicesApi.decrypt(obj.salt, obj.key);
var info = JSON.stringify(obj.info);
managerApi.auth(obj.domain, obj.sip, obj.guid, obj.dn, obj.pbxObj, obj.app, info, obj.digest, key);
}
}
});
});