Hello! Welcome to EmadulAi. In this article, we’ll learn how to get detailed information about a Telegram Group or Channel — such as title, description, and member count — using the Telegram Bot API. This tutorial will help you understand how bots can access public chat details easily with a few simple API calls.
What is Telegram?
Telegram is a popular cross-platform messaging application developed by Telegram Messenger LLP, a privately held company registered in London, United Kingdom. It is known for its advanced privacy protection, end-to-end encryption, and the ability to create and manage large group chats.
Telegram is available on multiple platforms including Android, iOS, Windows, macOS, and Linux. You can also use Telegram directly from your web browser without installing the app.
The platform was founded by Pavel Durov, a Russian entrepreneur and founder of VKontakte (VK). Telegram is completely free to use and offers cloud-based instant messaging with secure communication features.
How to Get Telegram Channel or Group Info?
Before we start, there are a few basic requirements you need to fulfill in order to send an HTTP request to the Telegram Bot API and get the channel or group information as a response. These requirements ensure that your bot can successfully communicate with the Telegram servers and fetch real-time data.
Requirements
Before we can get Telegram Channel or Group information using the Telegram Bot API, we need to meet a few basic requirements as listed below:
- Create a bot using the Telegram app and get the bot token.
- For tracking Channels or Groups, you will need the
@channel_nameor@group_name. However, you can also use chat_id (for example:-1001615491316), which is required for private groups or channels. - Ensure that the bot is not banned in the Group or Channel. Add the bot manually if the group or channel is private.
- To get the information of a Telegram Group or Channel, we need to send an HTTP request to the Telegram API.
https://api.telegram.org/bot<token-here>/<api-method-here>
If you want to learn more about other API methods, you can visit the official Telegram Bot API Documentation.
getChat Method
The getChat method allows us to fetch up-to-date information about a specific chat, such as:
the current name of the user, username of a group or channel, and other metadata.
https://api.telegram.org/bot<token-here>/getChat?chat_id=<id-number>
When executed successfully, the API returns a Chat object that contains all details of the specified chat or channel.
Example Response:
{
"id": -1001615491316,
"title": "EmadulAi - Support",
"username": "EmadulAi",
"type": "supergroup",
"description": "The official Group of EmadulAi.\n\nChannel: @emadulai\nWebsite: www.EmadulAi.in",
"permissions": {
"can_send_messages": true,
"can_send_media_messages": true,
"can_send_polls": false,
"can_send_other_messages": true,
"can_add_web_page_previews": false,
"can_change_info": false,
"can_invite_users": true,
"can_pin_messages": false
},
"linked_chat_id": -1001737659421,
"photo": {
"small_file_id": "AQADBQADO7AxG_8oWVQACAIAAwx_EMsW____v1GqOZ_Ct30jBA",
"small_file_unique_id": "AQADO7AxG_8oWVQAAQ",
"big_file_id": "AQADBQADO7AxG_8oWVQACAMAAwx_EMsW____v1GqOZ_Ct30jBA",
"big_file_unique_id": "AQADO7AxG_8oWVQB"
}
}
Thus, we successfully obtained all the essential information about the Telegram Group or Channel using the
getChat method.
Chat object does not include the total number of group members or channel subscribers.
To get the total count, we need to use another Telegram API method.
getChatMembersCount Method
To track the number of members in a Telegram Group or Channel, we can send another HTTP request using the
getChatMembersCount method.
Example:
https://api.telegram.org/bot<token-here>/getChatMembersCount?chat_id=<id-number>
We shall get a JSON response like this:
{
"ok": true,
"result": 157
}
The total number of followers or members is shown in the result key of the JSON response.
We just need to parse it out to display or use it in our project.
Full Example
Here is a full example through which you can learn how to get the Group or Channel information using Telegram Bot API.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=Edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>Telegram Group or Channel Information</title>
<link rel='stylesheet' href='./style.css'>
</head>
<body>
<div id='telegram-info'></div>
<script src='./script.js'></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,500;0,700;1,600&display=swap');
body {
padding: 15px;
background: #E8EAF6;
font-family: 'Poppins', sans-serif;
}
.container {
background: #fff;
padding: 5px;
border-radius: 10px;
box-shadow: 0 5px 35px rgba(0, 0, 0, .1);
font-size: 14px;
font-weight: 400;
}
.box {
display: flex;
flex-wrap: wrap;
background: #E3F2FD;
margin: 10px;
border-radius: 10px;
overflow: hidden;
}
.box >* {
position: relative;
padding: 15px;
background: #fff;
width: 100%;
}
.box >*:first-child {
background: #42A5F5;
color: #fff;
border-radius: 10px;
font-weight: 500;
}
.box >*:last-child {
background: #E3F2FD;
}
/**
* To Get Group or Channel Info
* Using Telegram API
* By EmadulAi
**/
/* Telegram Bot Api Token (sensitive), don't share publicly */
var token = '****************';
/* chat_id for the group or channel you want to track, i.e. @group, @channel or -1001615491316 */
var chat_id = '@emadulai';
/* 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);
}
/* Sending a request to Telegram API to get chat object of a particular channel or group */
function getChatDetails() {
return httpRequest(`https://api.telegram.org/bot${token}/getChat?chat_id=${chat_id}`, 'GET');
}
/* Parsing Chat Object from result key */
var chatObj = getChatDetails()['result'];
console.log(chatObj);
/* Sending 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');
}
/* Parsing Members Number from result key */
var members = getMemberCount()['result'];
console.log(members);
/* Writing in HTML */
document.getElementById('telegram-info').innerHTML = `<div class='container'>
<div class='box'>
<span>Title:</span>
<span>${chatObj.title}</span>
</div>
<div class='box'>
<span>Type:</span>
<span>${chatObj.type}</span>
</div>
<div class='box'>
<span>Username:</span>
<span>@${chatObj.username}</span>
</div>
<div class='box'>
<span>Chat ID:</span>
<span>${chatObj.id}</span>
</div>
<div class='box'>
<span>Description:</span>
<span>${chatObj.description ? chatObj.description.replaceAll('\n', '<br>') : "No description"}</span>
</div>
<div class='box'>
<span>Followers:</span>
<span>${members}</span>
</div>
</div>`;