Learn
Michael Calvey
September 7, 2022

Building with Transpose: Create your own ENS Twitter Bot

This guide introduces the key steps required to get a basic Twitter bot up and running, powered by on-chain data. This can provide the basis for building anything; from bots that tweet when particular NFTs in a collection are sold to displaying running leaderboards for web3 games. We’ll explore other fun projects in our Building with Transpose series soon, stay tuned!

Automated posting to Twitter can be a great way to maintain and build engagement for a community. NFT projects, DAOs, and protocols can all benefit from targeted tweets that keep the community up-to-date and engaged. This post will run through the steps required to get set up with a developer account on Twitter and build a simple bot that regularly tweets about ENS expiration activity using data obtained from the Transpose API.

Check out the bot live here

And the full code here

This code contains the sample we use to run our ENS bot and has a flexible system for adding additional bots. We will run through all the required steps to get your own bot up and running.

Overview

  1. Sign up for a Twitter developer account
  2. Create a Twitter project and app
  3. Generate access token and secret access token
  4. Create a Transpose team, register for a tier, and set up a key
  5. Clone the Twitter bot repo and start coding!

Setting up Twitter

For the sake of brevity, this post will focus on the steps that come after getting a developer account ready on Twitter. You can check out guides like https://blog.hubspot.com/website/how-to-make-a-twitter-bot for an in-depth review of the process.

In brief:

  1. Go to developer.twitter.com and log in with an existing account or create a new one
  2. Create a project and app
  3. Generate an access token and secret access token and note these down for later. We’ll use these to authenticate our bot to enable it to post.

Setting up Transpose

Transpose will make getting the data we need for our bot incredibly simple. Getting set up with a free key on Transpose is very quick.

  1. Go to app.transpose.io
  2. Sign up with Gmail
  3. Select a tier and name your team
  4. Name your first key

This will bring you to your new team’s dashboard. Note down your API key.

ENS Names

ENS names are readable names ending in .eth that can be used interchangeably with wallet addresses. ENS names are often treated as people’s web3 usernames, so short, readable names are in very high demand. You register names for a period of time, and great names expire as some people don’t renew their registration. When names expire, they have an associated “penalty price” that halves every day, to give the previous owner a chance to re-register.

Our bot will search through recently expired names and tweet short ones as they approach reasonable penalties.

The Code

Time to get to the actual code! Our tweet bot repo handles automatic periodic updates that let you easily pull relevant information and decide what to post. Posts are cached to make sure the bot doesn’t tweet the same thing twice.

  • main.py loads in all bot implementations in src/bots and runs the updates.
  • Bot implementations are stored in src/bots

Let’s walk through how it works!

Setting up Credentials

  1. Twitter credentials for each bot implementation need to be stored in individual json files under ./credentials/[BOT_NAME]_credentials.json. Make sure [BOT_NAME] exactly matches the name you pick for the bot class implementation so the credentials can automatically get loaded in.

Writing a Bot Implementation

All bot implementations start by inheriting the AbstractBot class. This simplifies interacting with the Transpose SDK and automates ensuring the bot doesn’t tweet the same thing twice.

Init Function

The init function defines the data we want to tweet about, in this case, the time since expiration for ENS names. We pass in the intervals we want to tweet at (time since expiration) as well as the corresponding messages to keep things modular. The name will automatically get set to the name of the file when the bot is initialized.

In the function, we first call the super().init(name) to initialize core bot functionality, then retrieve previous tweets to make sure we don’t double-tweet even if the bot has to restart.

Finally, we set up a couple of simple data structures to store tweets in memory.

Update Function

The update function gets called for every bot instance found in main.py. This is where we define what data the bot will pull and how it will decide whether or not to tweet.

In this code, we:

  • Loop through every period we want to check
  • Retrieve a list of names that match our parameters
  • Make the name hasn’t been tweeted at that interval yet
  • Tweet!

Name Selection

Picking interesting names isn’t necessarily trivial. Initially, the bot just pulled all expiring names shorter than 5 characters but we recently added a library that lets us also include common English words. We’ll leave the decision up to you, but the full code is available in the tweet bot GitHub repo.

Summary

This guide introduces the key steps required to get a basic Twitter bot up and running, powered by on-chain data. This can provide the basis for building anything from a bot that tweets when particular NFTs in a collection are sold to leaderboards for web3 games. We’ll explore other fun projects in our build with Transpose series soon, stay tuned!

Return