-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
128 lines (98 loc) Β· 4.72 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
# Uvindu Bro <https://t.me/UvinduBro>
# @Uvindu_Bro
from telethon import TelegramClient, events, Button
from urldownload import download_file
from config import BOTTOKEN, APIID, APIHASH, DOWNLOADPATH, USERNAME
import os
import time
import datetime
import asyncio
import aiohttp
from handlers import progress, humanbytes, time_formatter, convert_from_bytes
import traceback
bot = TelegramClient('UvinduBro', APIID, APIHASH).start(bot_token=BOTTOKEN)
def get_date_in_two_weeks():
#get maximum date of storage for file by returns date in two weeks
today = datetime.datetime.today()
date_in_two_weeks = today + datetime.timedelta(days=14)
return date_in_two_weeks.date()
async def send_to_transfersh_async(file):
size = os.path.getsize(file)
size_of_file = humanbytes(size)
final_date = get_date_in_two_weeks()
file_name = os.path.basename(file)
print("\nπ Uploading file: {} (size of the file: {})".format(file_name, size_of_file))
url = 'https://transfer.sh/'
with open(file, 'rb') as f:
async with aiohttp.ClientSession() as session:
async with session.post(url, data={str(file): f}) as response:
download_link = await response.text()
print("Link to download file (will be saved till {}):\n{}".format(final_date, download_link))
return download_link, final_date, size_of_file
@bot.on(events.NewMessage(pattern='/start'))
async def start(event):
#send a message when the command /start is issued.
await event.respond('Hello, I am Upload Bot.\n\nSend me any direct link and reply it with /upload for upload it to Telegram as file.\n\nSend me any file and reply it with /transfersh to generate direct download link of that file.\n\nA bot by @Uvindu_Bro π±π°',
buttons=[
[Button.url("π¦ Source Code", url="https://github.com/UvinduBro/TeleUploadBot"),
Button.url("π Channel", url="https://t.me/UvinduBro")]])
raise events.StopPropagation
@bot.on(events.NewMessage(pattern='/upload'))
async def up(event):
if event.reply_to_msg_id:
start = time.time()
url = await event.get_reply_message()
ilk = await event.respond("π₯ Downloading π₯")
try:
filename = os.path.join(DOWNLOADPATH, os.path.basename(url.text))
file_path = await download_file(url.text, filename, ilk, start, bot)
except Exception as e:
print(e)
await event.respond(f"Downloading Failed\n\n**Error:** {e}")
await ilk.delete()
try:
orta = await event.respond("π€ Uploading to Telegram π€")
dosya = await bot.upload_file(filename, progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(d, t, orta, start, "Uploading to Telegram...")
))
zaman = str(time.time() - start)
await bot.send_file(event.chat.id, dosya, force_document=True, caption=f"Uploaded By {USERNAME}")
except Exception as e:
traceback.print_exc()
print(e)
await event.respond(f"Uploading Failed\n\n**Error:** {e}")
await orta.delete()
raise events.StopPropagation
@bot.on(events.NewMessage(pattern='/transfersh'))
async def tsh(event):
if event.reply_to_msg_id:
start = time.time()
url = await event.get_reply_message()
ilk = await event.respond("π₯ Downloading π₯")
try:
file_path = await url.download_media(progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(d, t, ilk, start, "π₯ Downloading π₯")
))
except Exception as e:
traceback.print_exc()
print(e)
await event.respond(f"β Downloading Failed\n\n**Error:** {e}")
await ilk.delete()
try:
orta = await event.respond("π€ Uploading to TransferSh π€")
download_link, final_date, size = await send_to_transfersh_async(file_path)
zaman = str(time.time() - start)
await orta.edit(f"File Successfully Uploaded to TransferSh.\n\nπ Link {download_link}\nπ
Expired Date {final_date}\n\n~By @UvinduBro π±π° ~")
except Exception as e:
traceback.print_exc()
print(e)
await event.respond(f"β Uploading Failed\n\n**Error:** {e}")
raise events.StopPropagation
def main():
if not os.path.isdir(DOWNLOADPATH):
os.mkdir(DOWNLOADPATH)
#start the bot.
print("\nβ
Your bot started!\n\nDo visit @UvinduBro")
bot.run_until_disconnected()
if __name__ == '__main__':
main()