Skip to content

Commit

Permalink
feat: support storing sessions in volume
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Sep 15, 2023
1 parent 9a5d2b9 commit 45f0f80
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ log
*.session
*.session-journal
temp
session
dev.sh
test.py
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
command: python bot.py
# volumes:
# telegram-onedrive-session:
5 changes: 3 additions & 2 deletions onedrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 45f0f80

Please sign in to comment.