meroupatate / pytricks-discord-bot

Be notified on Discord when you receive a PyTrick from the Real Python Newsletter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PyTricks Discord bot

Be notified on Discord when you receive a PyTrick from the Real Python newsletter.

About

PyTricks are short python snippets sent to your email inbox by the Real Python team. If you haven't done so, you can subscribe to their newsletter here.

This script fetches the PyTricks you receive on the Gmail API and sends them to a Discord server. Example

Prerequisites

  • a Gmail address subscribed to the Real Python newsletter
  • python3 (version > 3.6)
  • python3-pip

Installing

  1. Clone the repository:
git clone https://github.com/meroupatate/pytricks-discord-bot.git
  1. Install the python dependencies:
cd pytricks-discord-bot
pip3 install -r requirements.txt
  1. Follow the Python Quickstart tutorial to get you Gmail API credentials:
  • Enable the Gmail API
  • Save your credentials.json file
  • Copy the script quickstart.py and make sure to allow read AND write operations by replacing the line
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']

by

SCOPES = ['https://www.googleapis.com/auth/gmail.modify']

(this script won't work as expected if it has no write permissions)

  • Run the modified quickstart.py on a machine with a web browser and give the required authorizations when they ask you to This script should generate a token.pickle file that you will need to connect to the Gmail API.
  1. Once done with the steps above, put token.pickle in the git repository you previously cloned:
mv token.pickle /path/to/pytricks-discord-bot
  1. Edit .env.example and replace the link with a webhook from your Discord server (click here for more information on how to create your webhook):
mv .env.example .env
vim .env
# DISCORD_WEBHOOK = 'https://discordapp.com/api/webhooks/xxxx/yyyyyyy'
  1. If everything went fine, you should now be able to run python get_tricks.py. You can keep it running by creating a dedicated systemd service:

An example of /etc/systemd/system/pytricks.service file for a cloned repository located at /home/patate/pytricks-discord-bot:

[Unit]
Description=PyTricks service
After=multi-user.target

[Service]
Type=idle
WorkingDirectory=/home/patate/pytricks-discord-bot/
ExecStart=/usr/bin/python3 /home/patate/pytricks-discord-bot/get_tricks.py
Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

Start your service by running systemctl start pytricks.service and receive your PyTricks on Discord :D

About

Be notified on Discord when you receive a PyTrick from the Real Python Newsletter


Languages

Language:Python 100.0%