Skip to content

Instantly share code, notes, and snippets.

@ajmwagar
Created February 4, 2021 17:02
Show Gist options
  • Save ajmwagar/6f120a3637fbdfd2e5771ed6b244cca7 to your computer and use it in GitHub Desktop.
Save ajmwagar/6f120a3637fbdfd2e5771ed6b244cca7 to your computer and use it in GitHub Desktop.
// Import the zipbot library
const { Bot, Event } = require("zipbot");
const axios = require("axios");
const Querystring = require('querystring');
// Create a new bot object
// However no auth is needed since we host it all in our own private environment
const bot = new Bot();
const RETURN_TO_WHITE_SECS = 20;
const DMX_URL = "http://139.138.97.133:8080/set_dmx";
const COLORS = [
{
name: "red",
fnc: async () => await setColor(255, 0, 0, 0)
},
{
name: "blue",
fnc: async () => await setColor(0, 0, 255, 0)
},
{
name: "green",
fnc: async () => await setColor(0, 255, 0, 0)
},
{
name: "white",
fnc: async () => await setColor(255, 255, 255, 255)
},
{
name: "purple",
fnc: async () => await setColor(160,32,240, 0)
},
];
// Claims
// A "Claim" is a preview of the message;
// The bot replies with whether or not we want
// to engage the user with a response (defaults to no)
bot.on(Event.Claim, (ctx, msg) => {
if (msg.to == "+18339477446" && COLORS.map(color => color.name).includes(msg.body.toLowerCase())) {
ctx.claim();
}
});
// Bots
// Inbound messages are handled by the Bot event
// In normal terms, this is your default response to a receive webhook.
// // However, we do provide a nice 'Context' object for easily responding to messages.
bot.on(Event.Bot, async (ctx, msg) => {
let color = COLORS.find(color => color.name == msg.body.toLowerCase());
await color.fnc();
ctx.reply(`Setting color to ${color.name}`);
ctx.stop();
});
// Run the bot, and start our framework
bot.run();
async function setColor(r, g, b, w, returnToWhite=true) {
let arr = makeArrayOf(0, 512);
arr[0] = r;
arr[1] = g;
arr[2] = b;
arr[3] = w;
let body = Querystring.stringify({
u: "1",
d: arr.join(",")
});
try {
await axios.post(DMX_URL, body, {
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
});
} catch (err) {
console.log(err);
}
if (returnToWhite) {
setTimeout(() => setColor(255, 255, 255, 255, false), RETURN_TO_WHITE_SECS * 1000);
}
}
function makeArrayOf(value, length) {
var arr = [], i = length;
while (i--) {
arr[i] = value;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment