ImgMacroBot — Telegram inline bot to generate image macros on the fly. 
Tags: telegram, showdev
Published: 2021-03-15 22:00:26

Sometimes I want to reply with a meme image in a Telegram conversation. You know, when a colleague asks you why did it take you so long to fix a simple issue, and you feel like:

![One does not simply fix a bug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bejy80mpbjmy18mryr5p.png)

Maybe those images make you feel like it's 2007 and you are on an imageboard, but I still like some of them. At the same time, I am too lazy to open an online generator, find an appropriate template, generate a meme and post it as an answer.

So, I decided to automate that with an [inline Telegram bot](https://core.telegram.org/bots/inline).

Meet [@ImgMacroBot](https://t.me/ImgMacroBot), a bot who can generate image macros on the fly in Telegram conversations.

The usage is simple. To generate an image above, just type "@ImgMacroBot one does not simply fix a bug" in any Telegram chat. If you've ever used inline bots like [@pic](https://t.me/pic), [@youtube](https://t.me/youtube), or [@imdb](https://t.me/imdb) you should know what happens next. Your Telegram client will prompt you some of the results matching your query. You can select one of those results and post it in a conversation.

Like this:

![Demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbkw7uht08s1mytw60jm.gif)

That's basically it. The list of supported templates [is short](https://github.com/madhead/ImgMacroBot#readme), but adding new macros is not a big deal due to the clean architecture I've used :) If you have an idea — just [submit an issue](https://github.com/madhead/ImgMacroBot/issues/new?assignees=madhead&labels=macro&template=macro-request.md&title=).

Technically, the bot is written in [Kotlin](https://kotlinlang.org) using [Ktor](https://ktor.io) and [Koin](https://insert-koin.io). It's a single endpoint web service, listening for [Telegram Bot API webhooks](https://core.telegram.org/bots/webhooks). Text is drawn using [Oswald](https://fonts.google.com/specimen/Oswald) font (I need Cyrillic, not supported in [Anton](https://fonts.google.com/specimen/Anton)) with [Skija library](https://github.com/JetBrains/skija), a Java wrapper for [Skia](https://skia.org), a 2D library powering your phone and browser. It is really cool and next time you need to make something with graphics, consider using Skia and its wrapper for your language. Next, generated images are upload to Imgur via [its API](https://apidocs.imgur.com) (the documentation could be better). The whole thing is running on a [free VM](https://www.oracle.com/cloud/free) in Oracle Cloud. So, yeah, next time you need to host something lightweight — check out their offering. Oracle also provides a free DB instance, which I'm using to cache the links. Monitoring: [Grafana Cloud](https://grafana.com/products/cloud) (also free). Deployments: [GitHub Actions](https://github.com/features/actions) + [Ansible](https://www.ansible.com/). So it didn't cost me a penny, except for ~50 hours of coding in two weeks on the evenings.

Feel free to ask any questions on the tech stack, I'll be glad to help you with you Telegram bots.