A GIF generating chat bot on a $18 dedicated server
Our dumb GIF chat bot would have cost us 10x more on AWS.
There are a lot of GIF chat bots out there, but they only let you find a GIF and post it. We had the highly innovative idea to add text right into the GIF itself, thereby enhancing our ability to make super dumb jokes. Behold some of the creative possibilities:
We've been using Telegram lately and it has a great API, so that's where we decided to build the prototype. With all that in mind, here's a video of the finished product and we'll work backwards to see how it all works:
Here's what needs to happen immediately after a user starts typing:
Do a search on Giphy and start downloading the top results.
Make a transparent image file containing the text to overlay on top of the Giphy search results.
Composite those two pieces together.
Re-encode and return the result to the Telegram servers.
In the video you can see that Telegram returns around 20 inline results every time you pause while typing. That's great for seeing lots of options, but it's also a lot of encoding to do simultaneously. And if the bot isn't fast enough than it definitely won't be worth using.
To make serverside compositing and encoding fast enough usually requires a bunch of hardware or expensive video encoding services. However, there is one thing working in our favor - each gif in the result is completely independent, so it's very parallelizable. We just need a server with enough cores to take advantage of that fact.
We have been meaning to try out Scaleway dedicated servers for a while, but since they are located in Europe we were a bit concerned about latency. After running a few basic tests it seemed like the price per core advantage more than outweighed the latency downsides for this project. I mean, take a look at these dedicated server prices:
Sure, the per-core CPU performance isn't amazing. These are low cost, low power cores - but there are a lot of them! We were also a bit concerned about data transfer costs if this thing somehow took off, so the unlimited transfer was also an upside. In comparison, to get something similar on EC2 would cost between $140-$270 per month (t2.xlarge or t2.2xlarge). That doesn't include any bandwidth charges either.
How it Works
Telegram has a great Java API, so we did everything in Java. We used the following:
Giphy4j to do the Giphy searches
TelegramBots library to integrate with Telegram
Standard Java drawing and image APIs to draw the text out to a transparent PNG
FFmpeg to composite the PNG file with text onto the GIF and save as a new file
One important note is that Telegram supports a special kind of MP4 file that it will autoplay and loop in the same way as a gif. That means the quality can be much higher and the file sizes much lower. So we actually ended up using MP4 versions of the GIFs from Giphy and re-encoding back to MP4. We also ended up using FFmpeg to do the encoding because it was more reliable than the Java video encoding libraries we found. It's not ideal to be spawning processes and writing files during requests, but overall it seems pretty fast.
Nope! We got a solid burst of users for the first few days while the store listing was new, but then it died right down. We tried a bunch of different things to make the bot easier to use, but nothing seemed to help. We ended up around 10-20 daily users. Hockey stick growth, just not the curved part of the stick.
Giving Slack a Try
Since we had all the encoding stuff done we decided to put the bot in Slack too. The Slack bot UI isn't as good as Telegram, you can only show one GIF option at time, but since so many people we knew used Slack it still seemed worth it. It's pretty fun, but hasn't quite turned into something we use regularly yet. You can install the Slack bot and give it a try by going to gifmsgbot.larvalabs.com.
We love the GIF bot, but almost nobody else does.
The $18 server worked pretty well, it's nice to have that option when you need it and don't care as much about uptime and things like that.
Chat bots are pretty fun and easy to write, we'll probably do another one at some point.