telegram: Support media in messages for using a webserver (not included in
patch 3576c9bc94562bf539d31f44a3ac08d9ed4a7d05
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Jan 30 01:29:08 CET 2022
* telegram: Support media in messages for using a webserver (not included in
irgramd) to get the downloaded media from Telegram, also support
non-downloadable media (geo, contact, etc.)
hunk ./irgramd 68
+ tornado.options.define('media_url', default=None, metavar='BASE_URL', help='Base URL for media files, should be configured in the external (to irgramd) webserver')
hunk ./telegram.py 13
+from utils import sanitize_filename
hunk ./telegram.py 34
+ self.media_url = settings['media_url']
+ self.media_cn = 0
hunk ./telegram.py 246
- message = '[{}] {}'.format(mid, event.message.message)
+
+ if event.message.media:
+ text = await self.handle_telegram_media(event.message)
+ else:
+ text = event.message.message
+
+ message = '[{}] {}'.format(mid, text)
hunk ./telegram.py 269
-
- # Format messages with media
-# if event.message.media and (event.message.photo or event.message.gif):
-# message = await self.download_telegram_media(event.message, 'Image')
-# if message:
-# messages.insert(0, message)
-# elif event.message.media and (event.message.sticker):
-# messages.insert(0, 'Sticker: {}'.format(event.message.sticker.id))
-
hunk ./telegram.py 304
- async def download_telegram_media(self, message, tag):
+ async def handle_telegram_media(self, message):
+ caption = ' | {}'.format(message.message) if message.message else ''
+ to_download = True
+ media_url_or_data = ''
+
+ if message.web_preview:
+ media_type = 'web'
+ logo = await self.download_telegram_media(message)
+ to_download = False
+ media_url_or_data = message.message
+ if message.media.webpage.title and logo:
+ caption = ' | {} | {}'.format(message.media.webpage.title, logo)
+ elif message.media.webpage.title:
+ caption = ' | {}'.format(message.media.webpage.title)
+ elif logo:
+ caption = ' | {}'.format(logo)
+ else:
+ caption = ''
+
+ elif message.photo: media_type = 'photo'
+ elif message.audio: media_type = 'audio'
+ elif message.voice: media_type = 'rec'
+ elif message.video: media_type = 'video'
+ elif message.video_note: media_type = 'videorec'
+ elif message.gif: media_type = 'anim'
+ elif message.sticker: media_type = 'sticker'
+ elif message.document: media_type = 'file'
+
+ elif message.contact:
+ media_type = 'contact'
+ caption = ''
+ to_download = False
+ if message.media.contact.first_name:
+ media_url_or_data += message.media.contact.first_name + ' '
+ if message.media.contact.last_name:
+ media_url_or_data += message.media.contact.last_name + ' '
+ if message.media.contact.phone_number:
+ media_url_or_data += message.media.contact.phone_number
+
+ elif message.game:
+ media_type = 'game'
+ caption = ''
+ to_download = False
+ if message.media.game.title:
+ media_url_or_data = message.media.game.title
+
+ elif message.geo:
+ media_type = 'geo'
+ caption = ''
+ to_download = False
+ media_url_or_data = 'lat: {}, long: {}'.format(message.media.geo.lat, message.media.geo.long)
+
+ elif message.invoice:
+ media_type = 'invoice'
+ caption = ''
+ to_download = False
+ media_url_or_data = ''
+
+ elif message.poll:
+ media_type = 'poll'
+ caption = ''
+ to_download = False
+ media_url_or_data = ''
+
+ elif message.venue:
+ media_type = 'venue'
+ caption = ''
+ to_download = False
+ media_url_or_data = ''
+
+ if to_download:
+ media_url_or_data = await self.download_telegram_media(message)
+
+ return '[{}] {}{}'.format(media_type, media_url_or_data, caption)
+
+ async def download_telegram_media(self, message):
hunk ./telegram.py 381
- return
- if not local_path:
- return
-
- request = tornado.httpclient.HTTPRequest(
- url = 'https://yld.me/paste',
- method = 'POST',
- body = open(local_path, 'rb').read(),
- )
- response = await tornado.httpclient.AsyncHTTPClient().fetch(request)
-
- os.unlink(local_path)
- return tag + ': ' + response.body.decode().strip()
+ if not local_path: return ''
+
+ if message.document:
+ new_file = sanitize_filename(os.path.basename(local_path))
+ else:
+ filetype = os.path.splitext(local_path)[1]
+ new_file = str(self.media_cn) + filetype
+ self.media_cn += 1
+
+ new_path = os.path.join(self.telegram_media_dir, new_file)
+ if local_path != new_path:
+ os.replace(local_path, new_path)
+ if self.media_url[-1:] != '/':
+ self.media_url += '/'
+ return self.media_url + new_file
hunk ./utils.py 4
+import re
hunk ./utils.py 45
+
+def sanitize_filename(fn):
+ return re.sub('[/{}<>()"\'\\|&]', '', fn).strip('-')