In this article I want to talk about how the Telegram bot was written, which could send articles from Habrahabr, first to Python, and then to Go, and what came of it.
It all began in the summer when Telegram began to penetrate into my life more and more. After writing a couple of test bots in Python, the idea arose to write something for Habrahabr. The first idea was simple: the bot should have just sent notifications about new articles.
After a brief googling, it was discovered that the site has an RSS feed. The process of sending articles immediately simplified: now it was not necessary to parse the entire site, just look at the RSS feed every few minutes (in my case - 10).
The first version of the bot was written pretty quickly. The pyTelegramBotAPI library was used to interact with the Telegram API, and feedparser was used to parse the RSS feed .
In the process of work, the structure became very complicated: now the bot did not just send new articles, but could also sift out those articles that did not contain tags that the user was subscribed to. The implementation was very simple: the SQLite database was used, which had only 2 columns (id and tags), since Python3 had a built-in library for interacting with it. Also added commands for editing tags: the user could delete, add and copy tags. The last thing happened was very simple: the user sent a link to the profile to the bot, after which the tags were searched using (I repent! I did not know that it was bad) a regular expression.
The final program turned out to be quite unstable: it fell quite often (in order to avoid this, we had to wrap a part of the code in while True:
. Do not forget about the regular expression. As a result, the bot, although crooked, worked, so it was decided to leave everything as it is and allow it to live (as it turned out, until the end of February). Sources can (but need?) Find GitHub .
For a long time, the idea to try Go went to my head, but my hands did not reach. And then, finally, free time was found. After reading the documentation, I wanted to write something, and then just the Python bot fell (even while True
did not help). Realizing that this is a sign from above, I began to rewrite the bot on Go.
Unfortunately, I don’t have any real development experience on Go, and there’s no one to ask. Therefore, the solutions that I use, experienced people may not seem the best. If something is really bad, then please write what is wrong. And now let's move on to describing the architecture of the application.
I'll start with the libraries. Telegram -bot-api.v4 is used to interact with the Telegram API, and go-sqlite3 is used to interact with SQLite3. RSS feed parsed with gofeed . To copy user tags, use the analogue Beautiful Soup on Go - soup .
We now turn to the code. There is a main loop that receives new messages from the bot. It defines the type of commands:
for update := range updateChannel { if update.Message == nil { continue } else if update.Message.Command() == "start" { startChan <- update.Message } else if update.Message.Command() == "help" { helpChan <- update.Message ... } else { message := tgbotapi.NewMessage(update.Message.Chat.ID, "...") message.ReplyToMessageID = update.Message.MessageID bot.send(message) } }
As it was possible to understand from the code, each command is processed in a separate goroutine, the transfer of messages is carried out using channels:
startChan := make(chan *tgbotapi.Message, 50) helpChan := make(chan *tgbotapi.Message, 50) getTagsChan := make(chan *tgbotapi.Message, 50) ... // Goroutines go bot.start(startChan) go bot.help(helpChan) go bot.getTags(getTagsChan) ...
It is also possible to send alerts to users. This happens through a special web page.
It was decided to split the project into several packages:
Here is the process of starting the program:
If during the launch or operation of the bot an error occurs that does not allow the program to work normally, the program ends with code 1.
Unfortunately, the problems could not be avoided this time. But they are not connected with the bot's work, but with the development of the program and collecting the executable file under Linux (development takes place on Windows 10, the server runs on Ubuntu 04/16): CGO is needed to run the go-sqlite3
library, and when the go build
command is executed with the CGO_ENABLED=1
flag CGO_ENABLED=1
errors appear. Because of this, you have to compile a project on a virtual machine with Ubuntu.
You may ask: "So why use this bot if there is a website?". I will try to answer. But before that I want to say that the bot is by no means a replacement for the site, just an add-on.
Summing up, I would like to say that the experience of writing a bot on Go was quite interesting and useful (I hope). If you are interested in a bot, you can find it yourself here , and its source code - again on GitHub .
Source: https://habr.com/ru/post/350858/
All Articles