The Timebot is finished.
Continue reading About A Bot
Category: JavaScript
Subtle, But Important
The amount of work to get this,
instead of this,
is the difference between a two week vacation and two weeks of research.
Helper function(s)
Writing the Slackbot, which communicates via text messages so there are plenty of places where there are numerals being referred to in text form, which means there are often cases where a word is either singular or plural, depending on whether it’s referring to one of something or more than one of something.
Botkit and Regular Expressions
Regex has never been my favorite part of writing code. I get that some people geek out on it and bless their hearts. For me, it’s an annoyance that is best left abstracted out of my daily coding experience. Thankfully, I am not alone. There are plenty of sites and programs that generate regular expression sequences for all your pattern matching needs.
Bot Engineering
Howdy makes a cool botkit that lets people write their own bots for Slack. Also Facebook, but meh.
In JavaScript, of all things, using node.js which makes the whole thing pretty accessible. Node.js lets you write JavaScripts that you can execute from a command line or a text file. You can then do things like create a web server or build Skynet.
Annoying JavaScript Functions
A quick little JavaScript to cycle through an array of hex colors. Installs an interval function to change the background color every second.
// Variables // install an interval function update_colors() and run it every 1000 milliseconds (once a second) var closeInterval = window.setInterval(update_colors, 1000); // global variable to control where we are in the array of colors var step = 0; // this is our array of colors var ColorCycle = [ "#838a85", "#bbaa99", "#c6ceb6", "#9fb1a2", "#748f8b" ];
Everything’s set up. There is no need for a window.onload() function because we’ve installed this as an interval function. That takes care of making sure we aren’t executing on null document objects.
function update_colors() { // Call a utility function that changes the background color swap_colors(step); // step is where we hold the value of the current item in the array step++; // Let's not go outside the bounds of the array, please if (step > ColorCycle.length) { step = 0; } }
There we go. All that’s left is a little utility function to do the actual swapping.
function swap_colors(new_color) { // Change the page background document.body.style.background = ColorCycle[new_color]; // This necessary for the first time through, but it seems to be unnecessary // for subsequent passes. I'm a little bit confused by this, but okay. // Changing the background color of the Section object inside <body> var section_style = document.querySelector("section"); section_style.style.background = ColorCycle[new_color]; }
So, as I mention in the comment, changing the “section” object background seems to be necessary since I’ve given it its own background definition in the style sheet. I suppose I could have just not given the section object a style, which would eliminate the need to change the background, but that’s okay, because now I know how to change an object’s background when it’s different than the body background.
Next, I will work on making the transitions between colors less jarring by writing a nice function that will blend colors.