来源:本站时间:2025-01-25
抓取電報群組成員並將其增加到你的群組中涉及到一些複雜的技術過程和恪守電報平台的方針。下面是一個概述:
安裝所需東西:
Python程式設計語言
Telethon函式庫(一個Telegram API函式庫)
創作Telegram運用:
拜訪Telegram's API開發者頁面並運用你的Telegram帳號登入。
建立一個新的運用程式以取得API ID和API Hash。
編寫抓取成員的Python腳本:
來自 telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
# 用你的API ID和API Hash替換
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
phone = 'YOUR_PHONE_NUMBER'
client = TelegramClient(phone, api_id, api_hash)
async def main():
await client.start()
chats = []
last_date = None
chunk_size = 200
groups = []
result = await client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash=0
))
chats.extend(result.chats)
for chat in chats:
try:
if chat.megagroup: # 如果是megagroup
groups.append(chat)
except:
continue
print('挑選一個群組以抓取成員:')
i = 0
for g in groups:
print(f'{i} - {g.title}')
i += 1
g_index = input("輸入群組編號: ")
target_group = groups[int(g_index)]
print('\n抓取成員...')
all_participants = []
all_participants = await client.get_participants(target_group, aggressive=True)
print('儲存成員...')
with open("members.csv", "w", encoding='UTF-8') as f:
f.write("username, user_id, access_hash, name, group, group_id\n")
for user in all_participants:
if user.username:
username = user.username
else:
username = ""
if user.first_name:
first_name = user.first_name
else:
first_name = ""
if user.last_name:
last_name = user.last_name
else:
last_name = ""
name = (first_name + ' ' + last_name).strip()
f.write(f"{username}, {user.id}, {user.access_hash}, {name}, {target_group.title}, {target_group.id}\n")
with client:
client.loop.run_until_complete(main())
注意:請確保你有增加成員的權限。
編寫增加成員的Python腳本:
來自 telethon.sync import TelegramClient
from telethon.errors import FloodWaitError
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.functions.channels import InviteToChannelRequest
# 用你的API ID和API Hash替換
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
phone = 'YOUR_PHONE_NUMBER'
client = TelegramClient(phone, api_id, api_hash)
async def add_users_to_group(client, users, target_group):
for user in users:
try:
print(f"Adding {user['username']}")
await client(InviteToChannelRequest(target_group, [user]))
time.sleep(2)
except FloodWaitError as e:
print(f'Flood wait for {e.seconds} seconds')
time.sleep(e.seconds)
async def main():
await client.start()
target_group = input("Enter the target group username or ID: ")
users = []
with open("members.csv", "r", encoding='UTF-8') as f:
rows = csv.DictReader(f, delimiter=",", quotechar='"')
for row in rows:
user = await client(ResolveUsernameRequest(row['username']))
users.append(user)
await add_users_to_group(client, users, target_group)
with client:
client.loop.run_until_complete(main())
合規性:在抓取和增加成員時,必須恪守Telegram的運用方針和隱私規矩。未經許可抓取和增加成員可能會導致你的帳戶被封鎖。
約束:Telegram對每個帳戶每天能增加到群組的成員數量有約束。請當心處理,以避免帳戶被約束或封鎖。
安全性:請確保你的API ID和API Hash安全,不要洩漏給他人。
經過這些過程,你可以抓取電報群組成員並將其增加到你的群組。不過,保證你是合法並且合規地進行這些操作。