How To Build A Free DM Twitter Bot

I recently learned how to build a Twitter bot that automatically sends a direct message (“DM”) to new followers saying “Thanks for the follow” and provides information on what i’m working on. And, I’m going to show you how you can do it too!

This is a great opportunity to learn Python, become familiar with Amazon Web Services (“AWS”), and it’s completely free to build.

If you found this article useful, please show your support by following me on Twitter @snyderfinance.

How did I get involved with this project?

I don’t have a background in computer science or programming, but I really wish I did. I see that knowing how to program code to pull data from the web or programming a digital workforce are incredibly useful skills to have.

About a year ago, I began working with Python to introduce myself into programming. There are a ton of resources available online on how to use Python. My favorite resource is Github, which is a website where people create projects and upload code that you can use for free.

Fast forward to last weekend, I found an article written two years ago that explains how to build a similar Twitter bot. However, I think this article skips over some crucial details that I will be providing, and the code written by the author no longer works due to changes made by Twitter impacting how direct messages are sent.

I will show you how to update the code (from my research in Github again) that will have you sending direct messages in no time.

What does a new follower see when they visit your profile?

If you use Twitter, you already know that most people include a link to their website in their profile so anyone who wants more information can find it there.

Another popular feature is to pin a Tweet to your profile, which could be one of your best tweets or it could be a tweet with a link to your latest work.

By creating this Twitter bot, you have another opportunity to connect with your followers, tell them thanks, and share more information about yourself and what you are working on.

Steps to build the Twitter Bot


1.) Download Xcode from the Apple App store. This app makes it easier to write and edit code.

2.) Python 2 is installed by default on Apple computers. However, I recommend downloading HomeBrew, which is a package manager, along with Python 3 so you have it available. This website is a good reference for how to download Python 3 for Mac. 

3.) The next step is to create a Github account. 

4.) Now, sign up for a free AWS account.

Get Access To The Twitter API

1.) Sign up for a developer account with Twitter at the following link. Click apply and login with your normal Twitter account credentials.

2.) Twitter will ask you why you want to get access to their API. I selected the options Hobbyist, and i’m exploring the API.

3.) Add your phone number, if you already haven’t, and verify your account. Then, fill out any remaining information on the Twitter@username tab.

4.) The next tab requests that you provide information on your intended use for Twitter’s API. The answers must be a certain length depending on the question. I’ll give you examples on how I responded that you can use as a reference.

How will you use the Twitter API or Twitter data?
I plan to use Twitter API to practice writing code and enhance my experience using Twitter. I will make a bot that will DM new followers with a message saying "Thank You", and I will provide information on what i'm currently working on.

Are you planning to analyze Twitter data? Yes.

How will you analyze Twitter data including analysis of Tweets or Twitter user?
I plan to analyze Twitter data to find Tweets that better fit my interests as well as find more user accounts that I would like to follow.

Will your app use Tweet, Retweet, like, follow, or Direct Message functionality?
Yes. My app will tweet, retweet, like, follow, and direct message using Tweepy. I will use the API to easily follow accounts that follow me.

Do you plan to display Tweets or aggregate data about Twitter content outside of Twitter?
I don’t intend to display this information so I answered No.

Will your product, service or analysis make Twitter content or derived information available to a government entity? This is No for me.

5.) Review your submission, accept the terms of use, and verify your email.

6.) Next, under your account name, select apps and create an app.

7.) Fill out your apps name and description. You can use the first answer in #4 for the app description and how you will intend to use the app. Don’t worry if you don’t have a website URL. Head to your Twitter profile and copy the website link to this box.

8.) Once your app is created, go to the keys and tokens tab and save the consumer keys, as well as, the generated access token and secret access token. These keys will be needed in order for the bot to work later.

Download Tweepy & Update Code For Sending DMs

1.) On Github, there are two options listed to download Tweepy. The easiest way to install it is using pip to pull it from PyPI.

2.) On your keyboard, press Command (or Cmd) ⌘ + Space to open Spotlight Search. Search and open terminal.

3.) In terminal, type the following: pip install tweepy

4.) Once Tweepy is installed, you will need to locate the API files to make a few updates.
  • Open terminal and type: python
    • Then type,
      • from distutils.sysconfig import get_python_lib
      • print get_python_lib()
    • A file path will appear in terminal, and at the end of the path you will see “site-packages” which is where Tweepy was installed.

5.) Open Finder, and press Control (or Ctrl) + Shift + G to open a search bar to go to a specific folder. Type in the file path from terminal to get to the site-packages folder.

6.) In the Tweepy folder, locate the file. Visit the following link at Github, and add the green highlighted lines of code to the file in the same rows. Once the code is added, save and close the file.

7.) Now, locate the file. Visit the following link at Github, and add the green highlighted lines of code to the file in the same rows. Once the code is added, save and close the file.

Install Boto 3

1.) Boto enables developers to create, configure, and manage AWS services. To download, open terminal and type: pip install boto3

Download Twitter Bot Source Code & Update DM Message

1.) The Twitter bot source code can be downloaded from the following link at Github. Save the zip file to your computer.

2.) In the new-follower-twitter-bot-1.0 folder, there is another folder named resources, and within that folder there is a folder named keyStorage. Use Xcode to create a python file to store your Twitter consumer and access keys in the format below. Name the file and save.

keys = dict(

3.) In the file, scroll down to row 49 and update “def handler” to “def lambda_handler”

Next, scroll down to row 105 and delete the following:
api.send_direct_message(user_id =, text = newDM)

This code is no longer works with Twitter’s API to send direct messages. Instead, insert the following lines of code starting at row 105:

event = {
 "event": {
   "type": "message_create",
   "message_create": {
     "target": {
     "message_data": {
       "text": 'This is a test'

On the line “text”: ‘This is a test’, write the message that you would like your new followers to receive instead of ‘This is a test’.

Save and close the file.

Prepare To Upload Files To AWS Lambda

The Twitter bot source code and the dependent site packages will be uploaded into AWS Lambda. You can set a schedule within Lambda to run the source code at your preferred frequency, and a direct message will be sent to a new follower each time the code runs.

1.) Open the Python site packages folder again, and copy the packages within this folder into the new-follower-twitter-bot-1.0 folder.

2.) AWS does not have Tweepy built in so this needs to be uploaded along with any other packages that the code depends on.

If this was the first time you downloaded Tweepy and Boto3, it is easy to identify the site packages that need to be uploaded to AWS.

If you have a lot of site packages, you can scan through them and exclude the ones you know for sure won’t be needed for the code, and copy the rest of the site packages to the new-follower-twitter-bot-1.0 folder.

4.) Select the twitter bot source code and the site packages, and compress the files into a zip folder within the new-follower-twitter-bot-1.0 folder.

5.) Login into your AWS account, and click on the services tab in the top left of the site. Select the Lambda option under Compute.

6.) Select Author From Scratch.

7.) Create a function name, for example, myTwitterbot.

8.) Under runtime, choose Python 2.7 or Python 3.6 depending on which one you are using.

9.) Under permissions, select create a new role with basic Lambda permissions. I will show you how to modify this role in later steps.

10.) In the Function code section, update the code entry type to “upload a .zip file” and upload the compressed .zip folder from Step #2.

11.) Once the .zip file is uploaded, update the Handler from lambda_function.lambda_handler to main.lambda_handler.

12.) Next, scroll down to the Existing role section and click the “view [name of role] role on the IAM console” link.

13.) Click attach policy and select Create policy.

14.) In choose a service, select DynamoDB and select All DynamoDB actions (dynamodb:*)

15.) Under resources, select all resources and finally Review policy.

16.) Within review policy, enter a name and description, and select Create policy.

17.) Locate the policy that you created and attach it to your existing role.

18.) When you are back to the page with Execution role, select the existing role and save.

19.) In the Designer section on the top of the page, you will now see Amazon DynamoDB under the section that shows the resources that the function has access to.

20.) Scroll down to Basic settings and increase the time from 3 seconds to 5 seconds.

21.) Now, it’s time to add CloudWatch Events to schedule when your Twitter bot will run. Click on Cloud Watch Events and it will move to the “Add triggers from the list on the left” section.

22.) Under Configure triggers, select create a new rule and provide a name and description. The rule type that I used is Schedule expression.

23.) For the Schedule expression section, enter in the rate that you would like your Twitter bot to run. I currently have my bot run once a day so I entered rate(1 day).

24.) Lastly, save your function again and run a test to make sure there are no errors. If there are no errors, congratulations you finished setting up your free Twitter bot.

Tutorial video will be coming at the end of this week!


Popular posts from this blog

Investing In Legal Cannabis: Part 1 CBD