首页 > 解决方案 > Telegram 将 csv 文件中的抓取用户从特定行添加到特定行

问题描述

请有人可以帮助自定义波纹管代码,使其能够将 Telegram 抓取的用户从 CSV 文件从特定行添加到特定行(即:从 CSV 文件的第 13 行到第 32 行)。我需要它来避免通过在我的频道中添加我的群组用户来避免被电报禁止帐户。

from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
from telethon.tl.functions.channels import InviteToChannelRequest
import sys
import csv
import traceback
import time
import random

api_id = 111111111   #Enter Your 7 Digit Telegram API ID.
api_hash = '0000000000000000000000'   #Enter Yor 32 Character API Hash
phone = '+2000000000'   #Enter Your Mobilr Number With Country Code.
client = TelegramClient(phone, api_id, api_hash)
async def main():
   # Now you can use all client methods listed below, like for example...
   await client.send_message('me', 'Hello !!!!!')


SLEEP_TIME_1 = 300
SLEEP_TIME_2 = 300
with client:
   client.loop.run_until_complete(main())
client.connect()
if not client.is_user_authorized():
   client.send_code_request(phone)
   client.sign_in(phone, input('40779'))

users = []
with open(r"Scrapped.csv", encoding='UTF-8') as f:  #Enter your file name
   rows = csv.reader(f,delimiter=",",lineterminator="\n")
   next(rows, None)
   for row in rows:
       user = {}
       user['username'] = row[0]
       user['id'] = int(row[1])
       user['access_hash'] = int(row[2])
       user['name'] = row[3]
       users.append(user)

chats = []
last_date = None
chunk_size = 200
groups = []

result = 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 == True:
           groups.append(chat)
   except:
       continue

print('Choose a group to add members:')
i = 0
for group in groups:
   print(str(i) + '- ' + group.title)
   i += 1

g_index = input("Enter a Number: ")
target_group = groups[int(g_index)]

target_group_entity = InputPeerChannel(target_group.id, target_group.access_hash)

mode = int(input("Enter 1 to add by username or 2 to add by ID: "))

n = 0

for user in users:
   n += 1
   if n % 80 == 0:
       sleep(60)
   try:
       print("Adding {}".format(user['id']))
       if mode == 1:
           if user['username'] == "":
               continue
           user_to_add = client.get_input_entity(user['username'])
       elif mode == 2:
           user_to_add = InputPeerUser(user['id'], user['access_hash'])
       else:
           sys.exit("Invalid Mode Selected. Please Try Again.")
       client(InviteToChannelRequest(target_group_entity, [user_to_add]))
       print("Waiting for 120-180 Seconds...")
       time.sleep(random.randrange(120, 180))
   except PeerFloodError:
       print("Getting Flood Error from telegram. Script is stopping now. Please try again after some time.")
       print("Waiting {} seconds".format(SLEEP_TIME_2))
       time.sleep(SLEEP_TIME_2)
   except UserPrivacyRestrictedError:
       print("The user's privacy settings do not allow you to do this. Skipping.")
       print("Waiting for 30-60 Seconds...")
       time.sleep(random.randrange(30, 60))
   except:
       traceback.print_exc()
       print("Unexpected Error")
       continue 

提前致谢

标签: pythonpython-3.xcsvtelegram

解决方案


推荐阅读