forked from uaudith/Virustotal-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegrambot.py
139 lines (124 loc) · 3.62 KB
/
telegrambot.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
import math
import os
import time
from pyrogram import Client, filters
from pyrogram.types import ForceReply, InputMediaPhoto
from config import Config
from virustotal import virus
msgdic = {}
app = Client(
"hey",
api_id=Config.APP_ID,
api_hash=Config.API_HASH,
bot_token=Config.BOT_TOKEN
)
def progress(client, current, total, message_id, chat_id, start):
now = time.time()
diff = now - start
if round(diff % 5.00) == 0 or current == total:
percentage = current * 100 / total
speed = current / diff
elapsed_time = round(diff) * 1000
time_to_completion = round((total - current) / speed) * 1000
estimated_total_time = elapsed_time + time_to_completion
elapsed_time = TimeFormatter(milliseconds=elapsed_time)
estimated_total_time = TimeFormatter(milliseconds=estimated_total_time)
progress = "[{0}{1}] \nPercentage: {2}%\n".format(
''.join(["█" for i in range(math.floor(percentage / 5))]),
''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
round(percentage, 2)
)
tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
humanbytes(current),
humanbytes(total),
humanbytes(speed),
estimated_total_time if estimated_total_time != '' else "0 s"
)
try:
client.edit_message_text(
chat_id,
message_id,
text="Downloading...\n {}".format(tmp)
)
except:
pass
def humanbytes(size):
if not size:
return ""
power = 2**10
n = 0
Dic_powerN = {0: ' ', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
while size > power:
size /= power
n += 1
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
def TimeFormatter(milliseconds: int) -> str:
seconds, milliseconds = divmod(int(milliseconds), 1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
tmp = ((str(days) + "d, ") if days else "") + \
((str(hours) + "h, ") if hours else "") + \
((str(minutes) + "m, ") if minutes else "") + \
((str(seconds) + "s, ") if seconds else "") + \
((str(milliseconds) + "ms, ") if milliseconds else "")
return tmp[:-2]
@app.on_message(filters.document)
def download_telegram_media(client, message):
me=Config.userid
if not message.from_user.id == me :
client.send_message(
chat_id=message.chat.id,
text = 'Please host your own bot at \nhttps://github.com/uaudith/Virustotal-telegram'
)
return
msg = client.send_message(
chat_id=message.chat.id,
text='Download is being started...\nPlease Wait !'
)
start_time = time.time()
download_location = client.download_media(
message=message,
file_name='./',
progress=progress,
progress_args=(
msg.message_id,
message.chat.id,
start_time
)
)
userid=message.from_user.id
client.delete_messages(userid,msg.message_id)
print('a')
check_size(download_location,userid)
print('b')
def send_msg(user,txt):
app.send_message(user,txt,parse_mode="markdown")
def check_size(path,userid):
viruslist = []
reasons=[]
b=os.path.getsize(path)
print('file size is',b)
obj=virus(str(path))
if b>32*1024*1024:
send_msg(userid,'Sorry This file is larger than 32Mb')
# obj.large_files()
return
else:
obj.smallfiles()
if obj.res==False:
send_msg(userid,'Error')
send_msg(userid,obj.verbose)
time.sleep(7)
obj.get_report()
for i in obj.report:
if obj.report[i]['detected']==True:
viruslist.append(i)
reasons.append('➤ '+obj.report[i]['result'])
if len(viruslist) > 0:
names=' , '.join(viruslist)
reason='\n'.join(reasons)
send_msg(userid,'\n☣ --Threats have been detected !-- ☣\n\n**{}** \n\n\n**Description**\n\n`{}`\n\n[Detailed Report]({})'.format(names,reason,obj.link))
else:
send_msg(userid,'✔️ File is clean ')
app.run()