Building a Twitter Bot with Vapor

Learn how to build a Twitter bot and create your own tweet automation tools with Vapor and Server Side Swift. By Beau Nouvelle.

Leave a rating/review
Download materials
Save for later

The saying “Work Smarter, Not Harder” is thrown around a lot these days. You may hear it from managers and salespeople, but did you know that it was an engineer who said it first?

Allen F. Morgenstern was likely thinking about automation when he coined the term in the 1930s. Today, automation in the form of robot vacuums, self-driving cars and digital assistants improve our lives by relieving us of the most tedious and repetitive tasks, freeing up time for more fun activities, like learning Swift!

This tutorial will help you work smarter by teaching you how to automate your Twitter presence like a pro.

You’ll learn how to:

  • Set up a Twitter Developer Account.
  • Send tweets using the Twitter API.
  • Schedule tweets using Vapor queues.

By the end of the tutorial, you’ll have a web app that tweets a Steve Jobs quote to your account once per day.

Note: This tutorial requires a Twitter account with a valid phone number attached as well as Xcode 12, Swift 5.4 and Vapor 4. The Getting Started with Server-Side Swift with Vapor 4 tutorial will walk you through the setup process.

Getting Started

Download the projects by clicking Download Materials at the top or bottom of the tutorial and unzip it.

The Twitter API requires you to make requests using OAuth1, and the starter project contains all the necessary OAuth code.

Open the project in Xcode by double-clicking Package.swift. Once open, the Swift Package Manager may take some time to pull the dependencies included in the project.

Browse through the folders to familiarize yourself with the project and give it a run to make sure everything compiles.

You’ll see the following output:

[ NOTICE ] Server starting on

Becoming a Twitter Bot Developer

Twitter bots are simple apps that perform tasks on the Twitter platform. They can do many things like post tweets, like tweets and even collect information on Twitter trends.

Companies might use bots to help with customer support, while influencers may use them for scheduling tweets to go out at specific times of the day.

Bots can also listen for events on other platforms and act upon changes, such as posting a tweet when a website publishes a news article.

There’s even a bot that generates moths and gives them made up scientific names, @mothgenerator!

Before you start sending tweets using Twitter’s API, you’ll need to set up your developer account and generate a few security keys.

Setting Up a Twitter Developer Account

Log into your Twitter account in your web browser and go to:

Get started with Twitter API and tools

Click Apply for a developer account. You’ll start a short setup process during which you’ll need to answer a few questions.

Twitter API and tools setup process

On the first screen, choose HobbyistMaking a BotGet Started.

You’ll need to provide and verify your phone number on the next screen if you haven’t already done so on your Twitter account.

Add a valid phone number

From this point on, the sign-up process is straightforward. How you answer the questions depends on your own preferences and the kind of apps and bots you plan to build. When you finish, you’ll end up at the Developer Portal Dashboard.

Now you can get a little more creative!

Your First Twitter App

In the panel on the left, open Projects & AppsOverview. Scroll down and click + Create App.

Twitter developer portal

Here’s the most challenging part of the entire tutorial: Coming up with a name for your app!

Name your app

The name must be unique so it won’t clash with the existing names already on the platform. If you’re out of inspiration, name it QuoteBot with a bunch of random numbers after it.

The next stage takes you through to the Keys & Tokens screen. Copy the API Key and API Key Secret somewhere safe. You’ll need these later.

Keep them secret. Keep them safe.

API keys and tokens

You’re almost at the final step. Click App Settings at the bottom right of the screen. Scroll down to App Permissions, click Edit and change your apps permissions to Read and Write. Click Save and confirm the change to permissions in the dialog that appears.

Next, click Keys & Tokens. You can find that under your app’s name, next to Settings. If you ever lose your tokens, this is where you’ll go to generate new ones.

At the bottom of this list, you’ll find a section called Access Token and Secret. Click the generate button and save both of those, too.

New Access Token and Secret

That’s it! You’re all done with the paperwork!

Permissions for access tokens

Note: Double-check that your tokens have been created with Read and Write permissions, otherwise the Twitter API will throw errors when you try to post a new tweet.

Scheduling Tweets With Vapor Queues

Before you start this section, it’s important to go through this checklist to ensure you have everything you need:

  • Starter Project
  • API Key
  • API Key Secret
  • Access Token
  • Access Token Secret

Twitter uses the API Key and API Key Secret to identify your app. Think of them as your app’s username and password. The Access Token and Access Token Secret provide further authentication to let your bot post tweets to your personal account.

Thankfully, you don’t need to know too much about these, as the starter project has done most of the authentication work for you.

What are Queues?

Many web services do a lot more than serve up web pages or respond to API requests. Have you ever wondered how newsletters show up in your inbox on time every week? What about the process that controls sending verification codes or push notifications?

It’s these little workers that make it all happen, and Vapor has them built-in!

You can put jobs into a queue and schedule them to run at specific times or when a certain condition is met. Think of them like little worker bees, waking up to perform a specific task and going back to sleep when they finish.

Of course, this is a simplified explanation. If you’d like to learn more, there’s an excellent tutorial on the subject called Vapor and Job Queues: Getting Started

Your server will use one of these little workers to tweet a quote to your personal account every day at noon.