我一直在使用telegram_bot,并试图获得groupChat id发送通知到组聊天,但不知道我必须使用哪些方法。
为了获得聊天id,当机器人参与聊天时,我使用message.chat.id,但我必须使用它来获得组聊天id,无法找到/
我一直在使用telegram_bot,并试图获得groupChat id发送通知到组聊天,但不知道我必须使用哪些方法。
为了获得聊天id,当机器人参与聊天时,我使用message.chat.id,但我必须使用它来获得组聊天id,无法找到/
当前回答
我对错误的第二个解决方案{"ok":true,"result":[]}
加入你的电报群 添加新用户(邀请) 搜索“getidsbot”=> @getidsbot 消息:/ start@getidsbot 现在您看到了ID。看起来像1068773197,对于bot来说是-1001068773197(前缀是-100)!! 把机器人踢出群组。 现在转到web浏览器,发送这一行(测试消息):
https://api.telegram.org/botAPITOKENNUMBER:APITOKENKEYHERE/sendmessage?chat_id=-100GROUPNUMBER&text=test
编辑API令牌和Group-ID!
其他回答
要获取群聊id,请执行以下步骤:
将Telegram BOT添加到群组中。 获取BOT的更新列表:
https://api.telegram.org/bot<YourBOTToken>/getUpdates
Ex:
https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates
查找"chat"对象:
{“update_id”:8393年,“消息”:{“message_id”:3,"从":{" id ": 7474年,“first_name”:“AAA”},“聊天”:{" id ": < group_ID >,“标题”:"},“日期”:25497年,“new_chat_participant”:{" id ": 71年,“first_name”:“名字”,“用户名”:“YOUR_BOT_NAME”}}}
这是将BOT添加到组中时的响应示例。
使用"chat"对象的"id"发送消息。 (如果你用bot创建了一个新的组,你只得到{"ok":true,"result":[]},删除并重新添加bot到组中) 私有图表只适用于image argoprojlabs/argocd-notifications:v1.1.0或更高版本。
依我看,最好的办法是使用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
群组聊天id应该以-(减号)开头
This is wrong
10540154212
This is correct
-10540154212
使用python和telethon,很容易获得聊天id。这个解决方案最适合那些使用电报API的人。
如果你没有电视马拉松,运行这个:
pip install telethon
如果你没有telegram的注册应用,注册一个: 链接如下:https://my.telegram.org/
然后运行以下代码:
from telethon import InteractiveTelegramClient
from telethon.utils.tl_utils import get_display_name
client = InteractiveTelegramClient('session_id', 'YOUR_PHONE_NUMBER', api_id=1234YOURAPI_ID, api_hash='YOUR_API_HASH')
dialog_count = 10
dialogs, entities = client.get_dialogs(dialog_count)
for i, entity in enumerate(entities):
i += 1 # 1-based index
print('{}. {}. id: {}'.format(i, get_display_name(entity), entity.id))
您可能希望向您的群组发送消息,以便该群组显示在列表顶部。
我不明白为什么最明显(可能也是最简单)的答案不在这里。
当你在写一个bot时,你可以用这三行简单的代码获取id:
bot.on('message', (msg) => {
console.log(msg)
})
然后从控制台检查groupId:D
当你可能试图查看groupId,以便能够用你的bot向一个组发送消息时,正确答案可能是这样的:
你不再使用groupId了。您使用组名(带有https://t.me/careless_whisper的组名)加上@前缀。记住,你的小组必须是公开的。来源:Telegram API
因此,如果你的组名是careless_whisper,收件人将是@careless_whisper