Skip to content

Commit

Permalink
Merge pull request #5 from Geek-MD/develop
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
Geek-MD authored Mar 27, 2022
2 parents f46b595 + e5d8291 commit 3360f16
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ SmartHomeBot
If you want the bot to run at startup, or advanced configuration, check the [Wiki](https://github.com/Geek-MD/SmartHomeBot/wiki).

## Roadmap
- [x] Basic functionality, only */start* and */help* commands. [`v0.1.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.1.0)
- [X] Basic functionality, only */start* and */help* commands. [`v0.1.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.1.0)
- [X] Add a list of allowed users who can interact with the bot. [`v0.2.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.2.0)
- [X] Add a list of admin users who can run admin restricted commands. [`v0.3.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.3.0)
- [X] Fixed a bug with admin restricted commands. [`v0.3.1`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.3.1)
- [ ] Add */reboot* command, restricted to admin users.
- [ ] Add confirmation buttons to execute */reboot* command.
- [X] Add */reboot* command, restricted to admin users. [`v0.4.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.4.0)
- [X] Add confirmation buttons to execute */reboot* command. [`v0.4.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.4.0)
- [ ] Critical data like Telegram bot token, allowed users list and admin users list are stored in external separate JSON files.
- [ ] Add */system* command so admins can check CPU temperature of server, CPU and RAM load.
- [ ] Add */listusers* command so any user can check allowed users list.
- [ ] Add */adduser* command so admins can add users to the allowed users list.
- [ ] Add */makeadmin* command so admins can upgrade a user to admin list.
- [ ] Add */status* command so admins can see a list of running Docker containers.
Expand Down
39 changes: 33 additions & 6 deletions smarthomebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
Use /help to list available commands.
"""

import logging
from telegram import Update, ForceReply, User
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import logging, os, time
from telegram import Update, User, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, CallbackQueryHandler

# bot variables
AUTH_TOKEN = "bot_token" # string
AUTH_TOKEN = "bot-token" # string
ALLOWED_USERS = [user_id_1, user_id_2] # integer
ADMIN_USERS = [user_id_1] # integer
ADMIN_USERS = [user_id_1] #integer
commands = ['/start', '/help', '/reboot']
admin_commands = ['/reboot']
reboot_keyboard = [[InlineKeyboardButton("yes", callback_data='y'), InlineKeyboardButton("no", callback_data='n')]]
reboot_keyboard_markup = InlineKeyboardMarkup(reboot_keyboard)
reboot_option = None

# multiline markup text used for /help command.
help_command_text = """This is a simple Telegram Bot used to automate notifications for a Smart Home\.
Expand All @@ -43,7 +46,28 @@ def help_command(update: Update, context: CallbackContext) -> None:
update.message.reply_markdown_v2(help_command_text)

def reboot_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Sorry, reboot command is dissabled for now.')
global reboot_option
button_pressed = False
update.message.reply_text('Reboot your system?', reply_markup=reboot_keyboard_markup)

def reboot_query(update: Update, context: CallbackContext) -> None:
global reboot_option
button_pressed = False
query = update.callback_query
query.answer()
reboot_option = query.data
while button_pressed == False:
if reboot_option == "y":
query.edit_message_text(text=f'Rebooting in 5 secs...')
time.sleep(5)
os.system("sudo reboot")
break
elif reboot_option == "n":
query.edit_message_text(text=f'Reboot aborted.')
button_pressed = True
break
elif reboot_option == None:
continue

def not_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Sorry, I can\'t understand that.')
Expand All @@ -62,6 +86,9 @@ def main() -> None:
# not allowed users can't interact with the bot.
updater.dispatcher.add_handler(MessageHandler(~Filters.user(ALLOWED_USERS), not_allowed_users))

# inline buttons.
updater.dispatcher.add_handler(CallbackQueryHandler(reboot_query))

# on non command i.e message, reply with not_command function.
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.text(commands), not_command))

Expand Down

0 comments on commit 3360f16

Please sign in to comment.