dailpad_cti
Version:
228 lines (196 loc) • 7.79 kB
JavaScript
var dPad = new(function () {
var webhookId = 0;
var call_Id = "";
//var agentId = "";
//var plugin = new Object();
this.webhook = function () {
console.error("webhook funcation called Fetching Existing Webhooks...");
var webhookList = [];
//var hookUrl = 'https://novelvox-dev.express-scripts.ca/runner?module=eclipse&cti=Dialpad_CTI';
//var hookUrl = 'http://10.127.83.201:8085/hook';
var hookUrl = 'https://80dd-199-249-177-123.ngrok.io';
//get webhook list
const webhookListoptions = {
method: 'GET',
headers: {
accept: 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
}
};
fetch('https://dialpad.com/api/v2/webhooks', webhookListoptions)
.then(response => response.json())
.then(response => {
if (Object.keys(response).includes('items'))
webhookList = response.items;
if (webhookList.length == 0) {
console.error("No Webhook Found! Creating a new one...");
this.registerWebhook(hookUrl);
} else {
var temp = 0;
for (let item in webhookList) {
if (webhookList[item].hook_url.toString() == hookUrl) {
console.error("webhook already registered...");
temp = 1;
}
}
if(temp == 0){
console.error("HookUrl Not Matched, Creating a new webhook...");
this.registerWebhook(hookUrl);
}
}
})
.catch(err => console.error(err));
};
this.registerWebhook = function (hookUrl) {
console.error("Register webhook funcation called...");
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
},
body: JSON.stringify({hook_url: hookUrl})
};
// var webhookId;
var data;
fetch('https://dialpad.com/api/v2/webhooks', options)
.then(response => response.json())
.then(response => {
if (response.id) {
console.error("New Webhook Created, response = ", response);
webhookId = response.id;
this.callState();
this.agent();
}
data = response;
// response.send(webhookId)
console.error("webhookId = ", webhookId);
})
.catch(err => console.error("Error = ", err));
}
this.callState = function () {
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
},
body: JSON.stringify({
call_states: [
'all'
],
enabled: true,
webhook_id: webhookId
})
};
fetch('https://dialpad.com/api/v2/subscriptions/call', options)
.then(response => response.json())
.then(response => console.error("Call API => ", response))
.catch(err => console.error(err));
};
this.agent = function () {
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
},
body: JSON.stringify({
agent_type: 'callcenter',
enabled: true,
webhook_id: webhookId
})
};
fetch('https://dialpad.com/api/v2/subscriptions/agent_status', options)
.then(response => response.json())
.then(response => {
console.error("AgentStatus API => ",response);
// agentId = response.id;
})
.catch(err => console.error(err));
};
this.stateChange = function () {
const options = {
method: 'PATCH',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
},
body: JSON.stringify({
duty_status_reason: 'Email Replies',
on_duty: false
})
};
var url = 'https://dialpad.com/api/v2/callcenters/operators/';
agentId = '5790140257632256';
fetch(url + agentId + '/dutystatus', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
};
this.callInitiate = function () {
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
},
body: JSON.stringify({
phone_number: '+8031000117837',
user_id: 6187409389355008
})
};
fetch('https://dialpad.com/api/v2/call', options)
.then(response => response.json())
.then(response => {
console.error(response);
call_Id = response.call_id;
// response.send(webhookId)
console.error(call_Id);
})
.catch(err => console.error(err));
//return data;
};
this.callTransfer = function () {
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer nwS9vg9g8Rr9uJSa8rqa494wzRtPGkkv5jXsRupQC72PWzuNCwxZKZKyeQ3wDjJtkM9BGERAd6bRW8VJdreFCLUKLdmrTfwkEa44'
},
body: JSON.stringify({
transfer_to_number: '+8031000118298'
})
};
fetch('https://dialpad.com/api/v2/call/5954916091265024/transfer', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
}
this.WebSocketTest =function() {
if ("WebSocket" in window) {
var ws = new WebSocket('ws://10.127.83.201:8084');
ws.onopen = function() {
console.error("websocket connection established with webhook");
ws.send("agentId:5790140257632256");
console.error("Testing >>>");
}
ws.onmessage = function (evt) {
console.error("Message event => ",evt);
var received_msg = evt.data;
console.error("Message Recieved in loginjs from webhook", evt.data);
};
ws.onclose = function() {
console.error("Websocket Connection is closed...");
};
}else {
alert("WebSocket NOT supported by your Browser!");
};
}
})();