Creating a Twitter Bot in Python Using Tweepy (Part-2)

Automation TWEEPY SERIES Image

    Deploying Bots to a Server Using Docker

    Docker provides a way to package any app in a Docker image containing not only the app itself, but all its dependencies too.

    Building the Docker Image

    To package your bot or application, you have to create a Dockerfile in the project’s root directory. This file contains a set of instructions used to create the Docker image containing your app.

    This is how your project structure should look:

    ├── bots/
    │   ├──
    │   └──
    │   └──
    │   └──
    ├── requirements.txt
    └── Dockerfile

    As you can see, the Dockerfile and requirements.txt live inside your project top directory.

    You can use the following Dockerfile to create the image for the Fav & Retweet bot. It uses Python:3.7-alpine as the base image. We chose this image as the base since it’s very small and contains Python3.7 and pip. This Dockerfile then copies the bot code and the requirements.txt file to the image. Finally, the dependencies are installed inside the image using pip3:

    FROM python:3.7-alpine
    COPY bots/ /bots/
    COPY bots/ /bots/
    COPY requirements.txt /tmp
    RUN pip3 install -r /tmp/requirements.txt
    WORKDIR /bots
    CMD ["python3", ""]

    The final line says what command must be run when this image is used.

    Now you can build the image using this command:

    $ docker build . -t fav-retweet-bot

    Then, using the docker images command, you can see the details of the newly generated image.

    Finally, you can use the docker run command to test the image, passing it the environment variables holding the authentication credentials:

    $ docker run -it -e CONSUMER_KEY="your-key-here" \
     -e CONSUMER_SECRET="your-secret-here" \
     -e ACCESS_TOKEN="access-key-here" \
     -e ACCESS_TOKEN_SECRET="access-token-here" \

    While it’s running, you’ll see the bot’s logs output on the screen.

    This same procedure can be used to create the Docker image for other bots.

    Example: Deploying a Twitter Bot to Amazon AWS

    Now that the Docker image is ready, there are a lot of ways to deploy it to run in any server. In this section, you’ll see one method you can use to deploy a bot to a server in Amazon AWS. This setup uses an Amazon AWS EC2 instance, like a virtual server, to run the Docker image.

    Note: You need an Amazon AWS account to follow these steps. All the resources used in this article are eligible for the AWS free tier, so if you are new to AWS, then you won’t be charged.

    Step 1: Export The Docker Image

    From the computer where you have created the Docker image, run these commands to export it to a file and compress it. You’re going to use this file later to upload the image to your EC2 instance:

    $ docker image save fav-retweet-bot:latest -o fav-retweet-bot.tar
    $ gzip fav-retweet-bot.tar

    Now the image has been compressed into fav-retweet-bot.tar.gz.

    Step 2: Create an Amazon EC2 Instance

    The first step in AWS is to create a new EC2 instance. The bot will run inside this instance. Log in to your AWS console, choose the EC2 service, and select Instances. You’ll see something like this:

    Create an EC2 instance

    Click the Launch instance button and select Ubuntu Server 18.04 as the base image:

    Select EC2 instance base image

    Now you can choose the instance type. This depends on the computing resources needed by your bot. For the bots described in this article, the t2.micro instance type is a good starting point. Select it and click Review and Launch:

    Select EC2 instance type

    On the next page, you can review and check your configuration. Then click the Launch button to begin the creation of your EC2 instance:

    Review and launch EC2 instance

    After you’ve clicked Launch, you’ll be asked to create a key pair. It’s used to allow you to connect to your instance and deploy the bot Docker image. Select Create a new key pair, enter a key pair name, and download the key pair.

    Remember where you’re downloading the key pair. You’ll need it later.

    Then click the Launch instances button to proceed:

    Create key pair

    You will see a page saying that your instance is being created. Click the View instances button to see the status of your instance:

    Instance is getting created

    At first, your instance state will be pending. You have to wait until it transitions to running:

    EC2 instance is in peding status

    EC2 instance is ready

    Step 3: Install Docker in Your EC2 Instance

    Now you have to connect to your instance using SSH to install Docker on it.

    If, in the previous screen, you right click on your instance, you’ll find a context menu. Select the Connect option to see how to connect using SSH:

    Context menu for EC2 instance

    This dialog shows how to connect to your instance:

    EC2 connection dialog

    Note: is the address that AWS assigned to the instance used in this article, but it will probably be different for you.

    First, change the permissions of your key pair file using the following command. Otherwise, you won’t be able to connect to your EC2 instance using this key pair:

    $ chmod 400 mykeypair.pem

    Then connect to your instance using SSH. You can copy the command you need to execute from the Connect To Your Instance dialog:

    $ ssh -i "mykeypair.pem" [email protected]

    Once you’re connected, run the following commands to install Docker in the instance:

    [email protected]:~$ sudo apt-get update
    [email protected]:~$ sudo apt install
    [email protected]:~$ sudo adduser ubuntu docker
    [email protected]:~$ exit

    It’s important that you exit the SSH session after the installation, so that in the next login your user permissions will be reloaded.

    Step 4: Upload Your Bot’s Docker Image

    From your local computer, upload the bot Docker image to your instance using scp. This can take some time depending on your internet connection:

    $ scp -i "mykeypair.pem" fav-retweet-bot.tar.gz \
        [email protected]:/tmp

    When the image upload finishes, log in again to your instance using SSH:

    $ ssh -i "mykeypair.pem" [email protected]

    Run these commands in your instance to decompress and import the Docker image:

    [email protected]:~$ gunzip /tmp/fav-retweet-bot.tar.gz 
    [email protected]:~$ docker image load -i /tmp/fav-retweet-bot.tar

    Step 5: Run Your Bot’s Docker Image

    The final step to deploy the bot is to run the Docker image in your EC2 instance, passing it the authentication credentials.

    You can do this using the docker command. Passing the flags -d and --restart-always assures that the bot will keep running if you disconnect from the SSH session or if the instance is restarted:

    [email protected]:~$ docker run -d --restart always \
    -e CONSUMER_KEY="your-key-here" \
    -e CONSUMER_SECRET="your-secret-here" \
    -e ACCESS_TOKEN="your-token-here" \
    -e ACCESS_TOKEN_SECRET="your-token-secret-here" \

    Using docker ps, you can check that the bot is running and find its container id. Finally, using the docker logs command and the container id, you can check the output from the bot to see if it works properly:

    $ [email protected]:~$ docker logs e6aefe73a885
    INFO:root:API created
    INFO:root:Processing tweet id 1118276911262785538
    INFO:root:Processing tweet id 1118276942162214918
    INFO:root:Processing tweet id 1118276990853951488
    INFO:root:Processing tweet id 1118277032360722433
    INFO:root:Processing tweet id 1118277034466324480

    Now, even if you disconnect from the SSH session or turn off your computer, the bot will continue to run on your AWS EC2 instance.

    Free AWS Resources

    To save resources and money (if your AWS free tier period has ended), you can stop or terminate your EC2 instance:

    EC2 terminate or stop and instance

    Stopping it will allow you to restart it in the future, but it will still consume some resources related to your instance storage. By contrast, if you Terminate an EC2 instance, then all its resources will be released, but you won’t be able to restart it.

    There’s a lot more to using AWS, like creating an AWS image of your instance, but that goes beyond the scope of this blog.

    Wrapping Up

    Building your own Twitter bots allows you to take your Twitter presence to the next level. By using bots, you can automate content creation and other Twitter activities. This can save you a lot of time and give your audience a better experience.

    The Tweepy package hides many low level details of the Twitter API, allowing you to focus on the logic of your Twitter bots.

    You can use the bots in this article as a starting point to automate part of your Twitter activity. Don’t forget to take a look at the whole Tweepy API documentation and use your imagination to make more complex bots that are meaningful to your use case.

    Tags: #Coding, #Python


    To add a comment, please Signup or Login