
學習是相對的。在我對python零基礎的條件下,開始學用ChatGPT,真的好費勁,因爲我看不懂腳本的語法、邏輯,也無法理解報錯的代碼。在這個過程中,反覆的和AI對話,讓我逐漸總結出它的思維特點,然後逐漸適應了和它的溝通方式。
我沒有把AI訓練成貓娘人格,相反,在努力訓練自己總結和它的溝通規律,得以讓我的對話,更符合它能有效理解和遵行的需求。
花了十幾個小時,寫了幾個簡單的腳本:
from telethon import TelegramClient
api_id = ID
api_hash = 'hash'
phone_number = '+86xxxx'
client = TelegramClient('session_name', api_id, api_hash)
async def main():
await client.connect()
if not await client.is_user_authorized():
await client.send_code_request(phone_number)
await client.sign_in(phone_number, input('Enter the code: '))
dialogs = await client.get_dialogs()
for dialog in dialogs:
print(f'ID: {dialog.id} Name: {dialog.name}')
await client.disconnect()
if __name__ == '__main__':
client.loop.run_until_complete(main())
運行這個腳本,提示進行對應手機號的登錄。登錄完成,就會以列表的形式,顯示所有的聊天,並且標識出對應的ID。
from telethon import TelegramClient, events, sync
api_id = ID
api_hash = 'hash'
session_file = 'session_name.session'
client = TelegramClient(session_file, api_id, api_hash)
client.start()
group_entity = client.get_entity(-100xxxx)#對話ID
messages = client.iter_messages(group_entity)
message_ids = [message.id for message in messages]
client.delete_messages(group_entity, message_ids)
client.disconnect()
這個腳本的功能,則是運行後,刪除指定對話ID(也可以是有管理員權限的羣組或者頻道)的所有消息。
from telethon import TelegramClient, events
api_id = ID
api_hash = 'hash'
# 填写多个 source_chat_id 和多个 target_user
source_chat_ids = [-100XXXXXXXXXX, -100XXXXXXXXXX, -100XXXXXXXXXX] # 需要監聽的來源群組
target_users = [-100XXXXXXXXXX] # 要轉發到的目標用戶
client = TelegramClient('session_name', api_id, api_hash)
# 处理消息事件
@client.on(events.NewMessage(chats=source_chat_ids))
async def handler(event):
for target_user in target_users:
await client.forward_messages(target_user, event.message)
async def main():
# 连接到 Telegram
await client.start()
print('已连接到 Telegram!')
# 获取多个源聊天实体
source_entities = []
dialogs = await client.get_dialogs()
for dialog in dialogs:
if dialog.id in source_chat_ids:
source_entities.append(dialog.entity)
# 开始监听消息
await client.run_until_disconnected()
with client:
client.loop.run_until_complete(main())
這個的功能,是從開始運行,就監聽一個或者多個對話(也可以是羣組或者頻道)的新消息,並且轉發到目標羣組。
from pyrogram import Client, errors
import time
# replace with your own API ID and API Hash
API_ID = ID
API_HASH = 'hash'
# replace with your source and target group ID
SOURCE_GROUP_ID = -100XXXXXXXXXX #源羣組
TARGET_GROUP_ID = -100XXXXXXXXXX #目標羣組
# create a Pyrogram client instance
app = Client('my_account', api_id=API_ID, api_hash=API_HASH)
# define a function to backup messages from source group to target group
def backup_messages(app):
# get messages from source group
try:
messages = app.get_chat_history(chat_id=SOURCE_GROUP_ID)
except errors.exceptions.chat_id_invalid:
print("Invalid chat ID.")
return
# sort messages by date
messages = sorted(messages, key=lambda m: m.date)
# set counter for messages
count = 0
# loop through messages and send them to target group
for message in messages:
if not message.service:
# if the message is not a service message, forward it to target group
if message.text:
app.send_message(chat_id=TARGET_GROUP_ID, text=message.text)
elif message.photo:
app.send_photo(chat_id=TARGET_GROUP_ID, photo=message.photo.file_id, caption=message.caption)
elif message.video:
app.send_video(chat_id=TARGET_GROUP_ID, video=message.video.file_id, caption=message.caption)
elif message.audio:
app.send_audio(chat_id=TARGET_GROUP_ID, audio=message.audio.file_id, caption=message.caption)
elif message.voice:
app.send_voice(chat_id=TARGET_GROUP_ID, voice=message.voice.file_id, caption=message.caption)
elif message.document:
app.send_document(chat_id=TARGET_GROUP_ID, document=message.document.file_id, caption=message.caption)
# increase message counter
count += 1
# check if we need to wait
if count % 100 == 0:
print(f"Forwarded {count} messages. Waiting for 5 minutes...")
time.sleep(300)
# start the app and backup messages
with app:
backup_messages(app)
這個腳本,需有一個賬戶同時加入源羣組和目標羣組,它將會按照時間順序把源羣組的所有消息(不包括服務消息)轉發到目標羣組。
转载请注明:刘太监的私藏 » 學用ChatGPT寫python腳本