How to Quickly Append Your Bot Profiles to Add a Label to Indicate They are Bots

Jessica Garson - Mar 27 '20 - - Dev Community

This tutorial was originally posted to the Twitter developer blog.

Recently we launched a new Developer Policy that has updated rules for the labeling of bots. If you are creating Twitter bots and want to ensure your bots are in compliance with the new labeling requirement, we’ve created a Python script to help you quickly add a label to your bots.

This script adds “#TwitterBot created by @handle” to the end of a profile bio.

My current profile is as follows:
Developer Advocate @Twitter. Python programmer. Noted thought leader on vegan snacks. Makes music as Messica Arson.

After I’d run this script, my profile would look like this:
Developer Advocate @Twitter. Python programmer. Noted thought leader on vegan snacks. Makes music as Messica Arson. #TwitterBot created by @jessicagarson

To use this script you will need to have Python 3 installed on your machine. This script uses tweepy, one of many open-source libraries built by the Twitter developer community. You will need to have this package installed.

You can run the following command in your terminal to install this package. In order to use pip, you will need to install pip first.

pip install tweepy
Enter fullscreen mode Exit fullscreen mode

To get started with this script you will need to clone this repository.

git clone https://github.com/twitterdev/bot-profile-append.git
Enter fullscreen mode Exit fullscreen mode

From here, you will need to edit the configuration file that is in the repository as secrets.yaml. This file contains your consumer key, consumer secret, access token, token secret, and user id. For more information about locating your keys and tokens check out our documentation on the subject. You are going to want to make sure you add this file to your .gitignore before you push it to GitHub. For more information about working with a .gitignore file check out this page. You also might want to review our guide on keeping tokens secure.

You will need to replace the x’s with your own credentials in the secrets.yaml. You will also need to just update the configuration file to the number of bots you have. Keep in mind that our policies only allow you to have 10 applications associated with a developer account. You will also need to add your Twitter handle without the @ symbol to the admin section next to the key handle.

Your secrets.yaml should look similar to this example:

admin:
  handle: "yourhandle"
bots:
  bot1:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot2:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot3:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot4:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot5:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot6:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot7:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot8:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot9:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
  bot10:
    CONSUMER_KEY: "xxxxxxxxxxxxxxx"
    CONSUMER_SECRET: "xxxxxxxxxxxxxxx"
    ACCESS_TOKEN: "xxxxxxxxxxxxxxx"
    TOKEN_SECRET: "xxxxxxxxxxxxxxx"
    USER_ID: "xxxxxxxxxxxxxxx"
Enter fullscreen mode Exit fullscreen mode

After you’ve set up your configuration file you should be ready to run your script. You will first need to be in the directory you cloned and run the script.

cd bot-profile-append.py
python bot-profile-append.py
Enter fullscreen mode Exit fullscreen mode

If one of your accounts is nearing or at the 160 character limit you will see the following error:

[{'code': 120, 'message': 'Account update failed: Description is too long (maximum is 160 characters).'}]
Enter fullscreen mode Exit fullscreen mode

At this point, you will see that your bots should now have #TwitterBot created by @[your Twitter handle] appended at the end of their profile. While creating bots with the Twitter API, be sure to take a look at the Developer Agreement or your Master Licensing Agreement or Order (if applicable), Automation Rules, Display Requirements, and the Twitter Rules and ensure what you are building is in accordance with them.

I used several libraries and tools beyond the Twitter API to create this code sample, but you may have different needs and requirements and should evaluate whether those tools are right for you. This script is just one method you can use to make sure you are labeling your bots accordingly. And please share your examples of this code in action with us by Tweeting at @TwitterDev.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .