feat(bot): support backfilling RSS feed history
All checks were successful
Python Package CI/CD / Setup and Test (push) Successful in 1m12s
Python Package CI/CD / Publish to PyPI (push) Successful in 1m5s

Added the ability to backfill messages from an RSS feed upon adding it to a room. Users can now add the `--backfill` flag when using the `addfeed` command to retrieve and post historical entries from the feed. This enhancement improves user experience by ensuring no important feed information is missed.
This commit is contained in:
Kumi 2024-06-16 08:28:29 +02:00
parent acaf7b17f4
commit 79391d1cc5
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 43 additions and 3 deletions

View file

@ -7,7 +7,7 @@ allow-direct-references = true
[project]
name = "matrix-rssbot"
version = "0.1.4"
version = "0.1.5"
authors = [{ name = "Private.coffee Team", email = "support@private.coffee" }]

View file

@ -582,6 +582,19 @@ class RSSBot:
) < self.loop_duration:
await asyncio.sleep(self.loop_duration - time_taken)
async def backfill_feed(self, room: MatrixRoom, url: str):
self.logger.log(f"Backfilling feed {url} in room {room.room_id}", "debug")
feed_content = await self.fetch_feed(url)
for entry in feed_content.entries:
entry_message = f"__{feed_content.feed.title}: {entry.title}__\n\n{entry.description}\n\n{entry.link}"
await self.send_message(
room,
entry_message,
(await self.get_event_type_for_room(room)) == "notice",
)
async def run(self):
"""Start the bot."""

View file

@ -8,7 +8,24 @@ import feedparser
async def command_addfeed(room: MatrixRoom, event: RoomMessageText, bot):
url = event.body.split()[2]
args = event.body.split()[2:]
if not args:
await bot.send_message(room, "Please provide a feed URL to add.", True)
return
if "--backfill" in args:
backfill = True
args.remove("--backfill")
else:
backfill = False
if len(args) != 1:
await bot.send_message(room, "Please provide only one feed URL.", True)
return
url = args[0]
bot.logger.log(f"Adding new feed to room {room.room_id}")
@ -67,6 +84,16 @@ async def command_addfeed(room: MatrixRoom, event: RoomMessageText, bot):
return
await bot.send_message(room, f"Added {url} to this room's feeds.", True)
if backfill:
await bot.send_message(
room,
"Backfilling messages from the feed. This may take a while, depending on the feed size.",
True,
)
await bot.backfill_feed(room, url)
except Exception as e:
await bot.send_message(
room, "Sorry, something went wrong. Please try again.", True