DEV Community


Posted on

Build a Twitter bot with ⚡️


In this tutorial, we're going to create a Twitter bot using @scheduled functions feature that will tweet the daily price of Blockstacks($STX).

Stacks is a new cryptocurrency integrated with Bitcoin's security, capital, and network, enabling you to build apps, smart contracts, and digital assets.


  • You must have an approved Twitter developer account.
  • AWS account.

You can clone the code for this project here:

Step 1: Create a Twitter app

First, let's set up our Twitter developer account to create an app and gain access to our API keys.

Screen Shot 2021-03-04 at 4.56.39 AM

Follow these instructions to create an app:

  • Navigate to and click Developer Portal
  • Create and name your app.
  • Scroll to App permissions and change to Read and Write.
  • Navigate to the Keys and tokens tab and regenerate all of your API keys. Make sure to save them somewhere safe.

Step 2: Create an Architect app & add environment variables

Now it's time to create our Architect project. We're going to follow the Quickstart found on

mkdir twitterApp
cd twitterApp
arc init
Enter fullscreen mode Exit fullscreen mode

Next, create a preferences.arc file holding the following keys:


You can sync these API keys by using the arc env CLI command

Here is an example of how your preferences.arc file should look:

# The @env pragma is synced (and overwritten) by running arc env
  TWITTER_ACCESS_TOKEN something-for-testing
  TWITTER_ACCESS_TOKEN_SECRET something-for-testing
  TWITTER_API_KEY something-for-testing
  TWITTER_API_SECRET something-for-testing

  TWITTER_ACCESS_TOKEN something-for-staging
  TWITTER_ACCESS_TOKEN_SECRET something-for-staging
  TWITTER_API_KEY something-for-staging
  TWITTER_API_SECRET something-for-staging

  TWITTER_ACCESS_TOKEN something-for-production
  TWITTER_ACCESS_TOKEN_SECRET something-for-production
  TWITTER_API_KEY something-for-production
  TWITTER_API_SECRET something-for-production
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Scheduled function

Start by adding a Scheduled function to your app.arc files @scheduled pragma.


folder public


get /

# Scheduled function for once a day.
daily rate(1 day)

# @aws
# profile default
# region us-west-1
Enter fullscreen mode Exit fullscreen mode
  • We're going to be using the twitter-api-client to interact with the Twitter API more seamlessly.
  • We'll use tiny-json-http to fetch the data from to use for our daily scheduled tweet.
  • cd into src/scheduled/daily to install these two dependencies above so that we may use them inside of the function.
  • Place this code into src/scheduled/daily
const tiny = require('tiny-json-http')
const { TwitterClient } = require('twitter-api-client')

exports.handler = async function scheduled (event) {

  const twitterClient = new TwitterClient({
    apiKey: process.env.TWITTER_API_KEY,
    apiSecret: process.env.TWITTER_API_SECRET,
    accessToken: process.env.TWITTER_ACCESS_TOKEN,
    accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET

  let url = ``

  const status = await tiny.get({url})
  .then(response => {
    let stx =
    let tweet
    if (stx) {
      //tweet the $STX price
      tweet = 'Daily $STX price: ' + "\n" + "\n" + '$' + stx.priceUsd
    return tweet
  }).catch(err => {

  console.log('Log2:', status)

  await twitterClient.tweets.statusesUpdate({
    status: status
  }).then(response => {
    console.log("Tweeted!", response)
  }).catch(err => {

  console.log(JSON.stringify(event, null, 2))

Enter fullscreen mode Exit fullscreen mode
  • First, we authenticate our Twitter account using our API keys loaded through the twitter-api-client.
  • Then, we use tiny-json-http to pull in the $STX data from
  • After our account is authenticated and data loaded, we create a tweet using the twitter-api-client to tweet out a status update.

You can test this code to ensure that your function is working by placing this exact code in the get / of your app. When you navigate to this endpoint, it will trigger a status update to your Twitter feed.

Screen Shot 2021-03-09 at 8.01.44 AM


Architect and Begin are great tools for interfacing with the Twitter API. I encourage you to explore both and the Twitter API to see what kinds of bots you can come up with!

Top comments (0)