Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxfoo committed Apr 25, 2021
0 parents commit ed6e76f
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# odysee-simple-alerts-connector

This nodecg bundle listens for LBC tips from odysee hyperchat's and renders an alert using the simple-alerts bundle.

I will create a portable version containing node.js, nodecg and the 2 bundles for an easy setup some time this week.
188 changes: 188 additions & 0 deletions dashboard/panel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.monospace {
font-family: monospace;
}
</style>
</head>
<body>
<div id=odysee-wrap>
<p>
Hello, This bundle activates alerts when some one tips using odysee hyperchat.
</p>
<p>
To get started, go to your livestream and get its claim id(Not stream key) and paste it into claim id box.
</p>
<p>
Then add the name of the default alert that you set up in simple-alerts
</p>
<p>
Then start adding triggers, place the name of the alert to be activated and the trigger type into each trigger.
</p>
</div>
<form id="newTrigger">
<button type="submit">Add New Trigger</button>
</form>
<script>
const claim_id = nodecg.Replicant('claim_id');
const triggers = nodecg.Replicant('triggers');
const defaultTrigger = nodecg.Replicant('defaultTrigger');
const test = nodecg.Replicant('test');

const div = document.getElementById('odysee-wrap');
var form = document.createElement('form');
form.setAttribute('id', 'claim_id');
var claimid = document.createElement('label');
claimid.setAttribute('for', 'claim_id');
var claimidLabel = document.createTextNode("claim id: ");
claimid.appendChild(claimidLabel);
var claimidInput = document.createElement('input');
claimidInput.setAttribute('id', 'claimidInput');

var defaultAlert = document.createElement('label');
defaultAlert.setAttribute('for', 'defaultAlert');
var defaultLabel = document.createTextNode("Default Alert: ");
defaultAlert.appendChild(defaultLabel);
var defaultInput = document.createElement('input');
defaultInput.setAttribute('id', 'defaultInput');

var defaultSubmit = document.createElement('button');
defaultSubmit.setAttribute('type', 'submit');
defaultSubmit.setAttribute('id', 'defaultBtn');
var defaultBtnLabel = document.createTextNode("Submit");
defaultSubmit.appendChild(defaultBtnLabel);

var testAmount = document.createElement('label');
testAmount.setAttribute('for', 'testAmount');
var testLabel = document.createTextNode("Test Amount for Triggers: ");
testAmount.appendChild(testLabel);
var testInput = document.createElement('input');
testInput.setAttribute('id', 'testInput');

var testBtn = document.createElement('button');
testBtn.setAttribute('type', 'submit');
testBtn.setAttribute('id', 'testBtn');
var testBtnLabel = document.createTextNode("TEST");
testBtn.appendChild(testBtnLabel);

form.appendChild(claimid);
form.appendChild(claimidInput);
form.appendChild(document.createElement("br"));
form.appendChild(defaultAlert);
form.appendChild(defaultInput);
form.appendChild(document.createElement("br"));
form.appendChild(defaultSubmit);
form.appendChild(document.createElement("br"));
form.appendChild(testAmount);
form.appendChild(testInput);
form.appendChild(document.createElement("br"));
form.appendChild(testBtn);
form.appendChild(document.createElement("br"));
div.appendChild(form);

NodeCG.waitForReplicants(triggers, claim_id, defaultTrigger, test).then(() => {
document.getElementById('defaultInput').value = defaultTrigger.value;
document.getElementById('claimidInput').value = claim_id.value;
triggers.value.forEach(showPanels);
});

function showPanels(value, index, array) {

var form = document.createElement('form');
form.setAttribute('id', 'form' + index);
var alert = document.createElement('label');
alert.setAttribute('for', 'alert' + index);
var alertLabel = document.createTextNode("Alert Name: ");
alert.appendChild(alertLabel);
var alertInput = document.createElement('input');
alertInput.setAttribute('id', 'alertinput' + index);

var amount = document.createElement('label');
amount.setAttribute('for', 'amount' + index);
var amountLabel = document.createTextNode("Amount: ");
amount.appendChild(amountLabel);
var amountInput = document.createElement('input');
amountInput.setAttribute('id', 'amountinput' + index);

// Menu for logic type
var selectLogic = document.createElement('select');
selectLogic.setAttribute('id', 'selectLogic' + index);
var selectLogicLabel = document.createElement('label');
selectLogicLabel.setAttribute('for', 'selectLogic' + index);
selectLogicLabel.appendChild(document.createTextNode('Logic Type: '));

// Logic Type options
var logicOption = document.createElement('option');
logicOption.setAttribute('value', 'greaterthan');
var logicOptionLabel = document.createTextNode('Greater Than');
logicOption.appendChild(logicOptionLabel);
selectLogic.appendChild(logicOption);

var logicOption = document.createElement('option');
logicOption.setAttribute('value', 'equals');
var logicOptionLabel = document.createTextNode('equals');
logicOption.appendChild(logicOptionLabel);
selectLogic.appendChild(logicOption);

var logicSubmit = document.createElement('button');
logicSubmit.setAttribute('type', 'submit');
logicSubmit.setAttribute('id', 'logicBtn' + index);
var buttonLabel = document.createTextNode("Submit");
logicSubmit.appendChild(buttonLabel);
var logicDelete = document.createElement('button');
var deleteWrap = document.createElement('div');
logicDelete.setAttribute('id', 'deleteBtn' + index);
var deleteLabel = document.createTextNode("DELETE");
logicDelete.appendChild(deleteLabel);

form.appendChild(alert);
form.appendChild(alertInput);
form.appendChild(document.createElement("br"));
form.appendChild(selectLogicLabel);
form.appendChild(selectLogic);
form.appendChild(amount);
form.appendChild(amountInput);
form.appendChild(document.createElement("br"));
form.appendChild(logicSubmit);
form.appendChild(logicDelete);
form.appendChild(document.createElement("br"));
div.appendChild(form);

document.getElementById('alertinput' + index).value = value.name;
document.getElementById('amountinput' + index).value = value.amount;
document.getElementById('selectLogic' + index).value = value.type;

// Delete alert
document.getElementById('deleteBtn' + index).addEventListener('click', e => {
console.log("remove " + index);
triggers.value.splice(index, 1);
});

document.getElementById('logicBtn' + index).addEventListener('click', e => {
e.preventDefault();
console.log("Submit Trigger " + index)
value.name = document.getElementById('alertinput' + index).value;
value.amount = document.getElementById('amountinput' + index).value;
value.type = document.getElementById('selectLogic' + index).value;
});
}

document.getElementById('newTrigger').addEventListener('click', e => {
triggers.value.push({name: 'Alert1', type: 'equals', amount: 0});
});

document.getElementById('testBtn').addEventListener('click', e => {
test.value = document.getElementById('testInput').value;
});

document.getElementById('defaultBtn').addEventListener('click', e => {
console.log("Submit Defaults ")
claim_id.value = document.getElementById('claimidInput').value;
defaultTrigger.value = document.getElementById('defaultInput').value;
});
</script>
</body>
</html>
91 changes: 91 additions & 0 deletions extension/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const WebSocket = require('ws');
const request = require('request');
'use strict';

module.exports = function (nodecg) {
nodecg.Replicant('claim_id', { defaultValue: "Place claim id of your livestream here", persistent: true });
nodecg.Replicant('defaultTrigger', { defaultValue: "Alert1" });
nodecg.Replicant('triggers', { defaultValue: [{name: 'Alert1', amount: '', type: "greaterthan" }] });
nodecg.Replicant('test', { defaultValue: 0, persistent: false });

const claim_id = nodecg.Replicant('claim_id');
const defaultTrigger = nodecg.Replicant('defaultTrigger');
const triggers = nodecg.Replicant('triggers');
const test = nodecg.Replicant('test');
const socket = new WebSocket('wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=' + claim_id.value);
const equals = [];

function activateAlert(alertname, username, amount) {
var myJSONObject = {"name": alertname, "message":"(" + username + ") tipped (" + amount + ") LBC"};
request({
url: 'http://localhost:9090/simple-alerts/alert',
method: "POST",
json: true,
body: myJSONObject
}, function (error, response, body){
console.log("Done");
});
}

function checkTriggers(amount, alertName) {
equals.forEach(isEquals);
// Check if alert is equal too a trigger
function isEquals(value, index, array) {
if ( value.amount == amount ) {
alertName = value.name;
}
}
if ( alertName == defaultTrigger.value ) {
// Check greater than starting from largest number.
var sorted = triggers.value;
sorted.sort(function(a, b){return b.amount - a.amount});
for (const value of sorted) {
if ( value.type != "equals" && parseFloat(amount) >= parseFloat(value.amount) ) {
alertName = value.name;
break;
}
}
}
return (alertName);
}

triggers.on('change', value => {
equals.splice(0, equals.length);
triggers.value.forEach(sortEquals);
function sortEquals(value, index, array) {
if ( value.type == "equals" ) {
// Add message to Queue
equals.push(value);
}
}
});

test.on('change', value => {
var alertName = defaultTrigger.value;
alertName = checkTriggers(value, alertName);
activateAlert(alertName, "Slyver Testallone", value);
});

// Connection opened
// Alojz helped with websockets code
socket.addEventListener('open', function (event) {
socket.send('Hello LBRY!');
});

// Listen for messages
socket.addEventListener('message', function (event) {
var comment=JSON.parse(event.data);
console.log(comment.data.comment.comment);
// If comment has support
if(comment.data.comment.support_amount>0) {
console.log("Has tip");
var userName = comment.data.comment.channel_name;
var alertName = defaultTrigger.value;
var amount = comment.data.comment.support_amount

alertName = checkTriggers(amount, alertName);
activateAlert(alertName, userName, amount);
}
});

};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "odysee-simple-alerts-connector",
"version": "0.0.0",
"files": [
"dashboard",
"extension.js",
"extension"
],
"dependencies": {
"ws": "^7.0.0",
"request": "^2.0.0"
},
"nodecg": {
"compatibleRange": "^1.1.1",
"dashboardPanels": [
{
"name": "panel",
"title": "odysee-simple-alerts-connector",
"width": 3,
"file": "panel.html",
"headerColor": "#525F78"
}
]
}
}

0 comments on commit ed6e76f

Please sign in to comment.