Why use an API? It’s really nice to have an engine that can arbitrarily supply information to be used by any other interface.
Why use Google Apps Script (or GAS)? Well, it’s a really nice way of deploying an API without a server and without a database. And it’s free! Yes, you’re rate-limited, but you won’t reach that limit unless your friend is super popular (or your API is unbelievably awesome).
If you want to know more about deploying web apps on Google Apps Script, look here.
Here is a brief explanation of the code.
Listen for “GET” requests. It does this with the doGet() function, which is unique to GAS.
Load the data in the Google Sheet into one array.
Find the quotes that are least used (all of them, when you’re just starting out; but later on, this will prevent your bot from seeming repetitive)
Reduce the set of quotes to just the set of ones that haven’t been seen in a while (lowest count number)
Log the interaction
Return the data it as a JSON object to whoever asked for it
I do some other things along the way, like log every interaction for debugging (in the second tab), and return the total number of interactions done
When you deploy the app and make it publicly available, you test it by simply pasting the code into a browser. It should return something like:
{"botText":"Shut up, Dana. I didn't ask you","counter":96}
You can now use this data however you like!
Step 3: Build a Slack Bot via Zapier
In most companies, ordinary users can’t just create bots using the Slack API interface.
But you CAN install Zapier, and you can deploy bots with Zapier.
One caveat: you can only deploy your bot to listen for
Any mention of the bot text (e.g. “@turzobot”) in any public channel, or
Any mention of the bot text in one private channel
It’s not perfect. You can’t chat with the bot, for example, in case you’re feeling lonely.
Creating a Slackbot using Zapier
You set up the Zap to
Listen for a message in the channel
Filter it for the keyword you’re looking for
Fetch a random message from the API
Respond with a message
The trickiest part to set up is the API call. You have to use Webhooks by Zapier. You might notice, however, that most fields are optional. The only one you really need is the one at the top – the URL. And for now, that’s all you need!
Later, I plan on making my API more intelligent, replying with specific responses if keywords are used. But I haven’t gotten there yet!