Skip to content

kensand/rss-lemmy-bot

Repository files navigation

rss-lemmy-bot

A Lemmy bot intended to scan RSS feeds and post them to Lemmy communities.

How it works

This bot takes in a mapping of RSS feeds to sets of Lemmy Communities (Map<Feed, List>), will scan the RSS feeds at intervals, and post new items in those feeds to their respective Lemmy communities.

The bot does its best to avoid posting the same item from a feed twice. It does this by recording its start time and only posting items that are after it. Any time it posts an item for a given feed, it records the timestamp for that item, and will not post any other items older than that timestamp for that feed. Items will be posted in order of their published dates. All aforementioned timestamps are volatile and will be lost at exit. You may override the start time using the START_TIME environment variable - see Configuration for formatter.

Usage

Prerequisites

If running locally, ensure NodeJS >= 20 is installed.

Otherwise, docker and docker-compose may be desired.

After creating your Lemmy bot account on the server, go into the settings for the user and check the "bot account" checkbox.

Configuration

config.json

Copy config.template.json to config.json and configure it.

The defaultSchedule and per-feed schedule are optional. The default is 1 hour.

Post formatting can be overriden using mustache templates, and can be applied to post title, body, and url. The view provided to mustache takes the following model:

interface View {
    link?: string;
    guid?: string;
    title?: string;
    pubDate?: string;
    creator?: string;
    summary?: string;
    content?: string;
    isoDate?: string;
    categories?: string[];
    contentSnippet?: string;
    timestamp: Date;
    nsfw: boolean;
    linkPrefix?: string;
}

The default mustache templates are:

{
  "title": "{{{title}}}",
  "body": "{{{summary}}}",
  "url": "{{#linkPrefix}}{{{linkPrefix}}}{{/linkPrefix}}{{{link}}}"
}

Environment Variables

The START_TIME environment variable may be used to override the bots start time for back-filling or testing purposes. This should take the formatter of a string that can be parsed by the JavaScript Date constructor, such as 2023-06-27T14:48:53.123+00:00.

The CONFIG_PATH environment variable may be used to set the path to the configuration file. The default is ./config.json. In the docker container, the working directory is /rss-lemmy-bot, so the default absolute path is /rss-lemmy-bot/config.json.

You may override the defaultSchedule and the feed-specific schedule using a toad-scheduler SimpleIntervalSchedule. See config.ts for the exact formatter.

Run Locally

One time setup:

npm install && npm run build

Startup:

npm run start

Docker

docker run -v './config.json:/rss-lemmy-bot/config.json' -t kensand/rss-lemmy-bot

Docker Compose

See docker-compose.yml

Development

Harness

A docker-compose.yml file to act as a test harness is available in ./dev/docker.

docker stack deploy -c dev/docker/docker-compose.yml rss-lemmy-bot-harness

This will start a lemmy server on your localhost available at 127.0.0.1:80.

You will need to create the relevant communities in the test harness. A utility script is provided for this:

CONFIG_PATH=./dev/config.json \
npm run setupHarness

Running Locally

If you are using the test harness, you can use a provided dev config ./dev/config.json

CONFIG_PATH=./dev/config.json \
START_TIME=0 \
npm run start

Building Docker Image

docker build . -t rss-lemmy-bot:dev

Running Docker Image

docker run -v "./dev/config.json:/rss-lemmy-bot/config.json" -e START_TIME=0 --network host rss-lemmy-bot:latest