Let's Go Girls - Node.js, Shania Twain, and Twitter-bots

September 2020. 


This all started with a tweet. 

Tweet from Liz Maupin: if i were shania twain i would tweet lets go girls every day for the rest of my life

Chloe Condon is a DevRel who has been helping me along my journey to working in tech. I shared this tweet with her early in our menteeship, and she had a great idea: a twitter bot that tweets "Let's go Girls," every day. Shania Bot was born. Chloe and I spen thirty minutes on Azure's Logic Apps and she was up and running. Here's an article about using Logic Apps. 

But we needed to do more with her. And I needed to practice coding. And create content so I could start making a name for myself in the tech world, 

Chloe, as always, knew just what to do. She says she has an idea for how to make Shania reply to people (specifically, that whatever the person tweeted about isn't impressive.), but we need to do some research. We meet up on Teams and start messing around with some research on making Twitter bots. Eventually, we land on The Coding Train.  His Node.js Twitter bot tutorial is literally exactly what Chloe and I use to get Shania to reply. Copy/paste with slight adjustment. You know, developer stuff. 

Over time we add new functionality: an array of emojis to add in front of the reply, a couple of fixes to the code to make it cleaner, etc. etc. 

We get it working. She replies to us when we ask if she's impressed, but we're still running it locally on Chloe's machine everytime we want her to reply. 

We gotta get her online. 

Chloe and I hosted Aydrian Howard on a show we do on Thursday nights, The Show Must Go Off (the air), to help us with the process. He points us to Heroku to host her, since the first app you host there is free. We spend the episode getting her up and running, mess with the code a little to get it to work, accidentally show the keys on the stream, reset the keys, etc etc etc. We got her up, and she's working just as intended. 

Which brings us to today, Saturday, February 13, 2021. 

Chloe and I had been taking about getting her off Logic Apps and writing code to perform the same functionality. Today I made my own code using a suggestion from Brendan O'Leary: a npm called Node Schedule

I spent hours coming up with what ended up being 14 lines of code, but I got it working. 


Here's how I did it. 


HOW I USED NODE.JS AND NODE SCHEDULE TO REPLACE A LOGIC APP. 

First, In reading the documentation, I was looking for the right thing to use. I came across the ability to set recurring events at a specific time, the exact functionality I was looking to replace. 


You can build recurrence rules to specify when a job should recur. For instance, consider this rule, which executes the function every hour at 42 minutes after the hour:  const schedule = require('node-schedule');  const rule = new schedule.RecurrenceRule(); rule.minute = 42;  const job = schedule.scheduleJob(rule, function(){   console.log('The answer to life, the universe, and everything!'); });



Perfect. Copy, paste, adjust, done. 

Except, I had no idea how to acually call the "job" in my code. VS Code just kept telling me it was defined but never used. In my mind, I literally couldn't figure out how to make it happen. It was different from the way Shania worked with her response, she was using the twitter api through Twit. So it was using a totally different package and it wasn't much help. 

So after setting it all up, adding time zones to the rule variable using a function I found on their github, I'm still having trouble figuring out what to do. I'm reading the docs over and over, taking out the namesless function with the job variable, making it named and putting it back in, making myself crazy with 

node shania.js

node shania.js

node shania.js 

over and over running the app and getting new and more annoying errors each time. 


I go back to the docs. Turns out "job" is an eventemitter and needs to be handled, I think? This is where my understanding gets very very murky. 

Here's my code. 

const job1 = schedule.scheduleJob(rule, function() {   console.log("bah baaaaaaaah budah duh dah dah");   T.post('statuses/update', {status: "...let's go, girls."}); });   function letsGoGirls()  {   console.log("We went Girls") };  job1.on("tweetIt", letsGoGirls)

Let's walk through it. So job1 is an even emitter (I think?) which is scheduled to run according to the rule variable pased in at the same time as the nameless function. The rule says to do it every day at noon EST. The function writes the opening guitar riff of "Man I feel like a woman" to the console then it uses the twit object "T" to "post" a status update to twitter. The status will read "Let's go girls." 

Now this is where I have no idea how I made it work. 

The function letsGoGirls writes "We went Girls" to the console. I did it becuase I know that if I'm doing job1.on, I have to  pass in a string, and a function. I don't know why, I don't know how to do it without that, and I can't pass in the same function (which is why I tried to remove the nameless function and name it so I could pass it in again but that wasn't working.)

I'll be honest, this might as well be magic becuase all I know is it works, and "We Went Girls" does not get logged to the console. SOMEHOW this works, and I don't know why. 

However, it does in fact work. 


We Went, Girls

This is where it ends for now. I'll be revisiting this with someone who knows what they're doing and I bet I've made some hilariously foolish mistakes in my code, in my understanding of it, and I'll spend some time embarassed about it. 

But I'm learning. 

I'm getting better at something new that I never thought I'd be able to do. 

And then I wrote it down, becuase that's what I REALLY need to be doing from now on. Documenting it all. 


Here's to more blog posts in the future. 

Comments