|
//DEBUG=* node server.js |
|
var xmpp = require('simple-xmpp'), |
|
http = require('http'), |
|
fs = require('fs'), |
|
path = require('path'), |
|
logger = require('log4js'), |
|
sparkMessage = require('./myutils/sparkMethods'), |
|
os = require('os'); |
|
//Console logging to file |
|
logger.configure({ |
|
appenders: [ |
|
{type:'console'}, |
|
{type: 'file', filename: 'logs/application.log', catergory:'application'} |
|
], |
|
replaceConsole: true |
|
}); |
|
//Spark and bot tokens |
|
var sparkToken = "<your Spark Token"; |
|
var to = '<Your email>'; |
|
var botToken = '<optional Bot token'; |
|
//Jabber Text messages |
|
var jabberText = "Thank you for your message but I have moved to Cisco Spark "+ |
|
"as my primary contact application.\n"+ |
|
"I have automatically moved our conversation to Cisco Spark. "+ |
|
"If you have never used Spark before you should see an email invitation shortly. "+ |
|
"Also make sure to check out https://developer.ciscospark.com/ & https://www.ciscospark.com/ ."+ |
|
"To download Spark https://www.ciscospark.com/downloads.html. "+ |
|
"Oh and BTW don't forget my blog http://voipnorm.blogspot.com.\n"+ |
|
"See you in Spark...\n"; |
|
var jabberActiveText = "Thank you for your message but I have moved to Cisco Spark "+ |
|
"as my primary contact application.\n"+ |
|
"I have automatically moved our conversation to Cisco Spark and I am currently active which is the same as saying I am online. "+ |
|
"If you have never used Spark before you should see an email invitation shortly. "+ |
|
"Also make sure to check out https://developer.ciscospark.com/ & https://www.ciscospark.com/ ."+ |
|
"To download Spark https://www.ciscospark.com/downloads.html. "+ |
|
"Oh and BTW don't forget my blog http://voipnorm.blogspot.com.\n"+ |
|
"See you in Spark...\n"; |
|
var jabberDNDText = "********Do Not Disturb Alert***********\n"+ |
|
"Thank you for your message but I have moved to Cisco Spark "+ |
|
"as my primary contact application.\n"+ |
|
"I have automatically moved our conversation to Cisco Spark and I am in do not disturb right now so expect a delay. "+ |
|
"If you have never used Spark before you should see an email invitation shortly. "+ |
|
"Also make sure to check out https://developer.ciscospark.com/ & https://www.ciscospark.com/ ."+ |
|
"To download Spark https://www.ciscospark.com/downloads.html. "+ |
|
"Oh and BTW don't forget my blog http://voipnorm.blogspot.com.\n"+ |
|
"See you in Spark...\n"; |
|
var jabberOooText = "********Out of Office Alert***********\n"+ |
|
"Thank you for your message but I have moved to Cisco Spark "+ |
|
"as my primary contact application.\n"+ |
|
"I am currently out of the office so my responses may be delayed.\n"+ |
|
"I have automatically moved our conversation to Cisco Spark. "+ |
|
"If you have never used Spark before you should see an email invitation shortly. "+ |
|
"Also make sure to check out https://developer.ciscospark.com/ & https://www.ciscospark.com/ ."+ |
|
"To download Spark https://www.ciscospark.com/downloads.html. "+ |
|
"Oh and BTW don't forget my blog http://voipnorm.blogspot.com.\n"+ |
|
"See you in Spark...\n"+ |
|
"********Out of Office Alert***********"; |
|
//Default Spark Status |
|
var currentStatus = 'inactive'; |
|
|
|
console.log(Date()+">>>>>>>NodeJS XMPP Jabber assistant bot..."); |
|
|
|
xmpp.on('online', function(data) { |
|
console.log(Date()+'Connected with JID: ' + data.jid.user); |
|
}); |
|
|
|
//Spark to Jabber Presence mapping |
|
function presenceUpdate(){ |
|
setInterval(function(){ |
|
sparkMessage.getSpMeStatus(sparkToken, function(status){ |
|
|
|
if(currentStatus=== status){ |
|
return console.log('No Change to status: ' +currentStatus); |
|
}else{ |
|
if(status === 'ooo'){ |
|
xmpp.setPresence('away', 'Out of Office, get me on Spark https://www.ciscospark.com/'); |
|
}else if(status === 'active'){ |
|
xmpp.setPresence('online', 'I am active on Spark https://www.ciscospark.com/'); |
|
}else if(status === 'inactive'){ |
|
xmpp.setPresence('away', 'Always available on Spark https://www.ciscospark.com/'); |
|
}else if(status === 'dnd'){ |
|
xmpp.setPresence('dnd', 'Ping me on Spark when active again https://www.ciscospark.com/'); |
|
}else{ |
|
xmpp.setPresence('away', 'Always available on Spark https://www.ciscospark.com/'); |
|
} |
|
} |
|
}) |
|
},60000); |
|
} |
|
presenceUpdate(); |
|
|
|
//service to monitor bot uptime |
|
function postCPUCheck(){ |
|
setInterval(function(){ |
|
var cpuText = 'This is an uptime check for Jabber Assistant: '+os.uptime(); |
|
|
|
sparkMessage.newMessage(botToken,cpuText,to) |
|
},8640000) |
|
} |
|
postCPUCheck(); |
|
//Return messages when some sends you a XMPP message |
|
xmpp.on('chat', function(from, message) { |
|
sparkMessage.getSpMeStatus(sparkToken, function(status){ |
|
if(status === 'ooo'){ |
|
var sparkText = "**Out of Office Alert**<br>"+ |
|
"Thank you for switching to **Cisco Spark** and taking part in the use of the Spark APIs. " + |
|
"If this is your first time using Cisco Spark make sure to let me know and I will run you though the best features.<br>"+ |
|
"Just to make things easy here is your original message from Jabber: <br>"+ |
|
"**"+from+ "**: "+message+"<br>"+ |
|
"**Out of Office Alert**"; |
|
|
|
console.log('%s says %s', from, message); |
|
|
|
xmpp.send(from,jabberOooText ); |
|
sparkMessage.newMessage(sparkToken,sparkText,from); |
|
}else if(status === 'active'){ |
|
var sparkText = "Thank you for switching to **Cisco Spark** and taking part in the use of the Spark APIs. " + |
|
"If this is your first time using Cisco Spark make sure to let me know and I will run you though the best features.</br>"+ |
|
"Just to make things easy here is your original message from Jabber: </br>"+ |
|
"**"+from+ "**: "+message; |
|
|
|
console.log('%s says %s', from, message); |
|
|
|
xmpp.send(from,jabberActiveText); |
|
sparkMessage.newMessage(sparkToken,sparkText,from); |
|
|
|
}else if(status === 'dnd'){ |
|
var sparkText = "**Do Not Disturb Alert**<br>"+ |
|
"Thank you for switching to **Cisco Spark** and taking part in the use of the Spark APIs. " + |
|
"I am currentlu in **do not disturb** mode so it might take me a little while ot get back to you."+ |
|
"If this is your first time using Cisco Spark make sure to let me know and I will run you though the best features.</br>"+ |
|
"Just to make things easy here is your original message from Jabber: </br>"+ |
|
"**"+from+ "**: "+message; |
|
|
|
console.log('%s says %s', from, message); |
|
|
|
xmpp.send(from,jabberDNDText ); |
|
sparkMessage.newMessage(sparkToken,sparkText,from); |
|
}else{ |
|
var sparkText = "Thank you for switching to **Cisco Spark** and taking part in the use of the Spark APIs. " + |
|
"If this is your first time using Cisco Spark make sure to let me know and I will run you though the best features.</br>"+ |
|
"Just to make things easy here is your original message from Jabber: </br>"+ |
|
"**"+from+ "**: "+message; |
|
|
|
console.log('%s says %s', from, message); |
|
|
|
xmpp.send(from,jabberText ); |
|
sparkMessage.newMessage(sparkToken,sparkText,from); |
|
}; |
|
}); |
|
}); |
|
xmpp.on('error', function(err) { |
|
console.error(err); |
|
console.error(err.stack); |
|
}); |
|
//Handles lost connectiosn with XMPP service |
|
xmpp.on('close', function() { |
|
console.log('connection has been closed!'); |
|
sparkMessage.newMessage(botToken,'XMPP Connection Closed',to); |
|
connect(); |
|
console.log('connection reopened'); |
|
sparkMessage.newMessage(botToken,'XMPP Connection Reopened',to); |
|
}); |
|
function connect(){ |
|
xmpp.connect({ |
|
jid : '<your JID/email>', |
|
password : '<your Passwrd>', |
|
host : 'c2s.cisco.com.webexconnect.com', |
|
port : 5222, |
|
prefered: 'PLAIN', |
|
reconnect: false |
|
}); |
|
} |
|
connect(); |