来源:本站时间:2024-12-11
抓取電報群組成員並將其增加到你的群組中涉及到一些雜亂的技能過程和恪守電報渠道的方針。下面是一個概述:
Python程式設計語言
Telethon函式庫(一個Telegram API函式庫)
创立Telegram应用:
拜訪Telegram's API開發者頁面並運用你的Telegram帳號登入。
創立一個新的應用程式以取得API ID和API Hash。
编写抓取成员的Python脚本:
from 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脚本:
from 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安全,不要走漏给别人。
经过这些过程,你可以抓取电报群组成员并将其增加到你的群组中。不过,保证你是合法并且合规地进行这些操作。