├── .gitignore ├── LICENSE ├── README.md ├── bot.ini └── bot.py /.gitignore: -------------------------------------------------------------------------------- 1 | bot.ini 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Barend 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sudobot 2 | 3 | With sudobot, you can run commands on your server from Telegram. *It's fast and simple, but might not be secure*. 4 | 5 | To get started, edit the `bot.ini` file with your favourite text editor to use your bot token and user ID (not username). 6 | 7 | You'll also need the latest version of python-telegram-bot installed: 8 | 9 | pip3 install python-telegram-bot 10 | 11 | Then run the bot with `python3 bot.py`. 12 | -------------------------------------------------------------------------------- /bot.ini: -------------------------------------------------------------------------------- 1 | [KEYS] 2 | bot_api = 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 3 | [ADMIN] 4 | id = 12345678 5 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import configparser 4 | from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler 5 | from telegram import InlineQueryResultArticle, ChatAction, InputTextMessageContent 6 | from uuid import uuid4 7 | import subprocess 8 | import time 9 | import logging 10 | 11 | 12 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 13 | level=logging.INFO) 14 | logger = logging.getLogger(__name__) 15 | 16 | 17 | def error(bot, update, error): 18 | logger.warn('Update "%s" caused error "%s"' % (update, error)) 19 | 20 | 21 | config = configparser.ConfigParser() 22 | config.read('bot.ini') 23 | 24 | updater = Updater(token=config['KEYS']['bot_api']) 25 | dispatcher = updater.dispatcher 26 | 27 | 28 | def start(bot, update): 29 | bot.sendChatAction(chat_id=update.message.chat_id, 30 | action=ChatAction.TYPING) 31 | bot.sendMessage(chat_id=update.message.chat_id, text="Hi. I'm sudobot.") 32 | if update.message.from_user.id != int(config['ADMIN']['id']): 33 | bot.sendChatAction(chat_id=update.message.chat_id, 34 | action=ChatAction.TYPING) 35 | time.sleep(1) 36 | bot.sendMessage(chat_id=update.message.chat_id, 37 | text="It seems like you aren't allowed to use me. :(") 38 | bot.sendChatAction(chat_id=update.message.chat_id, 39 | action=ChatAction.TYPING) 40 | time.sleep(1.5) 41 | bot.sendMessage(chat_id=update.message.chat_id, 42 | text="But sudobot is open source software, which means you can have your own! See my [GitHub repo](https://github.com/bvanrijn/sudobot) for details.", parse_mode="Markdown") 43 | else: 44 | bot.sendChatAction(chat_id=update.message.chat_id, 45 | action=ChatAction.TYPING) 46 | time.sleep(1) 47 | bot.sendMessage(chat_id=update.message.chat_id, 48 | text="You can use me to run commands on your computer or server.") 49 | bot.sendChatAction(chat_id=update.message.chat_id, 50 | action=ChatAction.TYPING) 51 | time.sleep(1.5) 52 | bot.sendMessage(chat_id=update.message.chat_id, 53 | text="Note that interactive commands or commands that generate a lot of output won't work.") 54 | bot.sendChatAction(chat_id=update.message.chat_id, 55 | action=ChatAction.TYPING) 56 | time.sleep(1) 57 | bot.sendMessage(chat_id=update.message.chat_id, text="Have fun!") 58 | bot.sendChatAction(chat_id=update.message.chat_id, 59 | action=ChatAction.TYPING) 60 | time.sleep(1.5) 61 | bot.sendMessage(chat_id=update.message.chat_id, 62 | text="Oh, before I forget: sudobot is open source software. See my [GitHub repo](https://github.com/bvanrijn/sudobot).", parse_mode="Markdown") 63 | 64 | 65 | def execute(bot, update, direct=True): 66 | 67 | try: 68 | user_id = update.message.from_user.id 69 | command = update.message.text 70 | inline = False 71 | except AttributeError: 72 | # Using inline 73 | user_id = update.inline_query.from_user.id 74 | command = update.inline_query.query 75 | inline = True 76 | 77 | if user_id == int(config['ADMIN']['id']): 78 | if not inline: 79 | bot.sendChatAction(chat_id=update.message.chat_id, 80 | action=ChatAction.TYPING) 81 | output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 82 | output = output.stdout.read().decode('utf-8') 83 | output = '`{0}`'.format(output) 84 | 85 | if not inline: 86 | bot.sendMessage(chat_id=update.message.chat_id, 87 | text=output, parse_mode="Markdown") 88 | return False 89 | 90 | if inline: 91 | return output 92 | 93 | 94 | def inlinequery(bot, update): 95 | query = update.inline_query.query 96 | o = execute(query, update, direct=False) 97 | results = list() 98 | 99 | results.append(InlineQueryResultArticle(id=uuid4(), 100 | title=query, 101 | description=o, 102 | input_message_content=InputTextMessageContent( 103 | '*{0}*\n\n{1}'.format(query, o), 104 | parse_mode="Markdown"))) 105 | 106 | bot.answerInlineQuery(update.inline_query.id, results=results, cache_time=10) 107 | 108 | 109 | start_handler = CommandHandler('start', start) 110 | execute_handler = MessageHandler([Filters.text], execute) 111 | 112 | dispatcher.add_handler(start_handler) 113 | dispatcher.add_handler(execute_handler) 114 | dispatcher.add_handler(InlineQueryHandler(inlinequery)) 115 | 116 | dispatcher.add_error_handler(error) 117 | 118 | updater.start_polling() 119 | updater.idle() 120 | --------------------------------------------------------------------------------