Jump to content
Welcome, welcome! Come in and register, and have some developer coffee. 👨‍💻 ×

[Node.js] Create a Telegram Bot With Telegram API (FREE & EASY)


Recommended Posts

Drum rooollls... da dduduuuuruuummrmumrumrumrumum...

 

Let's create an easy yet useful tool for your automation journey 🤭 🤭 🤭 🤭 Add it to your portfolio!!!!!!! 🔥🔥🔥🔥

 

Today we'll be learning how to send message to Telegram Groups/Channels through their free API. Before you begin, you need to be somewhat familiar (actually just the surface) with Promise, Async Await and HTTP methods in JavaScript. Do a quick read HERE and HERE

 

This step is optional, but I'm going to use Axios to make all my HTTP calls. Make sure you run the command below in your terminal before you proceed. You're welcome to other equivalent methods like the JavaScript built in function, fetch().

npm i axios

 

First, create a file for Telegram functions. I'm goin to name it Telegram.js. We're interested in the sendMessage endpoint to send messages. Note that we're using the GET method to send messages.

 

const axios = require('axios') // Import axios library
const token = '';
const chat_id = ''

const sendMessage = async (message) => {
	await axios.get(`https://api.telegram.com/bot${token}/sendMessage?chat_id=${chat_id}&text=${message}`)	
}

// We need to export the methods we declared in this file for external usages
module.exports = {
	sendMessage
}

 

If you've noticed, our token and chat_id fields are empty. We need to retrieve them in Telegram. Go to Telegram and search for the following account @BotFather. Click on start and you should see the following:

image.png

 

Select /newbot and follow the instructions. You should see this screen upon completion

image.png

 

Replace the token above in your code. Next we need to retrieve the chat_id. Go to the group/channel you wish to automate sending messages and invite your bot into the group/channel. To retrieve the chat_id, go to the following URL in your browser. Note that you must have at least one message in your group, and your bot is a member of the group/channel.

https://api.telegram.org/bot<<REPLACE_TOKEN>>/getUpdates

 

For example:

https://api.telegram.org/bot1234567890:ABCDEFGHIJKLMNOOOOOOOOO_KALSJDLKJL/getUpdates

 

Paste the response inside your dev tool or any JSON formatter you should see something similar. The chat_id is the one inside chat.id. Copy it and replace in our code

image.png

 

We're now done with the sendMessage function! Next, create another file called index.js and call the sendMessage function:

 

const { sendMessage } = require('./Telegram.js');

//Self calling anoynmous function
(async() => {
	await sendMesage('Hello from bot!');
})();

 

You should see your bot message appears in your group/channel. Congratz, you can now spam memes to your audience automatically.

 

Manage to follow? Let me know in the comment? Didn't manage to follow? Comment below for help!

  • Love 1
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...