<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ironjanowar on Medium]]></title>
        <description><![CDATA[Stories by Ironjanowar on Medium]]></description>
        <link>https://medium.com/@ironjanowar?source=rss-741f0cac2463------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WnIOD_y2C7-cEjvhcjRnJw.jpeg</url>
            <title>Stories by Ironjanowar on Medium</title>
            <link>https://medium.com/@ironjanowar?source=rss-741f0cac2463------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 10:54:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ironjanowar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Telegram bots and Elixir]]></title>
            <link>https://medium.com/@ironjanowar/telegram-bots-and-elixir-705909102496?source=rss-741f0cac2463------2</link>
            <guid isPermaLink="false">https://medium.com/p/705909102496</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[telegram]]></category>
            <category><![CDATA[telegram-bot]]></category>
            <dc:creator><![CDATA[Ironjanowar]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 17:15:12 GMT</pubDate>
            <atom:updated>2018-03-30T00:25:06.869Z</atom:updated>
            <content:encoded><![CDATA[<p>Well hello!!</p><p>I’ve been looking forward for a while now to write a little <a href="https://telegram.org/blog/bot-revolution">Telegram Bot</a> guide in <a href="https://elixir-lang.org/">Elixir</a> with <a href="https://github.com/rockneurotiko/telex">TELEX</a> (awesome bot api made by <a href="https://medium.com/u/3705fb1ff93f">Rock Neurotiko</a>, check his <a href="https://github.com/rockneurotiko">GitHub</a> rather than his Medium profile… ehem, ehem…).</p><p>Things about this post:</p><ul><li>I’ll use the Elixir syntax to talk about functions, this means a function called <strong>foo</strong> that receives <strong>one</strong> argument will be written as: <strong>foo/1</strong></li><li>The code written in this guide would be uploaded in the <a href="https://github.com/Ironjanowar/nice_bot">nice_bot </a>repo</li></ul><p>Lets start by creating our own Elixir project by running <em>mix new nice_bot</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*3qTnRrwzvPxq5i-7T1LYhA.png" /></figure><p>Now lets configure our Elixir project!</p><h3>Previous configurations</h3><p>First of all, <strong>dependencies</strong>. Add to <em>mix.exs</em> this two things:</p><ul><li>In the <strong>deps/0</strong> function <strong><em>{:telex, git: “</em></strong><a href="https://github.com/rockneurotiko/telex.git"><strong><em>https://github.com/rockneurotiko/telex.git</em></strong></a><strong><em>&quot;, tag: “0.4.0”}</em></strong></li><li>In the <strong>application/0</strong> function add to the list <strong><em>mod: {NiceBot, []}</em></strong></li></ul><p>Here is the <em>mix.exs</em> file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MDNx1Me01egqx86Zk6QF6w.png" /><figcaption><em>Remember to add a comma before </em><strong><em>mod: {NiceBot, []}</em></strong></figcaption></figure><p>Now for the config, we’ll need to identify our bot with a <strong>token</strong>, you can obtain a bot token from <a href="https://t.me/botfather"><strong>BotFather</strong></a>, using <a href="https://core.telegram.org/bots#6-botfather"><strong>this</strong></a> guide.</p><p>Once we have our amazing token we’ll open <strong>config/config.exs</strong> and write at the end:</p><pre>config :nice_bot,<br>  token: &quot;591018465:AAEhWJq4ESZbAqLBWzhv98ikUCb5ik2Lk34&quot;</pre><p>Once again here is our <em>config/config.exs </em>(I like <a href="https://carbon.now.sh/">Carbon</a> :D)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rSWlp3mWtg3uevsX8nE4HQ.png" /><figcaption>Fake token BTW</figcaption></figure><h3>Programming part ❤</h3><p>We’re going to open <strong>lib/nice_bot.ex</strong> and erase everything inside except the definition of the module.</p><p>This module is going to be our <strong>Application</strong>, 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:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hVmZsjGm9j8YeNu3AqCt4A.png" /></figure><p>First of all we need to import <strong>Supervisor.Spec</strong> to use <strong>supervisor/2</strong> function. After that we’ll specify two children:</p><ul><li>Telex (in order for all this to work)</li><li>NiceBot.Bot (we’ll see it later)</li></ul><p>We are going to set fancy logs so we know if the bot is up and running. The result would be this file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dJkerNbkcQXUTmrHD4QBVg.png" /></figure><p>As you can see we take the <strong>token</strong> from the <em>config/config.exs</em> file with <strong>Telex.Config.get/2</strong></p><p>Now lets continue with the bot <strong>handlers</strong>, this is basically telling our bot to which commands should it respond, in order to do this we’ll need to create <em>lib/nice_bot/bot.ex</em> and define a module called <strong>NiceBot.Bot</strong> (which is the misterious children we wrote before). Here is our first handler for the command /start:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jvVNx54Pu70Y1SYHzrPkrg.png" /></figure><p>Telex needs to know the name of your bot, that’s why we need the <strong>bot/0</strong> function and we need <strong>Telex.Bot</strong> to handle messages and <strong>Telex.Dsl</strong> to answer messages.</p><p>So! Sexy <strong>handle/3</strong> and <strong>answer/1</strong> right there, pretty ezpz!! How are we going to use them?</p><p><strong>handle/3</strong> receives a tuple <strong>{:command, “start”, msg}</strong>, this means the bot will handle <em>/start</em> commands and give us the message (msg) that triggered the handler, this msg struct contains a lot of useful information that can be found <a href="https://core.telegram.org/bots/api#message">here</a>.</p><p>What about the <em>_name</em> and <em>_extra</em>, well <em>_name</em> is just the name of the bot (<strong>spoiler alert</strong> Telex lets you have multiple bots in the same application) and maybe in another tutorial will check out what you can do with <em>_extra…</em></p><p>So this is it! Lets run it and fix all the compilation errors! :D</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*VQtpOoyfuMvWXJaZ4kUwpg.png" /><figcaption>YAY!</figcaption></figure><p>We need to fetch our dependencies with <em>mix deps.get</em> DON’T YOU THINK @Ironjanowar?!?!?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*adKHhh3VR9RBiVkcqd8wJw.png" /><figcaption>SUCCESS!</figcaption></figure><p>This is the one, LETS RUN IT WITH <em>mix run — no-halt</em>!!</p><p>After a long compilation log the result should be this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*IWO-9sBQhUDs-F9SvAKKMw.png" /></figure><p>Lets check if this is real in Telegram</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9kzowDKUVN4UomTCSgAfTQ.png" /><figcaption>YEEEES!</figcaption></figure><p>This is it! I’ll appreciate any correction of my English and I hope you have a great time making bots ❤</p><p>Feel free to open issues in the repo with questions, I’ll do my best to answers them :D</p><p>Examples of bots:</p><ul><li><a href="https://github.com/ironjanowar/daily_bot">https://github.com/ironjanowar/daily_bot</a></li><li><a href="https://github.com/Ironjanowar/openlibra_spam_bot">https://github.com/Ironjanowar/openlibra_spam_bot</a></li><li><a href="https://github.com/Ironjanowar/refraner_bot">https://github.com/Ironjanowar/refraner_bot</a> (currently in development)</li><li><a href="https://github.com/samgh96/spotiuribot">https://github.com/samgh96/spotiuribot</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=705909102496" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>