summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rwxr-xr-xinverter-bot33
2 files changed, 34 insertions, 2 deletions
diff --git a/README.md b/README.md
index 7a3e6e9..dac7747 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,7 @@ The bot accepts following parameters:
to use the bot (required)
* ``--inverterd-host`` (default is `127.0.0.1`)
* ``--inverterd-port`` (default is `8305`)
+* ``--shell-admin``
## Launching with systemd
@@ -61,4 +62,4 @@ systemctl start inverter-bot
## License
-MIT \ No newline at end of file
+MIT
diff --git a/inverter-bot b/inverter-bot
index edae9a5..4dd36aa 100755
--- a/inverter-bot
+++ b/inverter-bot
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-import logging, re, datetime, json
+import logging, re, datetime, json, subprocess, os
from inverterd import Format, Client as InverterClient, InverterError
from typing import Optional
@@ -134,6 +134,30 @@ def msg_status(update: Update, context: CallbackContext) -> None:
handle_exc(update, e)
+def msg_shell(update: Update, context: CallbackContext) -> None:
+ try:
+ argv = re.findall('^shell (.*)$', update.message.text)[0].split()
+ result = subprocess.run(argv, capture_output=True)
+ if result.returncode != 0:
+ raise ChildProcessError('spawned process returned ' + str(result.returncode))
+
+ buf = '[stdout] ' + result.stdout.decode('utf-8')
+ buf += '\n[stderr] ' + result.stderr.decode('utf-8')
+ reply(update, escape(buf))
+ except Exception as e:
+ logging.exception(str(e))
+ reply(update, 'exception: ' + escape(str(e)))
+
+
+def msg_spawn(update: Update, context: CallbackContext) -> None:
+ try:
+ argv = re.findall('^spawn (.*)$', update.message.text)[0].split()
+ os.spawnlp(os.P_NOWAIT, argv[0], *argv)
+ except Exception as e:
+ logging.exception(str(e))
+ reply(update, 'exception: ' + escape(str(e)))
+
+
def msg_generation(update: Update, context: CallbackContext) -> None:
try:
today = datetime.date.today()
@@ -210,6 +234,7 @@ if __name__ == '__main__':
help='ID of users allowed to use the bot')
parser.add_argument('--inverterd-host', default='127.0.0.1', type=str)
parser.add_argument('--inverterd-port', default=8305, type=int)
+ parser.add_argument('--shell-admin', required=True, type=int)
args = parser.parse_args()
whitelist = list(map(lambda x: int(x), args.users_whitelist))
@@ -226,15 +251,21 @@ if __name__ == '__main__':
dispatcher = updater.dispatcher
user_filter = Filters.user(whitelist)
+ shell_admin_filter = Filters.user((args.shell_admin, ))
dispatcher.add_handler(CommandHandler('start', start))
+ dispatcher.add_handler(MessageHandler(Filters.regex(r'^shell ') & shell_admin_filter, msg_shell))
+ dispatcher.add_handler(MessageHandler(Filters.regex(r'^spawn ') & shell_admin_filter, msg_spawn))
+
dispatcher.add_handler(MessageHandler(Filters.text(_('status')) & user_filter, msg_status))
dispatcher.add_handler(MessageHandler(Filters.text(_('generation')) & user_filter, msg_generation))
dispatcher.add_handler(MessageHandler(Filters.text(_('gs')) & user_filter, msg_gs))
dispatcher.add_handler(MessageHandler(Filters.text(_('ri')) & user_filter, msg_ri))
dispatcher.add_handler(MessageHandler(Filters.text(_('errors')) & user_filter, msg_errors))
+
dispatcher.add_handler(MessageHandler(Filters.all & user_filter, msg_all))
+
# start the bot
updater.start_polling()