Last active
July 15, 2020 15:42
-
-
Save spasiu/74dbb414c1dead203232fbd2d8241676 to your computer and use it in GitHub Desktop.
switchboard bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const APP_ID = ''; | |
const KEY_ID = ''; | |
const SECRET = ''; | |
const author = { | |
role: 'appMaker', | |
name: 'Bot', | |
avatarUrl: 'https://www.centurybizsolutions.net/wp-content/uploads/2013/09/switchboard-operator.png' | |
}; | |
express() | |
.use(bodyParser.json()) | |
.post('/bot_events', async function(req, res) { | |
console.log(JSON.stringify(req.body, null, 4)); | |
for (const event of req.body.events) { | |
try { | |
if (event.payload.message.author.role === 'user') { | |
if (event.payload.message.content.payload === 'SUPPORT') { | |
await sunco('post', `/conversations/${event.payload.conversation.id}/messages`, { | |
author, | |
content: { | |
type: 'text', | |
text: '%((switchboard:passControl:agent))%' | |
} | |
}); | |
await sunco('post', `/conversations/${event.payload.conversation.id}/messages`, { | |
author, | |
content: { | |
type: 'text', | |
text: `OK, I've connected you to support. Please describe your issue!` | |
} | |
}); | |
} else { | |
await sunco('post', `/conversations/${event.payload.conversation.id}/messages`, { | |
author, | |
content: { | |
type: 'text', | |
text: `Hi. It's me again. I can help you: %[check balance](reply:BALANCE) %[top up](reply:TOPUP) %[shop](reply:SHOP) %[talk to support](reply:SUPPORT)` | |
} | |
}); | |
} | |
} | |
} catch(error) { | |
console.log('ERROR /bot_events >>>', error.message, error.response); | |
} | |
} | |
res.end(); | |
}) | |
.post('/start_events', async function(req, res) { | |
try { | |
await sunco('post', `/conversations/${req.body.conversation._id}/messages`, { | |
author, | |
content: { | |
type: 'text', | |
text: `Hi! I'm the Demo Bot. Let me know how I can help you! %[check balance](reply:BALANCE) %[top up](reply:TOPUP) %[shop](reply:SHOP) %[talk to support](reply:SUPPORT)` | |
} | |
}); | |
} catch(error) { | |
console.log('ERROR /start_events >>>', error.message, error.response); | |
} | |
res.end(); | |
}) | |
.get('/', (req, res) => res.send(` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Switchboard Demo</title> | |
<link href="style.css" rel="stylesheet" type="text/css" /> | |
<style> | |
#bg { | |
z-index: -1000; | |
position: fixed; | |
top: 0; | |
left: 0; | |
min-width: 100%; | |
min-height: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<img src="http://4.bp.blogspot.com/-JZIv0_a-6e0/VHSjQCbPGtI/AAAAAAABHYM/fvWeipzSb6A/s1600/Women%2BTelephone%2BOperators%2Bat%2BWork%2B(18).jpg" id="bg" alt=""> | |
<script> | |
!function(e,n,t,r){ | |
function o(){try{var e;if((e="string"==typeof this.response?JSON.parse(this.response):this.response).url){ | |
var t=n.getElementsByTagName("script")[0],r=n.createElement("script");r.async=!0,r.src=e.url,t.parentNode.insertBefore(r,t)}}catch(e){}} | |
var s,p,a,i=[],c=[];e[t]={init:function(){s=arguments;var e={then:function(n){return c.push({type:"t",next:n}),e},catch:function(n){ | |
return c.push({type:"c",next:n}),e}};return e},on:function(){i.push(arguments)},render:function(){p=arguments},destroy:function(){a=arguments}}, | |
e.__onWebMessengerHostReady__=function(n){if(delete e.__onWebMessengerHostReady__,e[t]=n,s)for(var r=n.init.apply(n,s),o=0;o<c.length;o++){ | |
var u=c[o];r="t"===u.type?r.then(u.next):r.catch(u.next)}p&&n.render.apply(n,p),a&&n.destroy.apply(n,a);for(o=0;o<i.length;o++)n.on.apply(n,i[o])}; | |
var u=new XMLHttpRequest;u.addEventListener("load",o),u.open("GET","https://"+r+".webloader.smooch.io/",!0),u.responseType="json",u.send() | |
}(window,document,"Smooch","5f05e92cd2ab27000ce8a9dc"); | |
Smooch.init({ | |
integrationId: '5f05e92cd2ab27000ce8a9dc', | |
businessName: 'Switchboard Demo', | |
businessIconUrl: 'https://www.centurybizsolutions.net/wp-content/uploads/2013/09/switchboard-operator.png', | |
integrationOrder: [], | |
customText: { | |
introductionText: 'We\\'re here to route your conversations!', | |
introAppText: '' | |
} | |
}).then(() => { | |
Smooch.startConversation(); | |
Smooch.open(); | |
}); | |
</script> | |
</body> | |
</html> | |
`)) | |
.listen(8000); | |
async function sunco(method, path, data) { | |
return axios({ | |
method, data, | |
url: `https://api.smooch.io/v2/apps/${APP_ID}${path}`, | |
auth: { | |
username: KEY_ID, | |
password: SECRET | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const Smooch = require('smooch-core'); | |
const CFC_ID = ''; // webhook for CFC | |
const APP_ID = ''; | |
const KEY_ID = ''; | |
const SECRET = ''; | |
const SERVICE_URL = ''; // ngrok or whatever | |
const smooch = new Smooch({ | |
scope: 'account', | |
keyId: KEY_ID, | |
secret: SECRET | |
}); | |
function sunco(method, path, data) { | |
return axios({ | |
method, data, | |
url: `https://api.smooch.io/v2/apps/${APP_ID}${path}`, | |
auth: { | |
username: KEY_ID, | |
password: SECRET | |
} | |
}); | |
} | |
(async function() { | |
try { | |
await smooch.webhooks.create(APP_ID, { | |
target: `${SERVICE_URL}/start_events`, | |
triggers: ['conversation:start'] | |
}) | |
const resCreatSwitch = await sunco('post', '/switchboards'); | |
const resCreatCustInt = await sunco('post', '/integrations', { | |
type: 'custom', | |
webhook: { | |
target: `${SERVICE_URL}/bot_events`, | |
triggers: ['conversation:message', 'switchboard:passControl'] | |
} | |
}); | |
const resCreatSwitchIntAgent = await sunco('post', `/switchboards/${resCreatSwitch.data.switchboard.id}/switchboardIntegrations`, { | |
name: 'agent', | |
integrationId: CFC_ID, | |
deliverStandbyEvents: false | |
}); | |
const resCreatSwitchIntBot = await sunco('post', `/switchboards/${resCreatSwitch.data.switchboard.id}/switchboardIntegrations`, { | |
name: 'bot', | |
integrationId: resCreatCustInt.data.integration.id, | |
deliverStandbyEvents: false | |
}); | |
await sunco('patch', `/switchboards/${resCreatSwitch.data.switchboard.id}`, { | |
enabled: true, | |
defaultSwitchboardIntegrationId: resCreatSwitchIntBot.data.switchboardIntegration.id | |
}); | |
} catch (error) { | |
console.log(error.message, error.response); | |
const responseSwitchboards = await sunco('get', '/switchboards'); | |
for (const switchboard of responseSwitchboards.data.switchboards) { | |
const responseSwitchboardIntegrations = await sunco('get', `/switchboards/${switchboard.id}/switchboardIntegrations`); | |
for (const switchboardIntegration of responseSwitchboardIntegrations.data.switchboardIntegrations) { | |
await sunco('delete', `/switchboards/${switchboard.id}/switchboardIntegrations/${switchboardIntegration.id}`); | |
console.log('deleted switchboard integrations', switchboardIntegration.id); | |
} | |
await sunco('delete', `/switchboards/${switchboard.id}`); | |
console.log('deleted switchboard', switchboard.id); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment