{"id":653,"date":"2023-03-04T10:42:44","date_gmt":"2023-03-04T02:42:44","guid":{"rendered":"https:\/\/keyi.eu.org\/?p=653"},"modified":"2023-03-04T10:42:44","modified_gmt":"2023-03-04T02:42:44","slug":"%e5%ad%b8%e7%94%a8chatgpt%e5%af%abpython%e8%85%b3%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/keyi.eu.org\/?p=653","title":{"rendered":"\u5b78\u7528ChatGPT\u5bebpython\u8173\u672c"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"716\" height=\"297\" src=\"https:\/\/keyi.eu.org\/wp-content\/uploads\/2023\/03\/image-1.png\" alt=\"\" class=\"wp-image-654\" srcset=\"https:\/\/keyi.eu.org\/wp-content\/uploads\/2023\/03\/image-1.png 716w, https:\/\/keyi.eu.org\/wp-content\/uploads\/2023\/03\/image-1-300x124.png 300w\" sizes=\"(max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<p>\u5b78\u7fd2\u662f\u76f8\u5c0d\u7684\u3002\u5728\u6211\u5c0dpython\u96f6\u57fa\u790e\u7684\u689d\u4ef6\u4e0b\uff0c\u958b\u59cb\u5b78\u7528ChatGPT\uff0c\u771f\u7684\u597d\u8cbb\u52c1\uff0c\u56e0\u7232\u6211\u770b\u4e0d\u61c2\u8173\u672c\u7684\u8a9e\u6cd5\u3001\u908f\u8f2f\uff0c\u4e5f\u7121\u6cd5\u7406\u89e3\u5831\u932f\u7684\u4ee3\u78bc\u3002\u5728\u9019\u500b\u904e\u7a0b\u4e2d\uff0c\u53cd\u8986\u7684\u548cAI\u5c0d\u8a71\uff0c\u8b93\u6211\u9010\u6f38\u7e3d\u7d50\u51fa\u5b83\u7684\u601d\u7dad\u7279\u9ede\uff0c\u7136\u5f8c\u9010\u6f38\u9069\u61c9\u4e86\u548c\u5b83\u7684\u6e9d\u901a\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u6211\u6c92\u6709\u628aAI\u8a13\u7df4\u6210\u8c93\u5a18\u4eba\u683c\uff0c\u76f8\u53cd\uff0c\u5728\u52aa\u529b\u8a13\u7df4\u81ea\u5df1\u7e3d\u7d50\u548c\u5b83\u7684\u6e9d\u901a\u898f\u5f8b\uff0c\u5f97\u4ee5\u8b93\u6211\u7684\u5c0d\u8a71\uff0c\u66f4\u7b26\u5408\u5b83\u80fd\u6709\u6548\u7406\u89e3\u548c\u9075\u884c\u7684\u9700\u6c42\u3002<\/p>\n\n\n\n<p>\u82b1\u4e86\u5341\u5e7e\u500b\u5c0f\u6642\uff0c\u5beb\u4e86\u5e7e\u500b\u7c21\u55ae\u7684\u8173\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from telethon import TelegramClient\r\n\r\napi_id = ID\napi_hash = 'hash'\r\nphone_number = '+86xxxx'\r\nclient = TelegramClient('session_name', api_id, api_hash)\r\n\r\nasync def main():\r\n    await client.connect()\r\n    if not await client.is_user_authorized():\r\n        await client.send_code_request(phone_number)\r\n        await client.sign_in(phone_number, input('Enter the code: '))\r\n    \r\n    dialogs = await client.get_dialogs()\r\n    for dialog in dialogs:\r\n        print(f'ID: {dialog.id} Name: {dialog.name}')\r\n\r\n    await client.disconnect()\r\n\r\nif __name__ == '__main__':\r\n    client.loop.run_until_complete(main())\r\n<\/code><\/pre>\n\n\n\n<p>\u904b\u884c\u9019\u500b\u8173\u672c\uff0c\u63d0\u793a\u9032\u884c\u5c0d\u61c9\u624b\u6a5f\u865f\u7684\u767b\u9304\u3002\u767b\u9304\u5b8c\u6210\uff0c\u5c31\u6703\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\uff0c\u986f\u793a\u6240\u6709\u7684\u804a\u5929\uff0c\u4e26\u4e14\u6a19\u8b58\u51fa\u5c0d\u61c9\u7684ID\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from telethon import TelegramClient, events, sync\r\n\r\napi_id = ID\napi_hash = 'hash'\r\nsession_file = 'session_name.session'\r\n\r\nclient = TelegramClient(session_file, api_id, api_hash)\r\n\r\nclient.start()\r\n\r\ngroup_entity = client.get_entity(-100xxxx)#\u5c0d\u8a71ID\r\nmessages = client.iter_messages(group_entity)\r\n\r\nmessage_ids = &#91;message.id for message in messages]\r\nclient.delete_messages(group_entity, message_ids)\r\n\r\nclient.disconnect()\r\n<\/code><\/pre>\n\n\n\n<p>\u9019\u500b\u8173\u672c\u7684\u529f\u80fd\uff0c\u5247\u662f\u904b\u884c\u5f8c\uff0c\u522a\u9664\u6307\u5b9a\u5c0d\u8a71ID\uff08\u4e5f\u53ef\u4ee5\u662f\u6709\u7ba1\u7406\u54e1\u6b0a\u9650\u7684\u7fa3\u7d44\u6216\u8005\u983b\u9053\uff09\u7684\u6240\u6709\u6d88\u606f\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from telethon import TelegramClient, events\r\n\r\napi_id = ID\r\napi_hash = 'hash'\r\n\r\n# \u586b\u5199\u591a\u4e2a source_chat_id \u548c\u591a\u4e2a target_user\r\nsource_chat_ids = &#91;-100XXXXXXXXXX, -100XXXXXXXXXX, -100XXXXXXXXXX] # \u9700\u8981\u76e3\u807d\u7684\u4f86\u6e90\u7fa4\u7d44\r\ntarget_users = &#91;-100XXXXXXXXXX] # \u8981\u8f49\u767c\u5230\u7684\u76ee\u6a19\u7528\u6236\r\n\r\nclient = TelegramClient('session_name', api_id, api_hash)\r\n\r\n# \u5904\u7406\u6d88\u606f\u4e8b\u4ef6\r\n@client.on(events.NewMessage(chats=source_chat_ids))\r\nasync def handler(event):\r\n    for target_user in target_users:\r\n        await client.forward_messages(target_user, event.message)\r\n\r\nasync def main():\r\n    # \u8fde\u63a5\u5230 Telegram\r\n    await client.start()\r\n    print('\u5df2\u8fde\u63a5\u5230 Telegram\uff01')\r\n\r\n    # \u83b7\u53d6\u591a\u4e2a\u6e90\u804a\u5929\u5b9e\u4f53\r\n    source_entities = &#91;]\r\n    dialogs = await client.get_dialogs()\r\n    for dialog in dialogs:\r\n        if dialog.id in source_chat_ids:\r\n            source_entities.append(dialog.entity)\r\n\r\n    # \u5f00\u59cb\u76d1\u542c\u6d88\u606f\r\n    await client.run_until_disconnected()\r\n\r\nwith client:\r\n    client.loop.run_until_complete(main())\r\n<\/code><\/pre>\n\n\n\n<p>\u9019\u500b\u7684\u529f\u80fd\uff0c\u662f\u5f9e\u958b\u59cb\u904b\u884c\uff0c\u5c31\u76e3\u807d\u4e00\u500b\u6216\u8005\u591a\u500b\u5c0d\u8a71\uff08\u4e5f\u53ef\u4ee5\u662f\u7fa3\u7d44\u6216\u8005\u983b\u9053\uff09\u7684\u65b0\u6d88\u606f\uff0c\u4e26\u4e14\u8f49\u767c\u5230\u76ee\u6a19\u7fa3\u7d44\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pyrogram import Client, errors\r\nimport time\r\n\r\n# replace with your own API ID and API Hash\r\nAPI_ID = ID\nAPI_HASH = 'hash'\r\n\r\n# replace with your source and target group ID\r\nSOURCE_GROUP_ID = -100XXXXXXXXXX #\u6e90\u7fa3\u7d44\nTARGET_GROUP_ID = -100XXXXXXXXXX #\u76ee\u6a19\u7fa3\u7d44\r\n\r\n# create a Pyrogram client instance\r\napp = Client('my_account', api_id=API_ID, api_hash=API_HASH)\r\n\r\n# define a function to backup messages from source group to target group\r\ndef backup_messages(app):\r\n    # get messages from source group\r\n    try:\r\n        messages = app.get_chat_history(chat_id=SOURCE_GROUP_ID)\r\n    except errors.exceptions.chat_id_invalid:\r\n        print(\"Invalid chat ID.\")\r\n        return\r\n\r\n    # sort messages by date\r\n    messages = sorted(messages, key=lambda m: m.date)\r\n\r\n    # set counter for messages\r\n    count = 0\r\n\r\n    # loop through messages and send them to target group\r\n    for message in messages:\r\n        if not message.service:\r\n            # if the message is not a service message, forward it to target group\r\n            if message.text:\r\n                app.send_message(chat_id=TARGET_GROUP_ID, text=message.text)\r\n            elif message.photo:\r\n                app.send_photo(chat_id=TARGET_GROUP_ID, photo=message.photo.file_id, caption=message.caption)\r\n            elif message.video:\r\n                app.send_video(chat_id=TARGET_GROUP_ID, video=message.video.file_id, caption=message.caption)\r\n            elif message.audio:\r\n                app.send_audio(chat_id=TARGET_GROUP_ID, audio=message.audio.file_id, caption=message.caption)\r\n            elif message.voice:\r\n                app.send_voice(chat_id=TARGET_GROUP_ID, voice=message.voice.file_id, caption=message.caption)\r\n            elif message.document:\r\n                app.send_document(chat_id=TARGET_GROUP_ID, document=message.document.file_id, caption=message.caption)\r\n\r\n            # increase message counter\r\n            count += 1\r\n\r\n            # check if we need to wait\r\n            if count % 100 == 0:\r\n                print(f\"Forwarded {count} messages. Waiting for 5 minutes...\")\r\n                time.sleep(300)\r\n\r\n# start the app and backup messages\r\nwith app:\r\n    backup_messages(app)\r\n<\/code><\/pre>\n\n\n\n<p>\u9019\u500b\u8173\u672c\uff0c\u9700\u6709\u4e00\u500b\u8cec\u6236\u540c\u6642\u52a0\u5165\u6e90\u7fa3\u7d44\u548c\u76ee\u6a19\u7fa3\u7d44\uff0c\u5b83\u5c07\u6703\u6309\u7167\u6642\u9593\u9806\u5e8f\u628a\u6e90\u7fa3\u7d44\u7684\u6240\u6709\u6d88\u606f\uff08\u4e0d\u5305\u62ec\u670d\u52d9\u6d88\u606f\uff09\u8f49\u767c\u5230\u76ee\u6a19\u7fa3\u7d44\u3002<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/keyi.eu.org\">\u5218\u592a\u76d1\u7684\u79c1\u85cf<\/a> &raquo; <a href=\"https:\/\/keyi.eu.org\/?p=653\">\u5b78\u7528ChatGPT\u5bebpython\u8173\u672c<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u662f\u76f8\u5c0d\u7684\u3002\u5728\u6211\u5c0dpython\u96f6\u57fa\u790e\u7684\u689d\u4ef6\u4e0b\uff0c\u958b\u59cb\u5b78\u7528ChatGPT\uff0c\u771f\u7684\u597d\u8cbb\u52c1\uff0c\u56e0\u7232\u6211\u770b\u4e0d\u61c2\u8173\u672c\u7684\u8a9e\u6cd5\u3001\u908f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/keyi.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/653"}],"collection":[{"href":"https:\/\/keyi.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/keyi.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/keyi.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/keyi.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":4,"href":"https:\/\/keyi.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":658,"href":"https:\/\/keyi.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/658"}],"wp:attachment":[{"href":"https:\/\/keyi.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/keyi.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/keyi.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}