Create a simple, free, text-driven Twitterbot with AWS Lambda & Node.js

Setup: Twitter

First, you’ll need to create a Twitter account for your bot, and confirm the email address you used to set it up. Then go to apps.twitter.com and sign in with your new Twitter account to create a Twitter app. Give your app a name, a description, and put a URL in the website field (it doesn’t really matter what URL, but linking to your own site is nice). You don’t need to enter anything in the “Callback URL” field. Scroll down to agree to the developer terms and submit the form.

Creating an application at apps.twitter.com

Setup: AWS

If you don’t already have an AWS account, you can sign up for one here.

aws configure --profile YOURPROFILENAME
module.exports = {
profile: '',
// load your AWS credentials from a custom profile
region: 'us-west-2',
//the region of your Lambda function
handler: 'index.handler',
//the name of the handler function: index because the main file is index.js
role: 'arn:aws:iam::YOURACCOUNTHERE:role/lambda_basic_execution', // the Lambda role
functionName: '',
//name
timeout: 10,
//how many seconds your function should run before it times out
memorySize: 128,
//how much memory your function needs (shouldn't need more than this)
publish: true,
// this creates a new version of your Lambda function every time you update it
runtime: 'nodejs',
// for node 10, otherwise use 'nodejs4.3'
}

Finally: The Bot Guts

This very very simple bot just pulls a line at random from a file and Tweets it. So you need a file! There’s a sample file in the repo at botfiles/sample-text.js. Add your text to this file, one tweetable item per line.

npm install --save ./YOURPATH/botfiles
rm -rf node_module/botfiles && npm install /YOURPATH/botfiles

Testing!

The index.js file has three functions, plus the exported Lambda handler function. One picks a line from your file, one checks to make sure it’s short enough to tweet, and one checks that it also doesn’t include offensive words (using Darius Kazemi’s wordfilter package) and isn’t undefined or empty. There are tests for these functions in the test/test.js file, and you can run them with ‘npm test’. (There’s also a gulp task for linting the index.js file — run ‘gulp lint’ to check your code.)

Deploy!

Once your configuration files (config.js and lambda-config.js) include your credentials, and you’ve added text to the text file, you’re ready to deploy! Just type ‘gulp deploy’.

Test Again!

You can check that your bot is firing correctly and that it’s wired up right to Twitter by going to the AWS Lambda console and submitting a test event. For us-west-2, the console is here. Click on your function name and choose “Configure test event” from the drop-down Actions menu in the upper left.

empty test event
Success!

Schedule!

To make your bot run on a schedule, open up the AWS Lambda console. (You may need to sign in again.) For us-west-2, it’s here. Click on your function name and then choose the “Event Sources” tab. Click on “Add event source.”

Making Changes!

Redeploying your code is easy. Make your changes locally, then run ‘npm test’ (to make sure nothing broke), ‘gulp lint’ (to catch any syntax issues), and then ‘gulp deploy’. You’ll see the progress in the console, it’ll will look something like this:

[09:35:09] Finished ‘node-mods’ after 5.63 s[09:35:09] Starting ‘zip’…[09:35:10] Finished ‘zip’ after 707 ms[09:35:10] Starting ‘upload’…[09:35:14] Finished ‘upload’ after 4.52 s[09:35:14] Finished ‘deploy’ after 11 s

Deleting!

If you want to delete the bot entirely, go back to the Lambda dashboard, select your function name, and choose ‘Delete function’ from the Actions drop-down:

--

--

Founder, Wordnik.com. Recovering lexicographer. Full Stack-Overflow Developer. Developer Advocate, IBM.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Erin McKean

Erin McKean

Founder, Wordnik.com. Recovering lexicographer. Full Stack-Overflow Developer. Developer Advocate, IBM.