Huginn + Reddit on Google Cloud

27 January 2021

gcloud huginn self-hosted

I have been using IFTTT to send the host posts of /r/worldnews to me via Telegram. I have no issues with using IFTTT however, one day I recieved this notification.

IFTTT Applet Limit Notification
IFTTT Applet Limit Notification

Honestly I don’t read the host posts frequently, so I don’t wish to pay for IFTTT Pro just to maintain the applet.

The first thing I turned to was self-hosted solutions, and a quick research later, I found Huginn after reading Huginn: An Open-Source, Self-Hosted IFTTT

Hosting on Google Cloud Platform

You need a server to run Huginn and one such place is Heroku. You can use Heroku to self-host Huginn, but I am going to use GCP’s free tier (Google Cloud Platform) instead.

Google Cloud Free Tier

Google Cloud Free Tier

The F1 instance offers

  • 600MB ram
  • 30GB HDD
  • 1GB network egress

which is plenty for what I am trying to do.

To setup the compute engine (a.k.a VPS) properly, follow this blog post https://jiayiliu.me/post/2020-08-21-huginn-docker-tmux-gcp/.

Huginn Overview

Huginn can be used for many purposes like:

  • web scraping
  • sending and receiving WebHooks
  • running custom JavaScript functions
  • sending email digests
  • and many more

But what I am interested here is to:

  1. Read /r/worldnews RSS feed
  2. Send each feed item as a message in Telegram via Telegram’s bot API

Huginn Agent

Agents monitor and acts on our behalf, and one such agent that we are going to look at is the Website Agent.

Website Agent
Website Agent

taking a closer look at the options

Website Agent Options
Website Agent Options

We can see that the XKCD website agent extracts the url, title and hovertext using CSS values and passes the data to the receiver Comic Formatter.

Putting everthing together, we get something like this

Huginn Agent Flow
Huginn Agent Flow

Setting up Huginn Agent

Now that we have a rough idea of how Agents work, it is time to set up the agents to read from Reddit’s RSS feed.

There are 3 Agents that we need to set-up

  1. Website Agent to read from Reddit’s JSON feed
  2. Event Formatting Agent to format the JSON feed into a readable message
  3. Telegram Agent to send a message to myself

1: Website Agent for Reddit JSON Feed

First set up the Website Agent with these options

{
  "expected_update_period_in_days": "2",
  "url": "https://www.reddit.com/r/worldnews.json",
  "type": "json",
  "mode": "on_change",
  "extract": {
    "title": {
      "path": "data.children[*].data.title"
    },
    "url": {
      "path": "data.children[*].data.url"
    },
    "permalink": {
      "path": "data.children[*].data.permalink"
    },
    "created": {
      "path": "data.children[*].data.created"
    },
    "author": {
      "path": "data.children[*].data.author"
    }
  }
}

2: Event Formatting Agent to Format Message

Then create an Event Formatting Agent to format the message.

Huginn uses Liquid as their template engine.

{
  "instructions": {
    "agent": "{{agent.type}}",
    "text": "*{{title}}*{% line_break %}{% line_break %}[{{url}}]({{url}}){% line_break %}{% line_break %}[https://www.reddit.com{{permalink}}](https://www.reddit.com{{permalink}}){% line_break %}{% line_break %}{{created | round | date: '%d/%m/%Y %H:%M:%S'}}"
  },
  "matchers": [],
  "mode": "clean"
}

3: Telegram Agent to Send Messages

There are 3 options here to set

  • Auth token
  • Chat
  • Parse mode

To get your Auth Token

  1. Use BotFather to create your auth token
    Huginn Telegram Agent BotFather
    Huginn Telegram Agent BotFather
  2. Open a conservation with the bot by visiting https://telegram.me/YourHuginnBot
  3. Select the chat_id from the dropdown
Huginn Telegram Agent Options

Huginn Telegram Agent Options

Overview

After setting everything up, you should have a flow like this

Huginn Final Flow
Huginn Final Flow

From here, you should be able to receive notifications everytime the Reddit JSON feed is updated.

Huginn Telegram Bot
Huginn Telegram Bot