Writing an emoji Twitter bot 🤖

Writing an emoji Twitter bot 🤖

by Monica Dinculescu, Emojineer

You may not know this about me (why would you?), but I'm reaaaaaally into emoji. I like their origin story; I like that I can send cute images to people; I even like how you have to understand the dark corners of Unicode to figure out why emoji sometimes look funky in browsers. My internal work title is literally "Emojineer." I also end up building silly things in an evening, just for the fake internet points... like a Twitter bot.

Meet @to_emoji, a Twitter bot that translates English to emoji. This post walks through the steps of how I built @to_emoji.

If you tweet at @to_emoji in English, the bot replies with your tweet translated into emoji.

If you tweet at @to_emoji in English, the bot replies with your tweet translated into emoji.

Step 0: Keywords → emoji

Making things with emoji started two years ago, with my friend Mu-An. Back then, the macOS emoji picker didn't have a keyword search. To find the right emoji, you had to know where it was. Even when macOS finally added a search feature, it would only look for the exact emoji name—searching for "coffee" would give you ☕️, but searching for "cup" wouldn't.

Mu-An built emojilib, which is an emoji keyword library. It's basically an enormous json file, where every emoji corresponds to a bunch of keywords—for example, ☕️ is ["beverage", "caffeine", "latte", "espresso"]. The best thing about emojilib is that it's open source, so if you feel there's a keyword missing from the library, you can just send a PR and add one! Anyway, I've been using it to build A LOT of silly things.

If you don't know about the macOS emoji picker, it's going to blow your mind. Press ^⌘Space. Do it now.

If you're using Windows 10, you can get the Windows emoji keyboard by pressing the Windows Key + semicolon.

Step 1: English → keywords → emoji

I built emoji-translate to turn English into emoji. It's a single page app, where you enter English in a textarea and get back that text translated to emoji. Originally it wasn't very smart: it went through every word in your sentence, used emojilib to look up emojis with matching keywords, and randomly selected one of the emojis. Over the years, it got smarter. For example, I added a dropdown so you could choose from among multiple matching emoji translations. I also added some English-specific grammar rules—for example, it identifies plural words ("cups") and searches for the singular version of the word instead ("cup"). I won't lie to you, it's not very glamorous code.

I recently went on an npm module binge, so I finally converted it to an npm module. This means that you can just add it to your node app and translate words to emoji to your heart's content.

emoji-translate is a npm module that turns English into emoji. It's also accessible on the web as a single page website.

emoji-translate is a npm module that turns English into emoji. It's also accessible on the web as a single page website.

Step 2: Set up a Twitter bot

Building a Twitter bot is super easy because there's a lot of existing code you can use. My strategy for getting started was to a) use the Twit npm module and b) fork my friend Rachel's simple Twitter bot. If you don't have a Rachel in your life, you can look at and fork my bot!

The hard part of writing a Twitter bot is keeping it alive. You have these requirements:

  • your bot can't go to sleep (or else it stops talking to people)
  • your bot process needs to be easily killed (or else you can have multiple copies, all replying to the same people, in an alarming emoji conspiration)
  • your bot can't talk too quickly or too much (because Twitter has a very aggressive rate limit and will mute your bot)

Fear not, I broke all of those rules (badly), thus reinforcing my existing belief that I'd make a really shit ops person. At one point, I had 4 concurrent, unkillable zombie bots, all awake and bright eyed. I even got my bot banned for a weekend because I accidentally got it talking to itself in a loop, so it got marked as spam in a hot minute. I learned nothing from that, apparently, because the next week someone got a different bot talking to it in a loop, and we had to make our bots block each other to fix that. Woof. I don't really have any good advice for it other than "never be mean to an ops person again because their work is super hard and it's likely you'll suck at it."

Step 3: Hosting

I ended up trying a bunch of hosting providers, and learned some things. Mostly that there's actually no good solution for the "I don't need any CPU or space, I just need a process that stays alive" problem. There's a bunch of awesome and free providers (like Google Cloud) that don't work for bots, because they assume your process is woken up by something like an HTTP request. At least the way I wrote my bot, it doesn't. Anyway, here are my top picks:

  • Heroku is pretty easy to use, because deploying is just pushing to a remote git branch. Their free plan is kind of ok if you don't mind your bot sleeping every hour. If you do, you need to shell out $7 a month
  • now.sh is really awesome and free, but is only really useful if your process is stable. They host processes on a Kubernetes container, and once you "stop" your deployment, they don't actually kill the process -- they just block accessing it via HTTP requests. This works great for something like your personal site, but poorly for a twitter bot that just listens to stream events from Twitter. This way lie zombie bots.
  • Digital Ocean is a little cheaper than Heroku, but makes you install Node on your droplet instance and knowing myself, I saw this was going to go poorly so I bailed.

Step 4: Profit

Unleashing a silly emoji bot on Twitter was totally worth it. I saw it write poetry. I saw someone trying to see up its skirt. I saw everyone be silly and use emoji. A++, would bot again.

Becoming a data scientist

5 gifs to explain machine learning

5 gifs to explain machine learning