Chat Echo Bot in Node.js
Explained
This is the same example as the other
chat echo bot example except that it is run inside the Node.js environment. This makes sense because it doesn't have a logical User Interface.
var IPCortex = require('ipcortex-pabx');
IPCortex.PBX.Auth.setHost('https://pabx.hostname');
IPCortex.PBX.Auth.login({
username: '<username>',
password: '<password>'
}).then(
function() {
console.log('Login successful');
IPCortex.PBX.startFeed().then(
function() {
console.log('Live data feed started');
IPCortex.PBX.enableChat(
function (ourRoom) {
ourRoom.addListener('update', function (room) {
room.messages.forEach(function (message) {
console.log(message.cN + ' has decreed: ' + message.msg);
if (!message.own)
room.post('Apparently you decreed: ' + message.msg);
});
});
}
);
},
function() {
console.log('Live data feed failed');
}
);
},
function() {
console.log('Login failed');
}
);
This is the complete application!
To try it out, run the following commands:
npm init
npm install --save ipcortex/node-api
node node_modules/ipcortex-pabx/updateAPI.js http://pabx.hostname
Then save the code above in the root folder of your npm project and run it with
node
(e.g.
node index.js
). You should receive all the appropriate success messages.
Test this in the same way as the browser-hosted version.
NOTE: if you are using self-signed certificates with your IPCortex Communication System and receiving errors then set the NODE_TLS_REJECT_UNAUTHORIZED
environment variable to 0. However, please don't use this in anything near a production system as this is essentially turning off security.