Huginn + Reddit on Google Cloud
27 January 2021
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.
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.
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:
- Read /r/worldnews RSS feed
- 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.
taking a closer look at the 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
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
- Website Agent to read from Reddit’s JSON feed
- Event Formatting Agent to format the JSON feed into a readable message
- 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
- Use BotFather to create your auth token
- Open a conservation with the bot by visiting https://telegram.me/YourHuginnBot
- Select the
chat_id
from the dropdown
Overview
After setting everything up, you should have a flow like this
From here, you should be able to receive notifications everytime the Reddit JSON feed is updated.