first commit
This commit is contained in:
176
www/js/index.js
Normal file
176
www/js/index.js
Normal file
@@ -0,0 +1,176 @@
|
||||
var my_8x8_name = "Dave Umrysh";
|
||||
var hostUrl = "https://matrix.example.com"; // Location of your Matrix server
|
||||
var webhook_secret = "UGfmxJyv49Sus2Y32dE7juXYMvp"; // Random key you set in your sixtyfour matrix bot
|
||||
var instance_name = "sixtyfourinstance";
|
||||
|
||||
var android_id = "";
|
||||
var devicePlatform = "";
|
||||
|
||||
document.addEventListener('deviceready', onDeviceReady, false);
|
||||
|
||||
function onDeviceReady() {
|
||||
// Cordova is now initialized. Have fun!
|
||||
|
||||
console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
|
||||
document.getElementById('deviceready').classList.add('ready');
|
||||
|
||||
|
||||
android_id = device.uuid;
|
||||
devicePlatform = device.platform;
|
||||
|
||||
cordova.plugins.backgroundMode.on('activate', function() {
|
||||
cordova.plugins.backgroundMode.disableWebViewOptimizations();
|
||||
});
|
||||
cordova.plugins.backgroundMode.enable();
|
||||
cordova.plugins.backgroundMode.disableBatteryOptimizations();
|
||||
|
||||
// Set up the GCM
|
||||
setUpGCM();
|
||||
|
||||
startListening();
|
||||
}
|
||||
|
||||
function startListening(){
|
||||
|
||||
replyToNotification.listen(function(n){
|
||||
console.log("Received notification " + JSON.stringify(n) );
|
||||
|
||||
if(n.package == "org.vom8x8.sipua"){
|
||||
// Check for rooms
|
||||
if(n.title == my_8x8_name){
|
||||
// This was my message out
|
||||
// Post to the bot channel
|
||||
var who_sent_it = n.title.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"');
|
||||
var params = JSON.stringify({ "secret": webhook_secret, "message": "**"+who_sent_it+"**: "+n.text.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"') });
|
||||
postToMatrix(hostUrl+"/_matrix/maubot/plugin/"+instance_name+"/webhook/r0?room=%21aaaaaaaaaaaaaaaaaa:matrix.example.com",params);
|
||||
}else if(n.title == "John Smith"){
|
||||
var who_sent_it = n.title.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"');
|
||||
var params = JSON.stringify({ "secret": webhook_secret, "message": "**"+who_sent_it+"**: "+n.text.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"') });
|
||||
postToMatrix(hostUrl+"/_matrix/maubot/plugin/"+instance_name+"/webhook/r0?room=%21bbbbbbbbbbbbbbbbbb:matrix.example.com",params);
|
||||
|
||||
|
||||
// Add other DM rooms here
|
||||
|
||||
|
||||
|
||||
}else if(n.conversationTitle == "Room #1"){
|
||||
var who_sent_it = n.title.replaceAll("Room #1: ","").replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"');
|
||||
var params = JSON.stringify({ "secret": webhook_secret, "message": "**"+who_sent_it+"**: "+n.text.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"') });
|
||||
postToMatrix(hostUrl+"/_matrix/maubot/plugin/"+instance_name+"/webhook/r0?room=%21ccccccccccccccccc:matrix.example.com",params);
|
||||
|
||||
|
||||
// Add other rooms here
|
||||
|
||||
|
||||
}else{
|
||||
var who_sent_it = n.title.replaceAll("All Company: ","").replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"');
|
||||
var params = JSON.stringify({ "secret": webhook_secret, "message": "**"+who_sent_it+"**: "+n.text.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"') });
|
||||
postToMatrix(hostUrl+"/_matrix/maubot/plugin/"+instance_name+"/webhook/r0?room=%21dddddddddddddddd:matrix.example.com",params);
|
||||
}
|
||||
}
|
||||
|
||||
}, function(e){
|
||||
console.log("Notification Error " + e);
|
||||
});
|
||||
}
|
||||
|
||||
function postTo8x8(room,message){
|
||||
var room_name = "";
|
||||
|
||||
if(room == "!bbbbbbbbbbbbbbbbbb:matrix.example.com"){
|
||||
room_name = "John Smith";
|
||||
|
||||
// Add other DM rooms here
|
||||
|
||||
|
||||
}else if(room == "!ccccccccccccccccc:matrix.example.com"){
|
||||
room_name = "Room #1";
|
||||
|
||||
|
||||
// Add other rooms here
|
||||
|
||||
|
||||
}else if(room == "!dddddddddddddddd:matrix.example.com"){
|
||||
room_name = "All Company";
|
||||
}
|
||||
|
||||
if(room_name != ""){
|
||||
var temp = {
|
||||
"room_name":room_name,
|
||||
"message":message
|
||||
};
|
||||
replyToNotification.replytonotification(temp, function (s) {
|
||||
// Send errors to the bot channel
|
||||
console.log(s);
|
||||
|
||||
var params = JSON.stringify({ "secret": webhook_secret, "message": "**8x8 Error**: "+s.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","").replaceAll('"','\"') });
|
||||
postToMatrix(hostUrl+"/_matrix/maubot/plugin/"+instance_name+"/webhook/r0?room=%21aaaaaaaaaaaaaaaaaa:matrix.example.com",params);
|
||||
}, function (e) {
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function postToMatrix(url,message){
|
||||
var http = new XMLHttpRequest();
|
||||
http.open("POST", url, true);
|
||||
//Send the proper header information along with the request
|
||||
http.setRequestHeader("Content-type", "application/json");
|
||||
|
||||
//http.onreadystatechange = function() {
|
||||
// console.log(http.status+"|"+http.responseText);
|
||||
//};
|
||||
|
||||
http.send(message);
|
||||
console.log("sent message to matrix")
|
||||
}
|
||||
|
||||
|
||||
function setUpGCM(){
|
||||
if(typeof device !== 'undefined'){
|
||||
pushNotification = PushNotification.init({
|
||||
android: {},
|
||||
ios: {
|
||||
alert: "true",
|
||||
badge: "true",
|
||||
sound: "true",
|
||||
clearBadge: "true"
|
||||
}
|
||||
});
|
||||
|
||||
pushNotification.on('registration', function(data) {
|
||||
window.localStorage.setItem("gcm_key", data.registrationId);
|
||||
// Try to send it off
|
||||
sendGCMtoServer();
|
||||
});
|
||||
|
||||
pushNotification.on('notification', function(data) {
|
||||
// data.message,
|
||||
// data.title,
|
||||
// data.count,
|
||||
// data.sound,
|
||||
// data.image,
|
||||
// data.additionalData
|
||||
postTo8x8(data.title,data.message);
|
||||
});
|
||||
|
||||
pushNotification.on('error', function(e) {
|
||||
console.log("error: "+e.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function sendGCMtoServer(){
|
||||
var gcm_key = window.localStorage.getItem("gcm_key");
|
||||
|
||||
if(gcm_key == null || gcm_key == ""){
|
||||
setUpGCM();
|
||||
}
|
||||
|
||||
if(gcm_key != null && gcm_key != ""){
|
||||
// Send to server
|
||||
var params = JSON.stringify({ "secret": webhook_secret, "gcm": gcm_key });
|
||||
postToMatrix(hostUrl+"/_matrix/maubot/plugin/"+instance_name+"/webhook/r1?gcm",params);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user