Build a Telegram Bot with Node.js

Build a Telegram Bot with Node.js

Table of Contents

  1. Introduction
  2. Setting up a Telegram Bot
  3. Creating Custom Commands
  4. Connecting the Bot for Local Development
  5. Implementing Custom Commands
  6. Conclusion

Introduction

Creating a Telegram bot can be a simple process, whether you choose to use the Telegram app or the website. In this Tutorial, we will guide you through the step-by-step process of setting up a Telegram bot using the Telegram web interface.

Setting up a Telegram Bot

To begin, open your Telegram app or visit the Telegram website. Search for "BotFather", which is the tool we will use to create our bot. Make sure to select the BotFather with the official verified symbol.

Once you have selected the BotFather, click on "Start". In the Telegram bot, any text that begins with a slash ("/") is considered a command. We will later explore how to create custom commands for our bot, but for now, let's focus on creating a new bot.

Click on "New Bot", and give your bot a name. For this tutorial, let's name our bot "Sync Google Photos". Next, you need to create a unique username for your bot. This username needs to end with the WORD "bot" and must be unique for your bot to identify.

After providing a username, BotFather will generate a token for your bot. It is crucial to save this token in a safe place, as it serves as the key to controlling your bot. Congratulations! You have successfully created your first bot.

Creating Custom Commands

To set custom commands for your bot, you can use the BotFather interface. Select "Set Command" and choose your bot. You can then send the commands you want to set in the format of "command name" followed by a brief description.

For example, you can set commands like "test" and "surprise" that perform specific functions. Once you have updated the commands, you can type "/" in your Telegram app to see the available commands. You can also access the commands through the menu interface.

Connecting the Bot for Local Development

To connect your Telegram bot to a local development server, you will need to set up a node.js project and use a tool called ngrok. Ngrok will allow your bot to communicate with your local node server by creating an HTTPS server that redirects the traffic from Telegram to your local server.

To install ngrok, sign up for an account and download the ngrok application that matches your operating system. Extract the downloaded file to your desired location. Afterward, connect your account to ngrok by using your authorization token.

Next, open your terminal and navigate to the directory where ngrok is located. Run the command to connect your account. If you encounter any issues, you can use the complete path to execute the command.

Once connected, specify the path and indicate the HTTP protocol along with the port number (usually 4040) where your local server is running. Ngrok will forward the HTTPS requests to your local server.

Test if the connection is successful by entering the ngrok URL in your browser. You should see that it connects to your local server and returns a Hello message.

Implementing Custom Commands

Now that we have set up our local node server and connected it to our Telegram bot, let's add some code to enable proper communication with the bot. We will create a controller and an axios library to handle all our Telegram requests and make API calls.

In the axios file, we import the axios library and our bot token. This will provide the base structure for all our Telegram API calls. We then create an axios instance and expose two methods: "get" to handle all the GET calls, and "post" to make any HTTP requests.

Next, in the telegram.js file, we create a function to send messages back to the bot. This function makes a GET call using axios and the "send message" method available in the Telegram API. The "send message" method requires two parameters: the chat ID and the text of the message.

To obtain the chat ID, we send a message to our Telegram bot and extract the chat ID from the response object. The chat ID distinguishes each chat room and allows us to send responses back to the correct chat.

We also create a "handle message" method that handles every message we receive from the Telegram bot. This method checks if the message starts with a slash ("/"). If it does, it removes the slash and records the rest of the STRING as a command. We can then handle this command and send a corresponding message using the "send message" method.

If the command is unknown, we simply send a generic message. These methods allow us to handle different commands and respond accordingly.

To implement custom commands, we update the code in the handle message method based on the received command. For example, if the command is "surprise", we can send a random photo from our Google Photos account. We will cover the implementation of these commands in the next tutorial.

Conclusion

In this tutorial, we covered the process of setting up a Telegram bot, creating custom commands, connecting the bot for local development, and implementing custom commands. We explored the use of ngrok to establish a communication link between our local server and the Telegram bot. By following these steps, you can create your own Telegram bot and customize its functionality.

Thank you for tuning in, and we'll see you in the next tutorial!

FAQ

Q: Can I create multiple bots using BotFather? A: Yes, you can create multiple bots using BotFather. Each bot will have its own unique token and can be customized as per your requirements.

Q: Can I edit the commands for my bot after creation? A: Yes, you can edit the commands for your bot using the BotFather interface. Simply select your bot, choose "Set Command", and update the commands accordingly.

Q: How do I handle unknown commands in my bot? A: In the handle message method, you can add an additional condition to handle unknown commands. If the command is not recognized, you can send a generic message to the user.

Q: Can I send images or videos through my Telegram bot? A: Yes, you can send images, videos, and various other types of media through your Telegram bot. You can utilize the API methods available to send media content to your users.

Q: Is it possible to deploy my Telegram bot on a server for public access? A: Yes, you can deploy your Telegram bot on a server to make it accessible to the public. You would need to host your bot's code on a server and ensure the necessary configurations are in place for it to function correctly.

Most people like

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content