From 45f0f8003931e4e6ac4037fd14b482f97c01991c Mon Sep 17 00:00:00 2001 From: hlf20010508 Date: Fri, 15 Sep 2023 15:44:33 +0800 Subject: [PATCH] feat: support storing sessions in volume --- .gitignore | 1 + README.md | 19 +++++++++++++++---- bot.py | 7 +++++-- docker-compose.yml | 8 ++++++-- onedrive.py | 5 +++-- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 988ca07..c9b8198 100755 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ log *.session *.session-journal temp +session dev.sh test.py \ No newline at end of file diff --git a/README.md b/README.md index 73923ce..bac9afc 100755 --- a/README.md +++ b/README.md @@ -53,14 +53,14 @@ Example: - Some web browser may prevent you from visiting this url because of ssl mismatch. Try using [Chromium](https://download-chromium.appspot.com). - If you want to specify your own ssl keys, especially if you have your own site, or the self-signed ssl keys have expired, you can import your ssl keys like this: - Create volumes for ssl keys in `docker-compose.yml`: - ```docker-compose + ```docker-compose.yml services: telegram-onedrive: - ... - volumes: + ... + volumes: - /path/to/*.crt:/telegram-onedrive/ssl/server.crt - /path/to/*.key:/telegram-onedrive/ssl/server.key - ... + ... ``` 3. Create a Telegram bot through [BotFather](https://t.me/BotFather). Record `token` as `tg_bot_token`. 4. Create a Telegram application on [my.telegram.org](https://my.telegram.org). See [details](https://docs.telethon.dev/en/stable/basic/signing-in.html). Record `api_id` as `tg_api_id`, `api_hash` as `tg_api_hash`. @@ -77,6 +77,17 @@ Example: - Go to application's `Certificates & secrets`, press `Client secrets`, and press `New client secret`. Then fill `Description`, and choose an `Expires`. Finnaly, press `Add`. Record `Value` as `od_client_secret`. 8. `remote_root_path` is a directory on OneDrive. Like `/MyFiles/Telegram`. Default to `/`. 9. `delete_flag` decides whether bot can auto delete message. Pass `true` or `false`. Optional, default to `false`. +10. Optional, to keep sessions after recreating docker container, create a volume to store it in docker-compose.yml: + ```docker-compose.yml + services: + telegram-onedrive: + ... + volumes: + - telegram-onedrive-session:/telegram-onedrive/session + ... + volumes: + telegram-onedrive-session: + ``` ## Launch Through Docker ```sh diff --git a/bot.py b/bot.py index c5694dd..ffb7675 100644 --- a/bot.py +++ b/bot.py @@ -19,6 +19,9 @@ from onedrive import Onedrive from log import logger +if not os.path.exists('session'): + os.mkdir('session') + urllib3.disable_warnings() status_bar = None @@ -50,11 +53,11 @@ delete_flag = True if os.environ.get("delete_flag", "false") == 'true' else False # clients -tg_bot = TelegramClient("bot", tg_api_id, tg_api_hash, sequential_updates=True).start( +tg_bot = TelegramClient("session/bot", tg_api_id, tg_api_hash, sequential_updates=True).start( bot_token=tg_bot_token ) -tg_client = TelegramClient("user", tg_api_id, tg_api_hash, sequential_updates=True) +tg_client = TelegramClient("session/user", tg_api_id, tg_api_hash, sequential_updates=True) onedrive = Onedrive( client_id=od_client_id, diff --git a/docker-compose.yml b/docker-compose.yml index 69ac9e9..04988ed 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,9 @@ services: restart: always network_mode: host # volumes: - # - /path/to/ssl:/telegram-onedrive/ssl + # - /path/to/*.crt:/telegram-onedrive/ssl/server.crt + # - /path/to/*.key:/telegram-onedrive/ssl/server.key + # - telegram-onedrive-session:/telegram-onedrive/session environment: - server_uri=$server_uri - tg_bot_token=$tg_bot_token @@ -18,4 +20,6 @@ services: - od_client_secret=$od_client_secret - remote_root_path=$remote_root_path - delete_flag=$delete_flag(optional) - command: python bot.py \ No newline at end of file + command: python bot.py +# volumes: +# telegram-onedrive-session: \ No newline at end of file diff --git a/onedrive.py b/onedrive.py index ccb790c..c61f7b1 100644 --- a/onedrive.py +++ b/onedrive.py @@ -48,6 +48,7 @@ def __init__(self, client_id, client_secret, redirect_uri, remote_root_path): auth_server_url=auth_server_url ) + self.session_path = 'session/onedrive.session' self.remote_root_path = remote_root_path self.client_secret = client_secret self.redirect_uri = redirect_uri @@ -69,10 +70,10 @@ def auth(self, auth_code): self.save_session() def save_session(self): - self.client.auth_provider.save_session(path='onedrive.session') + self.client.auth_provider.save_session(path=self.session_path) def load_session(self): - self.client.auth_provider.load_session(path='onedrive.session') + self.client.auth_provider.load_session(path=self.session_path) def stream_upload(self, buffer, name): request = self.client.item(path=self.remote_root_path).children[name].content.request()