Twitterbots are fun, and it's easy to create one with Apache OpenWhisk and Bluemix. With this template, you can make a bot that randomly tweets a line from a textfile. (For an example, check out PLUBot.)
Prerequisites: I assume you have node and npm installed. Need help installing either? Here's a link for you.
- Clone this repo
- Run
npm install
- Open in your favorite editor.
To run your own bot you will need Twitter and Bluemix credentials, the OpenWhisk CLI, and an idea.
- a Twitter account
- a Twitter application (not the same as a Twitter account; apply for one at https://apps.twitter.com)
- a Consumer Key, a Consumer Secret, an Access Token, and your Access Token Secret
Put this information in the temp-config.js
file, and rename it to config.js
. Then delete temp-config.js
.
- a Bluemix account. Sign up for one at https://console.ng.bluemix.net/registration. No credit card required! You'll get 2GB of runtime and container memory free for 30 days, plus access to provision up to 10 services (including databases, devops pipelines, and more).
- Visit the OpenWhisk CLI setup page and log in to Bluemix.
- Install the OpenWhisk CLI (instructions here).
- Log in to Bluemix, if you haven't already.
- Follow these instructions (step 2 of the CLI setup) to authorize your OpenWhisk CLI:
You can also run your own OpenWhisk server! This is beyond the scope of this workshop, but you can find more info here.
To add text for your bot to tweet, enter it in the sample-text.js
file. Make sure each line you add (except the last one) ends with a comma. This file should have no blank lines.
There are tests for the functions in index.js
in the tests/test.js
file. Run them with npm test
.
Because we want to use node modules with our bot, we'll upload our entire directory as a zip file. NB: We want to zip the contents of our repo, not the root folder.
On a Mac, you can do this with this command:
zip -r -X "twitterbot.zip" *
If you update your bot, remember to remove your zip file so that it doesn't get re-zipped into your updated file! Try
rm twitterbot.zip && zip -r -X "twitterbot.zip" *
wsk action create myTwitterBot --kind nodejs:6 twitterbot.zip
Remember that if you change your action, you need to update
it:
wsk action update myTwitterBot --kind nodejs:6 twitterbot.zip
Before we set up triggers and rules to let our bot tweet by itself, let's check that it actually works.
wsk action invoke myTwitterBot -r --blocking
You should see a response like this (the "payload" should be a line from your sample-text.js)
{
"payload": "yep, tweeted: I'm a good bot, Brant"
}
We want our bot to tweet once an hour (don't have your bot tweet more than once an hour; that's kind of rude). You can set an alarm trigger with cron
(let's call it sendTweet
) to make this happen, like so:
wsk trigger create sendTweet --feed /whisk.system/alarms/alarm --param cron "5 * * * 0-6"
Cron syntax is tricky! If you don't want to remember it, you can use this handy crontab generator.
Once you have an action and a trigger, you can put them together with a rule, like so:
wsk rule create tweetRule sendTweet myTwitterBot
The format here is
wsk rule create NameOfRule NameOfTrigger NameOfAction
Your bot should now tweet five minutes after the hour, every hour, forever (or until your Bluemix trial expires).
Double-check the name of your rule by entering
wsk rule list
at the command line. Then delete your rule (most likely tweetRule
) with the line
wsk delete rule tweetRule
.
If you are creating this example on a lab computer, make sure you:
- log out of your Bluemix account
- delete your bot code on this machine:
rm -rf openwhisk-twitterbot-template
- de-authorize your OpenWhisk account on this machine, by removing
$HOME/.wskprops
(on the Interconect DevZone lab machines this will be in/usr/local/bin
). - delete sensitive commands (e.g., your OpenWhisk auth command) from your bash/zsh history. You can do this by editing the
~/.bash_history
or~/.zsh_history
files. (The zsh shell may also be called.zhistory
. Not sure where yours is? Tryecho $HISTFILE
.)
Thanks to Darius Kazemi for the wordfilter module and for the .pick function. Thanks to Ray Camden for the best darn OpenWhisk blog posts ever, plus the link to the crontab generator. Thanks also to Allison Parrish and David Celis for reviewing an earlier version of this bot repo; all errors remain the exclusive property & reponsibility of the author. :-)