/quantpy-twitter-bot

This project utilises Chat GPT to generate twitter (X) posts from a list of quantitative python ideas with a specific template file. Tweets will be posted daily.

Primary LanguageJupyter NotebookMIT LicenseMIT

quantpy-twitter-bot

This project automates the Twitter Feed of QuantPy using the OpenAI and Twitter API's.

📌 Current Features

  • utilises OpenAI Chat GPT to generate twitter (X) posts from a list of quantitative python ideas with a specific template file
  • develop OpenAI prompt template using Jupyter Notebooks environment for testing response
  • tweets are posted to twitter (using tweepy) every 12 hours with the apscheduler module in python
  • scripts are run using the operating system service and therefore from the command line of our remote server we can: check the status, stop or start the twitter bot service
  • we also have access to the logs generated from the twitter bot

⛰️ Task List

  • Project 1: QuantPy Main Twitter Feed Automation
    • Generate Quantitative Finance Twitter Content using OpenAI ChatGPT
    • Schedule & Send Tweets using Raspberry Pi Headless Server
  • Project 2: Automate scraping & publishing of top quality news on Quantitative Finance Topics
  • Project 3: Train & Publish specific Quantitative Finance ChatGPT Model
  • Project 4: Allow users to interact with trained model by tagging @TheQuantPy twitter handle on quant finance twitter posts 🎉 🍾

🛠️ Deployment Steps

These steps are to be followed to create a clone of the twitter bot and run it on a remote server or rapsberry pi (as I have done here).

⚙️ Pre-Steps:

  1. Create OpenAI Account for API
  2. Create API key for ChatGPT API
  3. Create Twitter Account for your bot
  4. Create Developer Account, this can be done for free, however there is a 250 word explanation of use case required.
  5. Add environment variables to a .env file, and have this where you host your app. List of environment variables required and naming convention below:
    • OPENAI_API_KEY
    • TWITTER_API_KEY
    • TWITTER_API_SECRET
    • TWITTER_ACCESS_TOKEN
    • TWITTER_ACCESS_TOKEN_SECRET
    • TWITTER_BEARER_TOKEN

Step 0 (Raspberry Pi): Setup Raspberry Pi

Setup Raspberry Pi in Headless mode, so you can access it like a remote server.

Step 1: Python3.11+ and Git

Ensure python 3.11 is installed, and download git.

sudo apt update
sudo apt install git

Step 2: download github repo to remote server

git clone https://github.com/TheQuantPy/quantpy-twitter-bot.git

Step 3: create & activate virtual env

3a. Enter the github project main folder

cd quantpy-twitter-bot

3b. create and activate virtual env

python -m venv env
source env/bin/activate

3c. download requirements

pip install -r requirements.txt

Step 4: setup twitter bot service using systemctl

Note

Important that twitter_bot.service file is installed properly on host server

We will run out twitter bot script as a service in the operating system so it can hva the following functionality:

  • start,
  • stop,
  • restart, and
  • status.

We will setup our bot script as a service using linux operating system sysemd, which can be commanded using systemctl.

4a. Setting up the service to run our script in daemon mode on the server:

cd /lib/systemd/system/
sudo nano twitter_bot.service

4b. Add the Following Text and save the service file:

[Unit]
Description=QuantPy Twitter Bot Service
After=multi-user.target

[Service]
Type=simple
ExecStart=/home/quantpy/quantpy-twitter-bot/env/bin/python /home/quantpy/quantpy-twitter-bot/main.py
Restart=on-abort

[Install]
WantedBy=multi-user.target

4c. Now that we have our service we need to activate it:

sudo chmod 644 /lib/systemd/system/twitter_bot.service
chmod +x /home/quantpy/quantpy-twitter-bot/main.py
sudo systemctl daemon-reload
sudo systemctl enable twitter_bot.service

4d. Now we can access the following commands

To start service:

sudo systemctl start twitter_bot.service

To check status:

sudo systemctl status twitter_bot.service

To stop service:

sudo systemctl stop twitter_bot.service

To check service logs:

sudo journalctl -f -u twitter_bot.service

The MIT License (MIT)

Copyright © 2023 QuantPy Pty Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  • The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.