<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: André Carlos</title>
    <description>The latest articles on DEV Community by André Carlos (@acjunior).</description>
    <link>https://dev.to/acjunior</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F174853%2Fadfe6c4a-dd00-4e55-8b4f-50ef094ff1da.jpg</url>
      <title>DEV Community: André Carlos</title>
      <link>https://dev.to/acjunior</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/acjunior"/>
    <language>en</language>
    <item>
      <title>Starting a Hello world with Telegram bot in Node.JS!</title>
      <dc:creator>André Carlos</dc:creator>
      <pubDate>Fri, 22 May 2020 04:00:08 +0000</pubDate>
      <link>https://dev.to/andrecrjr/starting-a-hello-world-with-telegram-bot-in-nodejs-3p7e</link>
      <guid>https://dev.to/andrecrjr/starting-a-hello-world-with-telegram-bot-in-nodejs-3p7e</guid>
      <description>&lt;p&gt;Using Telebot to create our first hello world Bot in Telegram!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--anNpuR7V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2A0lyfMqzUHPbZZe2u" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--anNpuR7V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2A0lyfMqzUHPbZZe2u" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@christianw?utm_source=medium&amp;amp;utm_medium=referral"&gt;Christian Wiediger&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello guys, this is my first article in Medium! I’m a web developer/front-end lover and i wanted to start here with something different than habitual: chat bots.&lt;/p&gt;

&lt;h3&gt;
  
  
  About Telegram
&lt;/h3&gt;

&lt;p&gt;Telegram is one of the most famous chat platforms bellow Whatsapp around the world and had been the one of the most famous open source project in the world, but its almost impossible to talk in Telegram without talk about its bots…&lt;/p&gt;

&lt;h3&gt;
  
  
  Whats is “Bots” ?
&lt;/h3&gt;

&lt;p&gt;Bots or Chat bots in a simply talking is about a chat that you have a interface with &lt;strong&gt;an “automatized” robot to achieve a certain goal&lt;/strong&gt; &lt;em&gt;(algorithm)&lt;/em&gt;; you will have keywords/commands to start interaction with the bot, what can be like: &lt;em&gt;“/giphy i want gif of something”&lt;/em&gt;, thats a kinda command that will be searching a gif inside Telegram for example.&lt;/p&gt;

&lt;p&gt;In Telegram, chat bots is amazing, you can create bots to &lt;a href="https://core.telegram.org/bots/payments"&gt;pay as a e-commerce&lt;/a&gt; inside them and send to the user that will be interacting with him what the platforms can send as photos, location, music etc, its API to create a chat bot is real amazing!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Warning: I will explain the basic with NodeJS to create the minimum BOT without deploy, will be nice to create a new post about it in the future if someone asks for it :) !&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Botfather
&lt;/h3&gt;

&lt;p&gt;First things first, we need to create our new Bot in Telegram, so you’ll need to go to Telegram Web/Desktop, and find the father of the Bots, BotFather!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mUZpqSS---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/764/1%2AjoQJ2X5M-yR8V5JdhGEJ-A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mUZpqSS---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/764/1%2AjoQJ2X5M-yR8V5JdhGEJ-A.png" alt=""&gt;&lt;/a&gt;Botfather the father of bots, the first bot.&lt;/p&gt;

&lt;p&gt;Just do the /start and they will “chat ” with you a lot of commands that you can do with him! And obviously let’s start a new bot: send &lt;strong&gt;/newbot&lt;/strong&gt; to him.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vB1_lKl2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/683/1%2A_1TXPooCNjcSK_yt36ol0Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vB1_lKl2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/683/1%2A_1TXPooCNjcSK_yt36ol0Q.png" alt=""&gt;&lt;/a&gt;/newbot in botfather, What will be the name of our bot!?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vziSjYij--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/695/1%2AHDGwkMAwsSvgbqyc7HMzJg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vziSjYij--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/695/1%2AHDGwkMAwsSvgbqyc7HMzJg.png" alt=""&gt;&lt;/a&gt;then you’ll be give the bot name url to him, that’ll need to have “Bot” in the end&lt;/p&gt;

&lt;p&gt;If your bot name is the &lt;strong&gt;unique&lt;/strong&gt; in all the &lt;strong&gt;around bot universe of Telegram…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lv3Cnkrs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/666/1%2AtzFWZszF3fEmWakWbmAUEw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lv3Cnkrs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/666/1%2AtzFWZszF3fEmWakWbmAUEw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats, you’ll get your API KEY and your link to your bot, in my case here “t.me/hellwrldbot” it’s the url, your can start your bot in telegram using it!&lt;/p&gt;

&lt;h3&gt;
  
  
  Coding with Node
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oIcWMNdS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/540/1%2AnpxtXyLjCSpM7Sma1eQj8g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oIcWMNdS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/540/1%2AnpxtXyLjCSpM7Sma1eQj8g.gif" alt=""&gt;&lt;/a&gt;Finally code!&lt;/p&gt;

&lt;p&gt;You will need to create a folder in your terminal/bash and a project using Yarn or NPM as if you started a normal project in nodejs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init -y helloworldbot OR npm init -y helloworld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Awesome you create a folder project, now we will need a guy that will help us with the telegram API and ladies and gentlemen, i present you &lt;a href="https://github.com/mullwar/telebot/"&gt;Telebot&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Telebot is a lib that will help us to deal with the “polling” to the bot (or webhook to most advanced uses) and we will give our commands of the bot to it.&lt;/p&gt;

&lt;p&gt;So we’ll add him to our package.json:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add telebot
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Amazing, now let’s code our index.js:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;It’s simple but there is a lot here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;line 1-&lt;/strong&gt; We get the Telebot from the package(orly)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;line 4-&lt;/strong&gt; We create a bot instance from Telebot with your API token got in the BotFather!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;line 9-&lt;/strong&gt; I use the “event listener” bot. &lt;strong&gt;on&lt;/strong&gt; to listen two commands in this case /hello and /start&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;line 12-&lt;/strong&gt; Inside this listener &lt;strong&gt;bot.on&lt;/strong&gt; we have a callback function&lt;/p&gt;

&lt;p&gt;This callback function will receive &lt;strong&gt;msg&lt;/strong&gt; , this is the &lt;strong&gt;JSON&lt;/strong&gt; which telegram will send to you, everything about the user will be sent in this  &lt;strong&gt;JSON.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;so &lt;strong&gt;bot.message(msg.from.id, “your message to user!”)&lt;/strong&gt; will be the reply as message!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;line 16-&lt;/strong&gt; We start the bot to polling and listen our bot in telegram&lt;/p&gt;

&lt;p&gt;Now you need to start the server to Bot works, you can modifying your package.json adding a script to NPM/Yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
"start": "node index.js",
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;above your dependencies in package.json and start it in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and now your bot in the url/link that Botfather gave you in telegram!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ruo3kbjj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/853/1%2A5rDwMUkNIO543Mrwszqm8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ruo3kbjj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/853/1%2A5rDwMUkNIO543Mrwszqm8g.png" alt=""&gt;&lt;/a&gt;talking to my bot! get your link from the botfather when he gave you the API!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mullwar/telebot"&gt;TeleBot&lt;/a&gt;is really good, there is a lot of ways to answer our users in telegram, as I said before, you can send photos, stickers, music, location, sell things using bot(!!!) and secure way, you can study their API!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There is another libs that do the same as telebot, you can see it &lt;a href="https://core.telegram.org/bots/samples"&gt;here&lt;/a&gt;, the most famous is &lt;a href="https://github.com/telegraf/telegraf"&gt;Telegraf&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://core.telegram.org/bots"&gt;See what you can do with telegram bots here&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;That’s all folks, for beginners, direct, simple, clean and easy!&lt;/p&gt;

&lt;p&gt;I hope you all enjoy it!&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>telegram</category>
      <category>chatbot</category>
    </item>
  </channel>
</rss>
