Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Building Your Own Bot

Cameron Rodriguez edited this page Apr 4, 2020 · 9 revisions

If you would like to make your own version of the @XKCDAltTextBot Twitter account, there are a few changes that will need to be made to get it setup for your usage. Follow the steps below to get started.

Preparing your Development Environment

The following instructions are based on using Windows. Modify the steps as necessary for your operating system.

You will need to install Python 3.8, Git, and Pipenv to modify, maintain, and run your bot. You will need access to the command line (initial setup only), a reliable Internet connection, and the ability to run Python files; however you do not require full-system access.

Steps:

  1. Setup Git, if you never have before.
  2. Create a folder suitable for development, open the command line and clone the source code with git clone https://github.com/cam-rod/XKCDAltTextBot.git.
  3. Install the dependencies by running pipenv install.

If you're new to Git, you should read the Git Pro book. It's an excellent guide to help you get started.

Getting ready on Twitter

You will need to prepare a Twitter account for this bot by becoming a developer. Once your create your account, go to dev.twitter.com and apply for a developer account by following the instructions. You do not need to fill in the following fields for this Twitter app: Callback URLs, Terms of Service URL, Privacy Policy URL. Take note of the handle of your Twitter account, the name of your developer app, and the account you would like to target; all of these are required to run the bot.

Once your account is setup, navigate to the Apps page and open Details. Under the Permissions tab, click Edit and change Access permission to Read and write and save. Finally, switch to the Keys and Tokens tab, and Regenerate/Generate your Consumer API keys and Access token & access token secret. Take note of all 4 keys and tokens, you will need these to communicate with the Twitter API.

URL and image locations

Not all sites and bots are like @xkcdComic and xkcd.com, where the link to the site and the image are the first to appear. The following steps will help you handle that.

URL location

The XKCDAltTextBot program searches for the most recent Tweet, and then grabs a specified link in the Tweet body and uses that to access the website. Look at a Tweet from the account you are targeting, and see where the link you need falls. Then, subtract one from that position and note it. For example, if you need the second link, note the number 1.

Image location

The XKCDAltTextBot program access the alt/title text from a specified image with title text. However, some sites may have multiple photos which fit this description. To specify the photo to use, you will need to determine where it always falls.

Ensure that BeautifulSoup and Requests are installed on your system (by running pipenv install), and then open the command line. With an example webpage of the format you expect as SITE, enter the following commands:

html_raw = requests.get(SITE)
html = BeautifulSoup(html_raw.text, 'html.parser')
html.find_all('img', title=True)

This command will output a list of photos with title text. Once you find the image you would like to use (checking the site yourself to verify), note one less than its location within the list. For example, if you need the first photo, note the number 0.

Creating a configuration file

Create a copy of config.yaml.example and rename it to config.yaml. It is important that you use the name config.yaml, otherwise the program will not work and your secret keys and tokens may be exposed. Then, substitute in all of the previous mentioned numbers, accounts, keys and tokens as follows:

  • API Key: This is your main API key, from the Twitter Developer site. It forms part of the OAuth header used to verify commands sent to the Twitter API.
  • API Secret Key: This is your main API key, from the Twitter Developer site. It forms part of the OAuth header used to verify commands sent to the Twitter API.
  • Access Token: This is your main access token, from the Twitter Developer site. It forms part of the OAuth header used to verify commands sent to the Twitter API.
  • Access Token Secret: This is your access token secret, from the Twitter Developer site. It forms part of the OAuth header used to verify commands sent to the Twitter API.
  • Target name in logs: This is how whatever topic you are targeting appears in logs. For example, this bot uses XKCD, so log messages include:
    • Searching for new XKCDs...
    • No new XKCDs found. Sleeping for 15 seconds...
    • Twitter search returned existing XKCD. Sleeping for 15 seconds...
  • Target account handle: The username of the account that your bot will check. Do not include the @ symbol. For example, @XKCDAltTextBot uses xkcdComic.
  • Tweet URL location: One less than the location of the URL (that your bot grabs the image from) within the Tweet. For example, @XKCDAltTextBot this is 0.
  • Target image location on site: One less than the location of the image with title text you are trying to access. For example, @XKCDAltTextBot uses 0.
  • Your account handle: The username of the account associated with the bot. Do not include the @ symbol. For example, @XKCDAltTextBot uses XKCDAltTextBot.
  • Heroku bot name: If you are using Heroku to host your bot, enter the name of the Heroku app here. This will be used to load environment variables.

Save the file and ensure the name is config.yaml.

(Optional) Changing the file name

If you want, you can also change the name of the bot's Python file itself (xkcd_alt.py by default) to make it more applicable to your bot. If you choose to do this, you must open the Procfile and change worker: python xkcd_alt.py to worker: python BOT_PROGRAM_NEW_NAME.py.

Next Steps

That's all the changes you need to make to get your bot running. Now you can go forward to the next step, running the bot.

← Home // Running Your Bot →

Clone this wiki locally