-
Notifications
You must be signed in to change notification settings - Fork 14
/
bot.py
146 lines (117 loc) · 4.65 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import (
Flask,
request,
make_response,
Response,
stream_with_context
)
import os
import requests
import telebot
from telebot import types
import threading
from config import *
from DbHandler import DbHandler
from security import *
import tg_client
db = DbHandler(DB_URL)
server = Flask(__name__)
bot = telebot.TeleBot(API_TOKEN, threaded=True)
big_files = {}
TELEGRAM_FILE_URL = "https://api.telegram.org/file/bot{token}/".format(
token=API_TOKEN) + "{file_path}"
def google_url_shorten(url):
try:
req_url = "https://www.googleapis.com/urlshortener/v1/url?key={api_key}".format(
api_key=GOOGLE_URL_SHORTEN_API)
r = requests.post(req_url, json={'longUrl': url}, headers={
'content-type': 'application/json'})
return r.json()['id']
except Exception:
return url
@bot.message_handler(content_types=['document'])
def get_link(message):
replied_msg = bot.forward_message(
CHANNEL_REPLY_ID, message.chat.id, message.message_id)
if (message.chat.id in big_files):
big_files[message.chat.id].append({
'file_size': message.document.file_size,
'message_id': replied_msg.message_id
})
return bot.reply_to(message,
'Gotcha! Send more files or `/end filename` to end',
parse_mode='Markdown')
else:
hash_d = encode(replied_msg.message_id, API_TOKEN)
db.insert('links', {
'hash': hash_d,
'file_name': message.document.file_name,
'file_size': message.document.file_size,
'message_id': [replied_msg.message_id]
})
url = WEBHOOK_URL + '/d?id={hash}'.format(hash=hash_d)
return bot.reply_to(message, google_url_shorten(url))
@bot.message_handler(commands=['help'])
def help(message):
text = """You can send a file and you will receive a link to download from any browser.
You can also use /big and send multiple files, then use `/end filename` to get a link that will download your files appended. To split your files (Telegram supports up to 1,5GB) you can use this app for Android https://forum.xda-developers.com/showthread.php?t=2402650
You can use other apps to split (I accept suggestions @victor141516) or do it manually using a computer and any hex editor."""
return bot.send_message(message.chat.id, text, parse_mode='Markdown')
@bot.message_handler(commands=['big'])
def begin_big(message):
big_files[message.chat.id] = []
return bot.reply_to(message, 'You can now send as much files as you want, they will be downloaded together as one single file (beware order!!)')
@bot.message_handler(commands=['end'])
def end_big(message):
file_name = message.text.split('/end')[1][1:]
if (file_name == ''):
return bot.reply_to(message, "Operation canelled")
files = big_files[message.chat.id]
if (len(files) == 0):
return bot.reply_to(message, "You sent no files!")
size = 0
message_ids = []
for each_file in files:
size += each_file['file_size']
message_ids.append(each_file['message_id'])
hash_d = encode(files[0]['message_id'], API_TOKEN)
db.insert('links', {
'hash': hash_d,
'file_name': file_name,
'file_size': size,
'message_id': message_ids
})
url = WEBHOOK_URL + '/d?id={hash}'.format(hash=hash_d)
return bot.reply_to(message, google_url_shorten(url))
@server.route("/bot", methods=['POST'])
def getMessage():
bot.process_new_updates(
[telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/d")
def serve_file():
file_hash = request.args.get('id', default=0, type=str)
link = db.select('links', 'hash = \'{hash}\''. format(hash=file_hash))
if (link is False or len(link) == 0):
return "File not found"
else:
link = link[0]
stream = stream_with_context(tg_client.get_file_stream(link['message_id']))
return Response(stream,
headers={
'Content-Type': 'application/octet-stream',
'Content-Length': link['file_size'],
'Content-Disposition': 'attachment; filename="{file_name}"'.format(file_name=link['file_name'])
})
@server.route("/")
def webhook():
webhook = bot.get_webhook_info()
bot.remove_webhook()
bot.set_webhook(url=WEBHOOK_URL + "/bot")
return "!", 200
if (POLLING):
bot.remove_webhook()
thread = threading.Thread(target=bot.polling)
thread.daemon = True
thread.start()
server.run(host="0.0.0.0", port=os.environ.get('PORT', 5000))