Escaping the Basement For Christmas
The Christmas Party on the fourth floor of Reynholm Industries was in full swing. It was the party legends would be made of, something that would be talked about around the water cooler for years to come. Anyone who wasn't there was sorely missing out. And right now Roy was certainly missing out.
At his desk in the IT department in the basement, Roy stared at his computer screen in dismay at the endless support requests that kept coming in on Slack. Moss was out at some sort of Countdown related Christmas party, and no-one had seen Richmond since he left for that job as a zoo keeper, leaving his boss Jen the choice between going to the office party or making Roy stay behind and answer support queries...she'd obviously chosen the latter option. And now Roy was forced to endure things like this all evening long:
If only there was some way he could automate all of this so he could go upstairs and join the frivolity?
Slack has a fully featured customer accessible API that is able to do pretty much anything Roy could do in the web client. In addition to a collection of normal HTTP GET / PUT / POST API calls Slack supports a websocket API to allow the program to retrieve and answer messages in real time just like the web client does.
The normal way to use the rtm real time message service is:
- Make a standard HTTP GET call to the
rtm.startAPI method to get a unique websocket URL
- Connect to that bi-directional websocket URL
- Read JSON from / send JSON to that websocket to receive and send messages respectivly
Handling asynchronous programming where your code is both listening and sending messages at the same time used to be complex, but now Perl has a bunch of really good event loop modules that abstract all this away.
One such framework, Mojolicious, has excellent websocket handling right out of the box, supporting all the idiosyncrasies of connecting to the socket, handling the necessary HTTP socket upgrades specified in the protocols, and allows Roy to simply register callbacks that are triggered whenever the server has sent us a complete chunk of JSON down the socket.
Given all of this, it shouldn't be too hard for Roy to use it to write something with it to pretend to be him in Perl:
With the basics of connecting to the server (and staying connected by sending a ping every five seconds no matter what) done Roy needed to work out how to handle messages sent from the server. Slack sends all kinds of messages: people leaving or joining channels, ping responses, even notifications that people are starting to type. But in reality there was only one message Roy was interested in...someone starting to chat to him in a private one-on-one chat, presumably seeking support. He decided to script his standard one-line response to the first time anyone ever asked him anything that seemed to solve almost every problem that came his way:
Roy didn't have to wait long before someone triggered his script:
It worked! Roy ran gleefully upstairs to see if there was any eggnog left.
Hey You $#%! Fix My Computer!
Jen didn't seem to appreciate his clever solution when he bumped into her next to the punch bowl. She thought it would make people very angry. Normally Roy didn't listen to Jen too much, but he had to admit that she might be right about that. Maybe he should have the script let him know if someone was truly upset?
Roy figured if they were swearing at him, he'd better pay attention!
Now when the boss man complained, he should be tempoarly soothed while his script could summon him.
All that was needed was some way to notify himself. Of course! The Slack app on his mobile phone could be configured to buzz whenever a particular user - like slackbot, the Slack bot - messaged him. Now all he had to do was lookup the secret slackbot URL for his account on the Slack web pages and make asynchronous posts to that whenever he wanted to notify himself.
If he could just avoid drinking enough to lose his phone all would be great!