我一直在使用telegram_bot,并试图获得groupChat id发送通知到组聊天,但不知道我必须使用哪些方法。

为了获得聊天id,当机器人参与聊天时,我使用message.chat.id,但我必须使用它来获得组聊天id,无法找到/


当前回答

我发现的最简单的方法是只使用电报网:

开放网络。电报在浏览器(在我的情况下chrome) 右键单击左侧菜单上的组名 点击“检查”按钮 您将在属性中看到组id Data-peer-id ="-xxxxxxxxxx" or peer="-xxxxxxxxxx"

群聊id: -xxxxxxxxxx

频道聊天id: -100xxxxxxxxxx

(对于某些频道/组,您需要添加-100前缀)

编辑: 在某些情况下,当您单击组名时,ID会显示在浏览器的地址栏中

https://web.telegram.org/z/#-xxxxxxxxxx

其他回答

你可以通过这种方式获取聊天ID。

在与机器人的私人聊天中,随机发送一条消息。稍后您将搜索此消息。

从bot_father: XXXXXXXXX: yyyyyyyyyyyyyyyyyyyyy_yy获取api令牌

然后,在浏览器上使用该url发出请求:

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

请求返回一个json响应,在json文本中搜索你的随机消息,并在该对象中获得聊天id。

截至2021年5月,简单地说:

邀请@RawDataBot到你的组。

加入后,它将输出一个JSON文件,其中您的聊天id将位于message.chat.id。

"message": {
    "chat": {
        "id": -210987654,
        "title": ...,
        "type": "group",
        ...
    }
    ...
}

之后一定要把@RawDataBot从组中踢出去。

2018年代中期之后: 1)邀请@getidsbot或@RawDataBot到你的组,并从聊天id字段获取你的组id。

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:)使用非官方的聊天工具,比如Plus Messenger,在群组/频道信息中查看你的群组id。

2018年年中之前:(不要使用) 1: Goto (https://web.telegram.org) 2:去你的Gorup,找到你的Gorup链接(https://web.telegram.org/#/im?p=g154513121) 3:复制g后面的数字,在前面加一个(-)-154513121 4:发送你的信息给Gorup 机器人。“嗨”sendMessage (-154513121) 我现在就测试了,效果很好

function adminCheck( chat_id, name ) {
var bAdminCheck = false;
var contents = JSON.parse( getAdmin( chat_id ) );      
var i = 0;
while( !bAdminCheck && (i < contents.result.length ) ) {
    if( name == (contents.result[i].user.first_name + " " + contents.result[i].user.last_name) ) {
        bAdminCheck = true;  
    }
    i++;
}  
return bAdminCheck;

}

依我看,最好的办法是使用TeleThon,但鉴于apadana的答案已经过时,无法修复,我将在这里写下工作解决方案:

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())

这个片段将为您提供Telegram中的前10次聊天。

假设:

您已经安装了telethon和nest_asyncio 你有来自my.telegram.org的api_id和api_hash