Skip to content

Commit

Permalink
feat: support onedrive session, upload from url, work in memory
Browse files Browse the repository at this point in the history
- set default delete_flag in env
- auto relogin
- add from_users to filter user, need tg_user_name
- add /url file_url to upload from url
- add /clear to clear history
- can reply error
- no temp file anymore, all jobs will be working in memory.
  • Loading branch information
hlf20010508 committed Sep 13, 2023
1 parent 750e15c commit 5ff80e2
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 397 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
!auth_server.py
!ssl
!templates
!onedrive_large_file_uploader.py
!log.py
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,37 @@ That's why you need to prepare a lot of things to use this bot.
- `/start` to start with bot.
- `/auth` to authorize telegram and onedrive.
- `/status` to show pinned status message.
- `/clear` to clear all history except status message.
- `/links message_link range` to transfer sequential restricted content.
- `/url file_url` to upload file through url.
- `/autoDelete true/false` decides whether bot can auto delete message.
- `/help` for help.

Example:
`/links https://t.me/c/xxxxxxx/100 2` will transfer `https://t.me/c/xxxxxxx/100` and `https://t.me/c/xxxxxxx/101`.
`/url https://example.com/file.txt` will upload `file.txt` to Onedrive. It calls Onedrive's API, which means Onedrive's server will visit the url and download the file for you.

## Authorization Steps
- Send `/auth`.
- Wait and you'll receive the login code from telegram.
- Visit the uri the bot sends, and submit the code.
- After submission, it will redirect to the authorization uri for OneDrive. Login and authorize.
- If the bot says `Authorization successful!`, everything is done.
- After submission, it will send the authorization uri for OneDrive. Visit, login and authorize.
- If the bot says `Onedrive authorization successful!`, everything is done.

## Usage
- Add this bot to a group or channel.
- In the group or channel, forward or upload files(or videos, photos).
- If you want to transfer restricted content from a group or channel, right click the content, copy the message link, and send the link.
- Wait until the transfer completes. You can check status on pinned status message.
- Use `/help` for more information about other commands.

## Preparation
- Open `docker-compose.yml` and edit the environment config.
- `server_uri` is your domain. You need to specify a port, like `https://example.com:8080`, or `https://127.0.0.1:8080` if you don't have a web server. Protocol must be "https", not "http". The self-signed ssl files may be expired, if so you can generate it on your own, or wait for my update.
- Create a Telegram bot through [BotFather](https://t.me/BotFather). Record `token` as `tg_bot_token`.
- 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`.
- `tg_user_phone` is the phone number you just used to login to my.telegram.org.
- `tg_user_name` is your telegram user name. Check your profile, find your user name, it should be like `@user`, then record `user` as `tg_user_name`. Optional, default to void. If you don't set this parameter, every one can control your bot.
- Create a OneDrive application on [portal.azure.com](https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade) App registrations.
- Press `New registrations`.
- Fill `Name`.
Expand All @@ -57,7 +62,8 @@ Example:
- Press `Register`.
- In application's `Overview`, record `Application (client) ID` as `od_client_id`.
- 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`.
- `remote_root_path` is a directory on OneDrive. Like `/MyFiles/Telegram`.
- `remote_root_path` is a directory on OneDrive. Like `/MyFiles/Telegram`. Default to `/`.
- `delete_flag` decides whether bot can auto delete message. Pass `true` or `false`. Optional, default to `false`.

## Launch Through Docker
```sh
Expand Down
34 changes: 10 additions & 24 deletions auth_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

app = Flask(__name__)

temp_dir = "temp"

if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
code_tg = ''
code_od = ''


@app.route("/")
Expand All @@ -23,40 +21,28 @@ def telegram_code_index():

@app.route("/tg", methods=["GET", "POST"])
def telegram_code():
code_path = os.path.join(temp_dir, "tg_code")
global code_tg
if request.method == "POST":
code = request.json["code"]
with open(code_path, "w") as file:
file.write(code)
code_tg = request.json["code"]
return jsonify({"success": True})
if request.method == "GET":
if not os.path.exists(code_path):
if not code_tg:
return jsonify({"success": False})
else:
code = ""
with open(code_path, "r") as file:
code = file.read()
os.remove(code_path)
return jsonify({"success": True, "code": code})
return jsonify({"success": True, "code": code_tg})


@app.route("/auth")
def onedrive_code():
code_path = os.path.join(temp_dir, "od_code")
global code_od
if not request.args.get("get"):
code = request.args.get("code")
with open(code_path, "w") as file:
file.write(code)
code_od = request.args.get("code")
return "Authorization Successful!"
else:
if not os.path.exists(code_path):
if not code_od:
return jsonify({"success": False})
else:
code = ""
with open(code_path, "r") as file:
code = file.read()
os.remove(code_path)
return jsonify({"success": True, "code": code})
return jsonify({"success": True, "code": code_od})


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 5ff80e2

Please sign in to comment.