Alright, if I understand your question well. You're trying to get the total number of users from a telegram group, channel, or chat.
The premise is, you want to avoid using Telegram API which will require auth token.
However, since your main goal is just basically the same. I'll leave this method here, so you'll have a "backup plan" later.
Requirement & Notes:
- Create a bot on Telegram app, and get the bot token.
- For tracking channels, you just need to find the
@channel name.
- However for group chats, you need to find the chat_id for it. As explained below!
- Only vanilla Javascript is needed
- A bit of patience
At most usage, you just need to send a HTTP request to the Telegram API.
API Example: https://api.telegram.org/bot<token-here>?<api-method-here>
You can learn more about other API method Here.
Hold Up!
Firstly, what do you want to track? Is it a group? Is it a channel?
Both have different method, please check out the example below!.
To track the number of members that are following a channel
This is fairly easy. Just send a HTTP request to:
https://api.telegram.org/bot<token-here>/getChatMembersCount?chat_id=<channel-name>
For example: the name of the official telegram channel is @telegram.
Sooo, send a request to:
https://api.telegram.org/bot<token-here>/getChatMembersCount?chat_id=@telegram
You'll get a response like:
{ ok: true, result: 5605541 }
The total number of followers is shown in the result key.
To track the number of members in a group chat
Before we can get the number of members in a group. You need to find the <chat_id> for your group chat.
Note: Your bot must join the group before you can do anything like tracking, send message etc.
To do that, send a HTTP request to https://api.telegram.org/bot<token-here>/getUpdates.
Look closely into the response. You need to find the chat object, like this.
<...>
0: Object { update_id: 122334455, my_chat_member: {…} }
my_chat_member: Object { chat: {…}, from: {…}, date: 1622275487, … }
chat: Object { id: -1234567890, title: "Group-Name", type: "group", … }
<...>
You just need the id from the chat object. Which is -1234567890 in this case.
After you got the chat_id number.
Just send a HTTP request to:
https://api.telegram.org/bot<token-here>/getChatMembersCount?chat_id=<id-number>
You'll also get the response as:
{ ok: true, result: 3 }
The number of members is stored in the result key. Just parse it out and boom!
Full Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<span id="telegram-users">null</span>
</head>
<body></body>
<script src="hello.js"></script>
</html>
// Set Your Bot Token Here
var Token = "insert_your_bot_token";
/*
chat_id for the group or channel you want to track,
For channels, they are usually like @telegram, @Bitcoin_News_Crypto, @TelegramTips,
For group chats, they are numbers alike. Just like "-1234567890",
Modify and set your chat_id here if you found it,
It should look like var group_chat_id = "-1234567890".
*/
var chat_id = "@telegram";
// HTTP Request Function
function httpRequest(URL, Method)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open(Method, URL, false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(null);
return JSON.parse(xmlHttp.responseText);
}
// Send a request to Telegram API to get number of members in a particular group or channel
function getMemberCount()
{
return httpRequest(`https://api.telegram.org/bot${Token}/getChatMembersCount?chat_id=${chat_id}`, "GET");
}
// Run function and parse only the number of members
var result = getMemberCount()["result"];
console.log(result);
// Write the numbers of member back to HTML
document.getElementById("telegram-users").innerText = `The group or channel currently has ${result} members.`;
// Use this to find the *chat_id* for your group chat
//console.log(httpRequest(`https://api.telegram.org/bot${Token}/getUpdates`, "GET"));