Home
>_

Telegram HN Bot Retro


One of the few projects Ive created that I use daily, HN_telegram_bot is an easy way of keeping track of the posts I have not seen yet that appear on the Hackernews frontpage.

The bot simply allows two commands, '/list' and '/latest' along with a hidden command.

Every hour, the Golang script simply calls the Hackernews API and saves the title and link of any posts that havent already been saved and that have a score over 15 and saves these to a SQLite database.

The '/latest' command then replies with the latest five unread posts from that list. This message then contains a clickable command such as '/add_81727219'. Once clicked this will add the given post to the user's list.

The '/list' command then just displays the user's list. Each of the posts in the list has a clickable link such as '/del_71722981', which when clicked removes the given entry from the user list.

The hidden command is '/ping' which replies with a count of the unread posts and a total of all posts that have been saved in the app. Currently, I have saved over 82182 posts. As mentioned this is the most Ive ever used any of my own creations.

Ive never really had any issues with this thing, looking at the github repo for it, I started it 31 August 2020, so Id say Ive been using it for nearly two years now. Looking at the commits, I sort of finished the first build by September 8th the same year and since then made one commit in May 2021. So I'd had 8 months without an issue and after than 14 months and counting without any issues.

I remember that issue fix aswell now, I had saved too many posts to my list and the message wouldnt return because it was too large, so I added a limit of showing the first 30 posts in the user list.

Golang

I think to be honest the reason Ive had no issues with this thing is because of Golang. I remember it being a decent project to work on at the time as I had had a 'crush' on Golang for a while and finally tried to learn it (well, I think the third time I had tried). Although I obviously used a library for the Telegram bot calls, I still had plenty to implement myself.

In terms of deployment the binary build is just running on one of my servers using a systemd script. I have an ongoing tmux session on the server with its own window for the project where I can quickly look at the systemd script, the directory with the project in and look at the logs. All of the logs are setup to go to journal, so a command like this can see all logs:

journalctl -u telegram-hackernews.service

Redirecting this output to a file, its 942K. Unfortunately I log every command that is sent, so its not as large as I expected but it is a real waste of logging really as I only really need to see whats been sent if theres an issue.

Another issue I can see is that I may not have the systemd file backed up anywhere, although Im sure I just easily found inspiration on the internet and its easy to google. But it would be worth backing up or recording somewhere. Maybe even just at the end of this file.

The thing about the log files is I just dont want to disturb the program, I mean if it aint broke and all that and its not even a megabyte of logs and I literally use this every single day. The whole logging thing definitely makes me think I should just use logging more for like analytics on a website, anyway.


SQLite

A quick mention of SQLite, just the standard it seems great and Ive never had issues with it. Obviously if this thing had more users on it maybe things would be different. But anyway, its the perfect project for it as the things self-hosted so will never get too many users or anything. If I remember right though there is a whole user thing in there, but you can just lock it down to a single user. This is worth mentioning though, that the user stuff should just get removed in the database but keep the user lock that restricts only a single user to using the bot.

The other thing I like about using SQLite is when I have had to nervously update the app, on the very rare occasions, I could just copy the database to a seperate file, ready with all my data if my upgrade fails.


Start

Just made me realise that this project was probably the start of this whole idea of creating open source/free software that is self hostable for me. As I can imagine thinking about the database setup including other users was probably me thinking this was aimed at anybody when I started it. But then I obviously decided against it and just open sourced it. So maybe its the seed for my coming projects/ideas.


Telegram

Ive found telegram to be great, in fact I wish my friends used it really as I like the UI on my phone. The very minor issue I have with it is that when I delete or add, so use a command while in the middle of a message then it flicks to the bottom. However this is just how any messaging app would work. The only alternative to this would be building a web app or something like that, so Im fine with it.


Feedback

Looking through each part of the app the things I have are remove logs; Backup the systemd file and Users in database.

To be honest I can address these easily, the users in the database and the logs Im not going to change, as they are incredibly minor and Id rather just let the app chug along.

The systemd config Ill just drop at the bottom of this post. It is now backed up in my head.


Conclusion

Not alot to say really, Im really happy with this project and how its going using it for almost two years now. I really do use it every day and it is useful to me. Hopefully reflecting on it will help inspire me push on with other projects. Its also nice to think when I beat myself up that I nothing useful that Ive used for a while or whatever that I actually have, this app.


SystemD
[Unit]
Description=Telegram Hackernews bot
ConditionPathExists=/home/telegram-hackernews/
[Service]
Type=simple
Restart=always
WorkingDirectory=/home/telegram-hackernews/
ExecStart=/home/telegram-hackernews/main \
-key=[Botkey-sensitive] \
-user=[userID-sensitive]
PermissionsStartOnly=true
StandardOutput=journal
StandardError=journal
SyslogIdentifier=telegram-hackernews
[Install]
WantedBy=multi-user.target


Note

Very strange, working on my first post and going to publish it/update and Linode is down in the London region. I couldnt connect to it, so in the end I went for a reboot which is taking ages before realising the Linode status that says its down. Ive never noticed downtime before so just thought it was something I was doing. If I wasnt on here, Id have known nothing and now Ive got to wait for the server to come back up and make sure everything on there is back up and running. Annoying but also my own fault I suppose for not checking the Linode status page first.


28 Aug 2022, 3:49 p.m.

Home