Sends a message to a Discord channel when a new post is published on a website.
Go to file
2024-09-15 11:58:04 +10:00
README.md first commit 2024-09-15 11:55:45 +10:00
requirements.txt first commit 2024-09-15 11:55:45 +10:00
webhook.py first commit 2024-09-15 11:55:45 +10:00

Discord cardiParty webhook

This is a simple webhook to push new cardiParties into the cardiParty channel.

install

  1. Create and activate virtual env
  2. Install requirements: pip install -r requirements.txt
  3. Set up a cronjob
    1. set the env values (see below)
    2. set a time to run e.g. every 30 mins
    3. set the command listed below under "Cron job command"
  4. Test cronjob works
  5. Relax

How it works

We run this on the newcardigan wordpress server.

  1. With a cron job we call the civiCRM API use cv to grab the details of the latest cardiParty
  2. We pipe that string into a python script
  3. The script checks a custom RSS feed for new parties
  4. If no new party, end
  5. If new party,
    1. save the guid to latest_post.txt for the next round of checking
    2. use the image and title from the RSS feed and the details from the cv API call
    3. publish via a webhook to Discord

env values

We need to include some secrets via environment values:

channel is the channel ID (DISCORD_CHANNEL) token is the secret token for the webhook (DISCORD_TOKEN) cardiparty_ping is the ID of the @cardiParty ping role (DISCORD_CARDIPARTY_PING)

Cron job command

cv api4 Event.get '{"select":["title","address.city","start_date","summary"],"join":[["Address AS address","LEFT",["loc_block_id.address_id","=","address.id"]]],"orderBy":{"start_date":"DESC"},"limit":1}' | ./webhook.py

This should pipe a string like this into the script:

[{"id":108,"title":"Melbourne Art Library","address.city":"Melbourne","start_date":"2024-04-06 16:30:00","summary":"Melbourne Art Library (MAL) is a not-for-profit lending library that collects specialised art and design texts. They are proudly independent and are curious about what being a 'library' means."}]

RSS feed

The rss feed is https://newcardigan.org/category/cardiparties/?feed=cardipartyfeed.

This special cardipartyfeed is a slightly modified RSS2 feed. The only thing it does differently is include the featured image in an enclosure so that we can pick it up directly from the feed. See the newcardigan WordPress theme for how this works.