DEV Community

Azka
Azka

Posted on

Making a simple twitter autobase bot that posts incoming DM

Script can be found here: https://github.com/azukacchi/twitter_autobase

Description

(Very) simple twitter autobase bot that posts incoming DM

Features

1. Posts incoming DM that contains the trigger word.

2. Posts incoming DM with media. Three ways of sending a post containing media through DM:

  • Upload your own image with the text you want to add. However, DM will not be sent if it contains uploaded attachment other than photos (example: GIF/video). The result is something like this.
    upload image

  • Embed images/gif/video from an existing tweet by copy-pasting the link contained in a tweet with images/gif/video. Example: to embed the image from this tweet, copy the text and take only the link.

The text would be something like this:

190831 인천 스카이 페스티벌 #NCTDREAM #NCT #해찬 #HAECHAN https://pic.twitter.com/r97xH3s15X
Enter fullscreen mode Exit fullscreen mode

Take only the link and paste it into the DM along with the text you want to add and the trigger word like below.

your text hereeeeee lorem ipsum triggerword
https://pic.twitter.com/r97xH3s15
Enter fullscreen mode Exit fullscreen mode

The result is something like this.
embed image

  • To submit a quote-RT of a tweet, simply insert the link of the tweet like below.
this is going to be sent as a quote-RT your text hereeeee triggerword https://twitter.com/timeto_haechan/status/1300093500810170370
Enter fullscreen mode Exit fullscreen mode

The result is something like this.
quote rt

3. Cuts post longer than 280 characters and posts them as a thread. The result is something like this.

4. Sends a DM back for each incoming DM.

  • status sent: DM was successfully posted, the link to the posted tweet will be attached.
  • status notsent: DM was not posted because it does not contain the trigger word.
  • status wrong attachment: DM was not posted because it contains uploaded attachment other than photo. #### 5. Deletes the incoming DMs once they are posted. You can still see the content of the original DM from the link in the notification message you send back to the sender. Optional: you can also delete the notification DM you've just sent by uncommenting this line in senddm method.
#api.destroy_direct_message(int(notifdm.id))
Enter fullscreen mode Exit fullscreen mode

6. Makes a log for each run into test.log file.

What do you need

  1. Make sure you have already installed Python.
  2. A Twitter developer account; sign in to your Twitter account then apply for a developer account here. The time required until your application is approved might varies (mine was 10-14 days 😬). After your application is approved, make sure to change the permission to the "Read, Write, and Direct Messages". Generate the keys and tokens after you change the permission setting.

How to run this

  1. If you have git installed, clone this in your directory. If not, just download the ZIP file.
  git clone https://github.com/azukacchi/twitter_autobase.git
Enter fullscreen mode Exit fullscreen mode
  1. Install the required packages (or check requirements.txt to install each package separately).
  pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Set the required fields in config.py:
    • Put the keys and tokens you get from your Twitter developer account.
    • Set your timezone and the trigger word you want to use.
  2. Modify the notification message as you wish in the senddm() method.

How do I automate the script

You can use Task Scheduler on Windows (open Start then search Task Scheduler) to run the app.py. This app is quite simple to use, the only downturn is it only runs when your computer is either turned on or sleep but not shutdown (obviously). Some helpful tutorials can be found here and here.

Notes

This bot is not suitable for a big autobase account. The checkdm() method only checks the DM to your bot account, without saving the tweets to a dabatase. Then this bot will post the incoming DM as soon as it runs the post_all() method, with a certain time span between the tweets to avoid Twitter API rate limits. To run a big autobase account with higher posts rate, coming from higher followers counts, such as @collegemenfess with 500K+ followers, you'll need to check the dm more often and you'll need a database that enables you to store the incoming DM and post them later.

Questions

I've just started this as a new hobby during pandemic, if you have any questions feel free to reach me on Twitter ahah ☺️

Latest comments (0)