exclam: Add upload (!upl) command to upload files/media to chats/channels
patch ca68ae9cfb315331dd08f8033ea2270e8a93e626
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Apr 14 22:48:30 CEST 2024
* exclam: Add upload (!upl) command to upload files/media to chats/channels
Add "upload_dir" option to define the local directory to pick the files up,
by default "~/.cache/irgramd/upload"
diff -rN -u old-irgramd/exclam.py new-irgramd/exclam.py
--- old-irgramd/exclam.py 2024-11-22 10:17:32.411437170 +0100
+++ new-irgramd/exclam.py 2024-11-22 10:17:32.411437170 +0100
@@ -6,6 +6,7 @@
# Use of this source code is governed by a MIT style license that
# can be found in the LICENSE file included in this project.
+import os
from telethon.errors.rpcerrorlist import MessageNotModifiedError, MessageAuthorRequiredError
from utils import command, HELP
@@ -18,6 +19,7 @@
'!ed': (self.handle_command_ed, 2, 2, 2),
'!del': (self.handle_command_del, 1, 1, -1),
'!fwd': (self.handle_command_fwd, 2, 2, -1),
+ '!upl': (self.handle_command_upl, 1, 2, 2),
}
self.tg = telegram
self.irc = telegram.irc
@@ -141,3 +143,23 @@
'Forward a message with <compact_id> to <chat> channel/chat.'
)
return reply
+
+ async def handle_command_upl(self, file=None, caption=None, help=None):
+ if not help:
+ try:
+ file_path = os.path.join(self.tg.telegram_upload_dir, file)
+ self.tmp_tg_msg = await self.tg.telegram_client.send_file(self.tmp_telegram_id, file_path, caption=caption)
+ reply = True
+ except:
+ reply = ('Error uploading',)
+ else: # HELP.brief or HELP.desc (first line)
+ reply = (' !upl Upload a file to current channel/chat',)
+ if help == HELP.desc: # rest of HELP.desc
+ reply += \
+ (
+ ' !upl <file name> [<optional caption>]',
+ 'Upload the file referenced by <file name> to current ',
+ 'channel/chat, the file must be present in "upload"',
+ 'local directory.'
+ )
+ return reply
diff -rN -u old-irgramd/irgramd new-irgramd/irgramd
--- old-irgramd/irgramd 2024-11-22 10:17:32.411437170 +0100
+++ new-irgramd/irgramd 2024-11-22 10:17:32.411437170 +0100
@@ -110,6 +110,7 @@
tornado.options.define('tls', default=False, help='Use TLS/SSL encrypted connection for IRC server')
tornado.options.define('tls_cert', default=None, metavar='CERTFILE', help='IRC server certificate chain for TLS/SSL, also can contain private key if not defined with `tls_key`')
tornado.options.define('tls_key', default=None, metavar='KEYFILE', help='IRC server private key for TLS/SSL')
+ tornado.options.define('upload_dir', default=None, metavar='PATH', help='Directory where files to upload are picked up, default "upload" in `cache_dir`')
try:
# parse cmd line first time to get --config and --config_dir
tornado.options.parse_command_line()
diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py
--- old-irgramd/telegram.py 2024-11-22 10:17:32.411437170 +0100
+++ new-irgramd/telegram.py 2024-11-22 10:17:32.415437163 +0100
@@ -43,6 +43,7 @@
self.notice_size = settings['download_notice'] * 1048576
self.media_dir = settings['media_dir']
self.media_url = settings['media_url']
+ self.upload_dir = settings['upload_dir']
self.api_id = settings['api_id']
self.api_hash = settings['api_hash']
self.phone = settings['phone']
@@ -77,6 +78,11 @@
if not os.path.exists(self.telegram_media_dir):
os.makedirs(self.telegram_media_dir)
+ # Setup upload folder
+ self.telegram_upload_dir = os.path.expanduser(self.upload_dir or os.path.join(self.cache_dir, 'upload'))
+ if not os.path.exists(self.telegram_upload_dir):
+ os.makedirs(self.telegram_upload_dir)
+
# Setup session folder
self.telegram_session_dir = os.path.join(self.config_dir, 'session')
if not os.path.exists(self.telegram_session_dir):