This post is about to be updated.
Hello! Welcome to Fineshop Design.
In this article, we will learn how to get Telegram Group or Channel information (for e.g. description, members count, etc) using Telegram API.
© Fineshop Design | Telegram API |
Before we start let's take a look at its Demo.
What is Telegram?
Telegram is a popular cross-platform messaging app developed by Telegram Messenger LLP, a privately held company registered in London, United Kingdom. It is widely used because it offers some enhanced privacy and encryption features as well as supports large group chat features.
The Telegram app is multiplatform, with versions of the app available for iOS, Android, Windows, Mac, and Linux. You can also access the Telegram from a web browser.
Telegram was founded by Russian social media entrepreneur Pavel Durov, and the service is free to use.
How to get Channel or Group Info?
Before we start, there are few requirements to send HTTP Request to Telegram API and get the response.
Requirements
- Create a bot on Telegram app, and get the bot token.
- For tracking Channels or Groups,
@channel
name or@group
name is required. However you can also use chat_id, i.e.-1001615491316
. chat_id is required in case of private group or channel. - Bot must not be banned in the Group or Channel. Add the bot if the group or channel is private.
To get the information of a Telegram Group or Channel, what we need to do is send a HTTP request to Telegram API.
API Example: https://api.telegram.org/bot<token-here>?<api-method-here>
If you want to lean more about other API methods, you can read Telegram Bot API
getChat Method
Using this method, we can get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
Example:https://api.telegram.org/bot<token-here>/getChat?chat_id=<id-number>
It returns a Chat object on success, as shown in example below:
{
id: -1001615491316,
title: "Fineshop Design - Support",
username: "Fineshop_Design",
type: "supergroup",
description: "The official Group of Fineshop Design.\n\nChannel: @fineshopdesign\nWebsite: www.fineshopdesign.com",
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 got all the needed information about the group or channel.
But the chat object does not contain the total no. of group members or channel subscribers.
getChatMembersCount Method
To track the members count, we need to send a HTTP request again using getChatMembersCount
Method.
Example:https://api.telegram.org/bot<token-here>/getChatMembersCount?chat_id=<id-number>
We shall get a response like:
{
ok: true,
result: 158
}
The total number of followers is shown in the result
key. We just have parse it out.
Full Example
Here is a full example through which you can learn how to get the Group or Channel information using Telegram API.
index.html
Here is the HTML Codes:
<!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>
style.css
Here is the CSS Codes:
@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;
}
script.js
Here is the Vanilla JavaScript Codes:
/**
* To Get Group or Channel Info
* Using Telegram API
* By Fineshop Design
**/
/* 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 = '@Fineshop_Design';
/* 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>`;
Conclusion
This is all about getting a Telegram Group or Channel Data using Telegram API. I hope you enjoy this article. Please do share this article. And if you are facing problem in any section or you have any question then ask us in . Thank you!
© Copyright:
www.fineshopdesign.com