Chatbots and the Future of Marketing: Building Your First Chatbot with Node.js and Telegram

Danielle Mizrachi
Voice Tech Podcast
Published in
4 min readOct 30, 2019

About a year ago I attended an event in Tel Aviv with Kipp Bodnar, CMO of HubSpot, on the future of engagement.

His message was that today’s consumers don’t have the time or patience to wait; they want to be able to interact with your company in real-time. If they don’t get a smart and quick response, they will move on to the next company that will provide this for them. Kipp then went into detail about how chatbots can help companies fill this demand, and that if marketers didn’t start utilizing chatbots, they’d fall behind.

What he said made sense; instead of a lead getting an intercom message that the sales team are only online from 9 to 5, a chatbot can immediately interpret and answer questions at all hours, so you don’t miss out on an opportunity!

Being both a marketer and a programmer, I wanted to share with you how to get started on building a basic chatbot. This could be a very good way to convince your team of the value of chatbots, and how it might be a worthwhile investment.

Installation

  1. To start, create a new file in your text editor, I use VS code.
  2. Download Node.js for your computer
  3. Install Telegram

Getting a bot token on Telegram

  1. On telegram, search for BotFather
  2. Send /newbot to BotFather and give your bot a name
  3. Next, copy your bot token. This is how your app will talk to the telegram servers

Using VS code

  1. Create your bot folder and Index.js file
  2. Set up your json package with npm init-y in your terminal
  3. Next, use telegraf, a bot framework for Node.js. Install this using npm install telegraf — save
  4. Create a .env file to store your bot token in (in your .env file, type BOT_TOKEN= <your_token>)
  5. At the top of your index.js file, create a bot using Telegraf and your token in the .env file

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

Getting your bot to respond to user input

Telegraf provides you with a nice way to respond to users based on input, first let’s try

Getting updates for your bot

When someone messages a bot on telegram, this information moves between three parties; the user, the telegram server, and the bot. Once a user sends a message to the bot, the telegram server sends a request to the bot using JSON with either getUpdates or setWebhook. The bot will then process the request and send it back to the server, which will send the message back to the user.

All the incoming updates are stored in the telegram server until the bot gets them, but it will only stay in their server for 24 hours.

Setting up a folder for your updates

Using telegraf’s .launch method allows you to launch your bot in long-polling or webhook mode.

For simplicity, we will use getUpdates. In order to create a folder for the incoming messages, in your terminal type curl https://api.telegram.org/bot<your_token>/getUpdates |json_pp >updates.json

This is what the file will look like:

To start your bot, type npm start in your terminal. Check out the middleware section on telegraf and tools like wit.ai to make your bot smarter! Test your bot’s response to certain inputs in telegram.

If you have any questions while building this or on connecting your chatbot to an API, feel free to reach out to me on LinkedIn.

Something just for you

--

--

Danielle Mizrachi
Voice Tech Podcast

Coffee addict, marketer turned software engineer. Can tell you at least 3 facts learned from podcasts on the fly.