-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransferbot.py
More file actions
executable file
·212 lines (184 loc) · 7.76 KB
/
Copy pathtransferbot.py
File metadata and controls
executable file
·212 lines (184 loc) · 7.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Send any telegram-supported media to this bot and it will be uploaded over transfer.sh
Usage:
$ python transferbot.py
"""
__author__ = "jhonata.poma@gmail.com (Jhonata 'bomba' Poma)"
import logging, re, requests, os
from telegram.ext import Updater, CommandHandler, MessageHandler
from telegram.ext import Filters, CallbackQueryHandler
from telegram.ext.dispatcher import run_async
VERSION = '0.2'
FILES_POOL = '/tmp/'
CONFIG_FILE = 'conf/token.conf'
logging.basicConfig (format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger (__name__)
try:
TOKEN = open (CONFIG_FILE, 'r').read ().replace ("\n", "")
except Exception, e:
logger.error ("Could not find '%s'." %(CONFIG_FILE))
exit (1)
def remove (filename):
""" Removes the file after transfer.
"""
try:
os.remove (FILES_POOL + filename)
except Exception, e:
logger.error ("Something went wrong, backtrace: \n%s" %(e))
return
logger.info ("Removed %s", filename)
def transfer (filename):
""" Sends filename then calls remove to delete it. Returns transfer.sh file's url.
"""
upload_url = "https://transfer.sh/" + filename
try:
# Upload file
req = requests.put (url=upload_url, data=open (FILES_POOL + filename, "r"))
except Exception, e:
logger.error ("Something went wrong, backtrace: \n%s" %(e))
return (e)
logger.info ("Transferred %s", filename)
remove (filename)
try:
# Request shall return 'https://transfer.sh/AAAA/filename'
return (req.text.strip ())
except Exception, e:
logger.error ("Something went wrong, backtrace: \n%s" %(e))
return (e)
def download (document, filename):
""" Downloads attachment
"""
try:
document.download (FILES_POOL + filename)
except Exception, e:
logger.error ("Something went wrong, backtrace: \n%s" %(e))
return False
return True
def checkSize (filesize, update):
""" Checks if filesize fits inside 20mb. True if it does
"""
SIZE_LIMIT = 20971520
if (filesize > SIZE_LIMIT):
update.message.reply_text ("Your file is too big! Size limited to 20mb by Telegram Bot API", quote=True)
logger.warn ("Rejected file, size was %s" %(filesize))
return False
return True
# HANDLERS
def cmd_start (bot, update):
""" Send a message when the command /start is issued.
"""
update.message.reply_text ('Transferbot ' + VERSION)
update.message.reply_text ('Just send a picture, video, song or any other of telegram-supported ' \
'media to upload it over transfer.sh')
def cmd_help (bot, update):
""" Send a message when the command /help is issued.
"""
update.message.reply_text ('Just send a picture, video, song or any other of telegram-supported ' \
'media to upload it over transfer.sh')
def cmd_unknown (bot, update):
""" Send a message if the command is not defined.
"""
update.message.reply_text ('Command not found. Type /help.')
def cmd_error (bot, update, error):
""" Log Errors caused by updates.
"""
logger.warning ('Update "%s" caused error "%s"', update, error)
# ATTACHMENTS MGMT
@run_async
def fbk_document (bot, update):
""" Get document, then transfer it.
"""
if (checkSize (update.message.document.file_size, update)):
user = update.message.from_user
document = bot.get_file (update.message.document.file_id)
if (download (document, update.message.document.file_name)):
logger.info ("Got document from %s: %s", user.username, update.message.document.file_name)
update.message.reply_text ('Your transfer.sh link: ' + transfer (update.message.document.file_name),
quote=True)
@run_async
def fbk_audio (bot, update):
""" Get audio, then transfer it.
"""
if (checkSize (update.message.audio.file_size, update)):
FIRST_EMT = 0
ext = re.findall (r'/(\w+)', update.message.audio.mime_type)[FIRST_EMT]
filename = update.message.audio.file_id + '.' + ext
user = update.message.from_user
document = bot.get_file (update.message.audio.file_id)
if (download (document, filename)):
logger.info ("Got audio from %s: %s", user.username, filename)
update.message.reply_text ('Your transfer.sh link: ' + transfer (filename),
quote=True)
@run_async
def fbk_voice (bot, update):
""" Get audio, then transfer it.
"""
if (checkSize (update.message.voice.file_size, update)):
FIRST_EMT = 0
ext = re.findall (r'/(\w+)', update.message.voice.mime_type)[FIRST_EMT]
filename = update.message.voice.file_id + '.' + ext
user = update.message.from_user
document = bot.get_file (update.message.voice.file_id)
if (download (document, filename)):
logger.info ("Got voice from %s: %s", user.username, filename)
update.message.reply_text ('Your transfer.sh link: ' + transfer (filename),
quote=True)
@run_async
def fbk_video (bot, update):
""" Get video, then transfer it.
"""
if (checkSize (update.message.video.file_size, update)):
FIRST_EMT = 0
ext = re.findall (r'/(\w+)', update.message.video.mime_type)[FIRST_EMT]
filename = update.message.video.file_id + '.' + ext
user = update.message.from_user
document = bot.get_file (update.message.video.file_id)
if (download (document, filename)):
logger.info ("Got video from %s: %s", user.username, filename)
update.message.reply_text ('Your transfer.sh link: ' + transfer (filename),
quote=True)
@run_async
def fbk_photo (bot, update):
""" Get chat photo, the biggest from the list
"""
# Get the last picture of the set, highest resolution
pic_index = len (update.message.photo) - 1
if (checkSize (update.message.photo[pic_index].file_size, update)):
filename = update.message.photo[pic_index].file_id + '.jpg'
user = update.message.from_user
document = bot.get_file (update.message.photo[pic_index].file_id)
if (download (document, filename)):
logger.info ("Got photo from %s: %s", user.username, filename)
update.message.reply_text ('Your transfer.sh link: ' + transfer (filename),
quote=True)
# MAIN
def main ():
""" Start the bot.
"""
# Create the EventHandler and pass it your bot's token.
updater = Updater (TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# On different commands - answer in Telegram
dp.add_handler (CommandHandler ("start", cmd_start))
dp.add_handler (CommandHandler ("help", cmd_help))
# On unknown command, put some help text
dp.add_handler (MessageHandler (Filters.command, cmd_unknown))
# We don't need text interactions, give /help instead
dp.add_handler (MessageHandler (Filters.text, cmd_help))
# Add stuff to handle
dp.add_handler (MessageHandler (Filters.photo, fbk_photo))
dp.add_handler (MessageHandler (Filters.audio, fbk_audio))
dp.add_handler (MessageHandler (Filters.voice, fbk_voice))
dp.add_handler (MessageHandler (Filters.video, fbk_video))
dp.add_handler (MessageHandler (Filters.document, fbk_document))
# log all errors
dp.add_error_handler (cmd_error)
# Start the Bot
updater.start_polling ()
logger.info ('Kicking')
# Loop until SIGNALS
updater.idle ()
if __name__ == '__main__':
main ()