skip to Main Content

I am trying to find the API endpoint for telegram… a public one that does not require any login or API keys…

I found this: https://core.telegram.org/schema/json which includes a long JSON list of functions. Not sure if there are other API endpoints that can be used to query just a group and show stats for that group or how exactly to go about it.

I have seen other users suggest creating a telegram bot and then pull this data from the bot however not sure exactly how to do this effectively.

The main goal is to simply display the total users of a group via javascript and add the number via <span id="telegram-users">1</span>

I have seen this being done on coingecko’s coins and token listings under social however can not figure out how to simply show the total number of a telegram group in simple HTML format from JSON data for API..

UPDATE:

I did some research and freshened up on my coding skills.

There is this website: https://tgstat.com/ that pulls data for telegram channels. I am trying to use a simple javascript function to fetch this url and then use jsoup to get the data by using the element identifier which is .columns.large-2.medium-4.small-6.margin-bottom15 > div > .align-center > #text

Then use document.getElementById("telegram-members").innerHTML = ()

to display this data in html.

I understand the cosp and will use https://api.allorigins.win/raw?url= to bypass this.

2

Answers


  1. Chosen as BEST ANSWER

    This question has been moved here for easier understanding of the issue. How to pull data from another website using Javascript or jQuery (cross-domain) CORS (no api)


  2. 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

    • hello.html
    <!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>
    
    • hello.js
    // 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"));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search