因此,我正在尝试使用Telegram的API,并制作一个程序来从csv文件中获取数据,并向Telegram上的人发送个性化消息。该程序运行良好,但我想处理csv文件中的数据,以按用户名忽略特定用户,或者只是删除csv文件中的用户所在的行,这样他们就不会收到消息。
我试图集成熊猫,但是我不知道如何使用它,我只是弄乱了我的代码。
input_file = sys.argv[1]
users = []
with open(input_file, encoding='UTF-8') as f:
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)
mode = int(input("Enter 1 to send by user ID or 2 to send by username: "))
OR
for user in users:
if mode == 2:
if user['username'] == "":
continue
receiver = client.get_input_entity(user['username'])
elif mode == 1:
receiver = InputPeerUser(user['id'],user['access_hash'])
else:
print("Invalid Mode. Exiting.")
client.disconnect()
sys.exit()
message = random.choice(messages)
try:
print("Sending Message to:", user['name'])
client.send_message(receiver, message.format(user['name']))
print("Waiting {} seconds".format(SLEEP_TIME))
time.sleep(SLEEP_TIME)