Telegram Bot - how to get a group chat id?

后端 未结 12 1071
北海茫月
北海茫月 2020-11-30 16:42

I\'ve been using telegram_bot, and trying to get groupChat id to send notifications to group chat, but don\'t know which methods I have to use for it.

For getting ch

相关标签:
12条回答
  • 2020-11-30 17:23

    After mid-2018:
    1:) Invite @getidsbot or @RawDataBot to your group and get your group id from the chat id field.

    Message
     ├ message_id: 338
     ├ from
     ┊  ├ id: *****
     ┊  ├ is_bot: false
     ┊  ├ first_name: 사이드
     ┊  ├ username: ******
     ┊  └ language_code: en
     ├ chat
     ┊  ├ id: -1001118554477    // This is Your Group id
     ┊  ├ title: Test Group
     ┊  └ type: supergroup
     ├ date: 1544948900
     └ text: A
    

    2:) use an unofficial Messenger like Plus Messenger and see your group id in group/channel info.

    Before mid-2018: (don't Use)
    1: Goto (https://web.telegram.org)
    2: Goto your Gorup and Find your link of Gorup(https://web.telegram.org/#/im?p=g154513121)
    3: Copy That number after g and put a (-) Before That -154513121
    4: Send Your Message to Gorup bot.sendMessage(-154513121, "Hi")
    I Tested Now and Work like a Charm

    0 讨论(0)
  • 2020-11-30 17:25

    You can get Chat ID in this way.

    On private chat with your bot, send a random message. You will search this message later.

    Get Your API-token from bot_father : XXXXXXXXX:YYYYYYY-YYYYYYYYYYYYYYYYY_YY

    Then, on your browser make a request with that url :

    https://api.telegram.org/botXXXXXXXXX:YYYYYYY-YYYYYYYYYYYYYYYYY_YY/getUpdates
    

    The request returns a json response, in json text search your random message and get chat id in that object.

    0 讨论(0)
  • 2020-11-30 17:25

    create a bot, or if already created set as follows:

    has access to messages

    apparently, regardless of how old/new the Telegram group is:

    1. add a bot to the group
    2. remove bot from the group
    3. add bot again to the group

    4. create a script file and run getUpdates method example:

        var vApiTokenTelegram = "1234567890:???>yg5GeL5PuItAOEhvdcPPELAOCCy3jBo"; // @?????Bot API token
        var vUrlTelegram = "https://api.telegram.org/bot" + vApiTokenTelegram;
    
        function getUpdates() {
            var response = UrlFetchApp.fetch(vUrlTelegram + "/getUpdates");
            console.log(response.getContentText());
        }
    
    1. function shall log to the console the following:
    [20-04-21 00:46:11:130 PDT] {"ok":true,"result":[{"update_id":81329501,
    "message":{"message_id":975,"from":{"id":962548471,"is_bot":false,"first_name":"Trajano","last_name":"Roberto","username":"TrajanoRoberto","language_code":"en"},"chat":{"id":-1001202656383,"title":"R\u00e1dioRN - A voz da na\u00e7\u00e3o!","type":"supergroup"},"date":1587454914,"left_chat_participant":{"id":1215098445,"is_bot":true,"first_name":"MediaFlamengoRawBot","username":"MediaFlamengoRawBot"},"left_chat_member":{"id":1215098445,"is_bot":true,"first_name":"MediaFlamengoRawBot","username":"MediaFlamengoRawBot"}}},{"update_id":81329502,
    "message":{"message_id":976,"from":{"id":962548471,"is_bot":false,"first_name":"Trajano","last_name":"Roberto","username":"TrajanoRoberto","language_code":"en"},"chat":{"id":-1001202656383,"title":"R\u00e1dioRN - A voz da na\u00e7\u00e3o!","type":"supergroup"},"date":1587454932,"new_chat_participant":{"id":1215098445,"is_bot":true,"first_name":"MediaFlamengoRawBot","username":"MediaFlamengoRawBot"},"new_chat_member":{"id":1215098445,"is_bot":true,"first_name":"MediaFlamengoRawBot","username":"MediaFlamengoRawBot"},"new_chat_members":[{"id":1215098445,"is_bot":true,"first_name":"MediaFlamengoRawBot","username":"MediaFlamengoRawBot"}]}}]}
    
    1. Telegram group chat_id can be extracted from above message "chat":{"id":-1001202656383,"title"

    Hope This Helps (HTH)

    0 讨论(0)
  • 2020-11-30 17:30

    IMHO the best way to do this is using TeleThon, but given that the answer by apadana is outdated beyond repair, I will write the working solution here:

    import os
    import sys
    from telethon import TelegramClient
    from telethon.utils import get_display_name
    
    import nest_asyncio
    nest_asyncio.apply()
    
    session_name = "<session_name>"
    api_id = <api_id>
    api_hash = "<api_hash>"
    dialog_count = 10 # you may change this
    
    if f"{session_name}.session" in os.listdir():
        os.remove(f"{session_name}.session")
    
    client = TelegramClient(session_name, api_id, api_hash)
    
    async def main():
        dialogs = await client.get_dialogs(dialog_count)
        for dialog in dialogs:
            print(get_display_name(dialog.entity), dialog.entity.id)
    
    async with client:
        client.loop.run_until_complete(main())
    

    this snippet will give you the first 10 chats in your Telegram.

    Assumptions:

    • you have telethon and nest_asyncio installed
    • you have api_id and api_hash from my.telegram.org
    0 讨论(0)
  • 2020-11-30 17:34

    In order to get the group chat id, do as follows:

    1. Add the Telegram BOT to the group.

    2. Get the list of updates for your BOT:

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

      Ex:

      https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates
      
    3. Look for the "chat" object:

    {"update_id":8393,"message":{"message_id":3,"from":{"id":7474,"first_name":"AAA"},"chat":{"id":,"title":""},"date":25497,"new_chat_participant":{"id":71,"first_name":"NAME","username":"YOUR_BOT_NAME"}}}

    This is a sample of the response when you add your BOT into a group.

    1. Use the "id" of the "chat" object to send your messages.
    0 讨论(0)
  • 2020-11-30 17:34
    function doPost(e) {
        var contents = JSON.parse(e.postData.contents);
        //  GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Telegram Bot Update",     JSON.stringify(contents, null, 4));
        var chat_id = contents.message.chat.id;
        var text = contents.message.text;
        var name = contents.message.from.first_name + " " +  contents.message.from.last_name;
        var sResponse = telegramBotMachine( chat_id, text, name );
    }
    
    0 讨论(0)
提交回复
热议问题