Project Soon

Discord Bot

I searched around a bit to find a Discord bot that could read RSS and post it in a Discord server, I found most of them encouraged me to sign up, or use their docker dependency 1, something I tried to use with Alpine Virtual, but somehow it failed. I therefore went to use a previously webhook integration I created years ago for my game servers to notify about server status. After a couple of about a half an hour to investigate the webhook API 2 once more, I found the necessary fields to create an embedded post. This post would probably be the first post that will be posted there, if everything goes as planned.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import feedparser
import requests
from datetime import datetime
from time import sleep

webhook = '<webhook url>'
site = 'https://blog.aposoc.net/index.xml'

def send_embed_to_discord(title: str, description: str, url: str, timestamp: datetime, image: str=None):
	embed = {
			'title': title,
			'description': description,
			'url': url,
			'timestamp': timestamp.isoformat()
		}
	# Will probably never be used unless modifying RSS feed
	if image is not None:
		embed['image'] = {
			'url': image
		}
	data = {
		'embeds': [embed]
	}
	res = requests.post(webhook, json=data)
	if res.status_code != 204:
		print(res.status_code, res.text)

def parse_date(date):
	return datetime.strptime(date, '%a, %d %b %Y %H:%M:%S %z')

# Initial fetch
feed = feedparser.parse(site)

last_updated = parse_date(feed.feed.updated)

try:
	while True:
		sleep(60)
		feed = feedparser.parse(site)
		newly_updated = parse_date(feed.feed.updated)
		if last_updated <= newly_updated:
			for entry in feed.entries:
				published = parse_date(entry.published)
				if published > last_updated:
					send_embed_to_discord(entry.title, entry.description, entry.link, published)
			last_updated = newly_updated
except:
	pass

Apparently Hugo does not integrate images in the RSS feed, so I probably will have to add that myself. The current solution is fine for now, but it would be interesting to add even more features.


  1. https://github.com/synzen/monitorss ↩︎

  2. https://discord.com/developers/docs/resources/webhook ↩︎