Telegram bots and Elixir

Well hello!!

I’ve been looking forward for a while now to write a little Telegram Bot guide in Elixir with TELEX (awesome bot api made by Rock Neurotiko, check his GitHub rather than his Medium profile… ehem, ehem…).

Things about this post:

  • I’ll use the Elixir syntax to talk about functions, this means a function called foo that receives one argument will be written as: foo/1
  • The code written in this guide would be uploaded in the nice_bot repo

Lets start by creating our own Elixir project by running mix new nice_bot

Now lets configure our Elixir project!


Previous configurations

First of all, dependencies. Add to mix.exs this two things:

Here is the mix.exs file:

Remember to add a comma before mod: {NiceBot, []}

Now for the config, we’ll need to identify our bot with a token, you can obtain a bot token from BotFather, using this guide.

Once we have our amazing token we’ll open config/config.exs and write at the end:

config :nice_bot,
token: "591018465:AAEhWJq4ESZbAqLBWzhv98ikUCb5ik2Lk34"

Once again here is our config/config.exs (I like Carbon :D)

Fake token BTW

Programming part ❤

We’re going to open lib/nice_bot.ex and erase everything inside except the definition of the module.

This module is going to be our Application, so we need to see which childs does the application need and the options the Supervisor is going to use, this would be the skeleton:

First of all we need to import Supervisor.Spec to use supervisor/2 function. After that we’ll specify two children:

  • Telex (in order for all this to work)
  • NiceBot.Bot (we’ll see it later)

We are going to set fancy logs so we know if the bot is up and running. The result would be this file:

As you can see we take the token from the config/config.exs file with Telex.Config.get/2


Now lets continue with the bot handlers, this is basically telling our bot to which commands should it respond, in order to do this we’ll need to create lib/nice_bot/bot.ex and define a module called NiceBot.Bot (which is the misterious children we wrote before). Here is our first handler for the command /start:

Telex needs to know the name of your bot, that’s why we need the bot/0 function and we need Telex.Bot to handle messages and Telex.Dsl to answer messages.

So! Sexy handle/3 and answer/1 right there, pretty ezpz!! How are we going to use them?

handle/3 receives a tuple {:command, “start”, msg}, this means the bot will handle /start commands and give us the message (msg) that triggered the handler, this msg struct contains a lot of useful information that can be found here.

What about the _name and _extra, well _name is just the name of the bot (spoiler alert Telex lets you have multiple bots in the same application) and maybe in another tutorial will check out what you can do with _extra…


So this is it! Lets run it and fix all the compilation errors! :D

YAY!

We need to fetch our dependencies with mix deps.get DON’T YOU THINK @Ironjanowar?!?!?

SUCCESS!

This is the one, LETS RUN IT WITH mix run — no-halt!!

After a long compilation log the result should be this:

Lets check if this is real in Telegram

YEEEES!

This is it! I’ll appreciate any correction of my English and I hope you have a great time making bots ❤

Feel free to open issues in the repo with questions, I’ll do my best to answers them :D

Examples of bots: