From 86d5cdf377171ea178dd28e17962ed402042cfb5 Mon Sep 17 00:00:00 2001 From: tuxfoo Date: Sun, 16 May 2021 01:02:49 +1200 Subject: [PATCH] Add support for simple chat --- README.md | 15 +++--- extension/index.js | 112 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0bfc003..f2738d1 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ # odysee-simple-alerts-connector -This nodecg bundle listens for LBC tips from odysee hyperchat's and renders an alert using the simple-alerts bundle. +This nodecg bundle listens to Odysee hyperchat and in in-form's other bundles of events. +This bundle is also used to set up triggers for simple-alerts. -In future releases, this bundle may make use of other bundles such as: -* donation ticker -* chat overlay -* tip history -* top tipper +This bundle also connects to the following nodecg bundles such as: +* simple-alerts +* simple-donation-ticker +* simple-chat +* simple-goals -In releases, you can find a portable version containing nodecg and simple-alerts for Windows and Linux that is ready to go, just extract it and run it. +In releases, you can find a portable version containing nodecg and a collection of nodecg bundles for Windows and Linux that are ready to go, just extract it, run it, configure overlay's and stream. diff --git a/extension/index.js b/extension/index.js index 0e993bc..aa20c1d 100644 --- a/extension/index.js +++ b/extension/index.js @@ -15,9 +15,11 @@ module.exports = function (nodecg) { const defaultTrigger = nodecg.Replicant('defaultTrigger'); const triggers = nodecg.Replicant('triggers'); const test = nodecg.Replicant('test'); - + // Fail safe for disconnections + const reload = setInterval(isOpen, 300000); const equals = []; +// simple-alerts Rest API Request to add on alert to queue function activateAlert(alertname, username, amount) { var myJSONObject = {"name": alertname, "message":"(" + username + ") tipped (" + amount + ") LBC"}; request({ @@ -30,6 +32,33 @@ module.exports = function (nodecg) { }); } +// simple-chat rest API request to send chat message. + function sendToLog(username, message, extra) { + var myJSONObject = {"name": username, "message": message, "extra": extra}; + request({ + url: 'http://localhost:9090/simple-chat/log', + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + console.log("Pushed to Log"); + }); + } + +// simple-chat rest API request to add a chat message to history (so chat can be preloaded) + function addhistory(username, message, extra) { + var myJSONObject = {"name": username, "message": message, "extra": extra}; + request({ + url: 'http://localhost:9090/simple-chat/history', + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + console.log("Pushed to history"); + }); + } + +// simple-donation-ticker Rest API request to add a message to ticker. function addToTicker(username, amount) { var myJSONObject = {"name": username, "amount": amount}; request({ @@ -41,7 +70,7 @@ module.exports = function (nodecg) { console.log("Pushed to ticker"); }); } - +// simple-goals Rest API request to add value to goal function addToGoal(amount) { var myJSONObject = {"amount": amount}; request({ @@ -54,6 +83,7 @@ module.exports = function (nodecg) { }); } +// check alert triggers function checkTriggers(amount, alertName) { equals.forEach(isEquals); // Check if alert is equal too a trigger @@ -76,6 +106,63 @@ module.exports = function (nodecg) { return (alertName); } + function preloadChat(claimid) { + // Get comment history + var myJSONObject = { + "jsonrpc": "2.0", + "id": null, + "method": "get_claim_comments", + "params": { + "claim_id": claimid, + "page_size": 15, + "is_channel_signature_valid": true, + "visible": true + } + }; + request({ + url: 'https://comments.lbry.com/api', + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + nodecg.log.info("Preloaded Comment"); + (async function() { + for (var i = 0, j = body.result.items.length - 1; i < body.result.items.length - 1; i++, j--) { + await addComment(body.result.items[j]); + } + })(); + }); + + } + + //get channel/username from comment id. + async function addComment(value) { + var url = 'https://comments.lbry.com/api'; + var myJSONObject = `{ + "jsonrpc": "2.0", + "id": "null", + "method": "get_channel_from_comment_id", + "params": { + "comment_id": "` + value.comment_id + `" + } + }`; + var username = ""; + try { + const response = await got.post(url, { headers: {'Content-Type': 'application/json'}, body: myJSONObject, json: true}); + username = response.body.result.channel_name; + var comment = value.comment; + if (value.support_amount > 0) { + var extra = { class: "tipmsg", message: "Tipped " + value.support_amount + " LBC"}; + } else { + var extra = { class: "message-wrap", message: ""}; + } + addhistory(username, comment, extra); + nodecg.log.info(username + ":" + comment); + } catch (error) { + nodecg.log.info("Failed to fetch get channel name.") + } + } + triggers.on('change', value => { equals.splice(0, equals.length); triggers.value.forEach(sortEquals); @@ -97,8 +184,9 @@ module.exports = function (nodecg) { }); function isOpen() { - if (socket.readyState === WebSocket.OPEN) { - socket.close(); + if (socket.readyState != WebSocket.OPEN) { + nodecg.log.info("Timed out, Reconnecting...."); + getClaimid(); } } @@ -123,6 +211,7 @@ module.exports = function (nodecg) { } function reconnect(claimid) { + preloadChat(claimid); nodecg.log.info("Connecting using " + claimid); socket = new WebSocket('wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=' + claimid); // Connection opened @@ -134,17 +223,24 @@ module.exports = function (nodecg) { socket.addEventListener('message', function (event) { var comment=JSON.parse(event.data); nodecg.log.info(comment.data.comment.comment); + + var userName = comment.data.comment.channel_name; + var alertName = defaultTrigger.value; + var amount = comment.data.comment.support_amount; + var msg = 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 - addToGoal(amount); addToTicker(userName, amount); alertName = checkTriggers(amount, alertName); activateAlert(alertName, userName, amount); + sendToLog(userName, msg, {class: "tipmsg", message: "Tipped " + amount + " LBC"}); + addhistory(userName, msg, {class: "tipmsg", message: "Tipped " + amount + " LBC"}); + } else { + sendToLog(userName, msg, {class: "message-wrap", message: ""}); + addhistory(userName, msg, {class: "message-wrap", message: ""}); } });