Skip to content

Commit

Permalink
Merge pull request #6 from Geek-MD/develop
Browse files Browse the repository at this point in the history
v0.5.0
  • Loading branch information
Geek-MD authored Mar 28, 2022
2 parents 3360f16 + 151ba19 commit 6500523
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ SmartHomeBot
## Basic Installation
This bot relies on [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot), so you have to install this package first with

`pip install python-telegram-bot`
```
pip install python-telegram-bot
```

Clone this repo with

`git clone https://github.com/Geek-MD/SmartHomeBot.git`
```
git clone https://github.com/Geek-MD/SmartHomeBot.git
```

Edit smarthomebot.py, add your bot Telegram token, id number of allowed and admin users, and you're done. Now run with

`python smarthomebot.py`
```
python smarthomebot.py
```

If you want the bot to run at startup, or advanced configuration, check the [Wiki](https://github.com/Geek-MD/SmartHomeBot/wiki).

Expand All @@ -32,7 +38,7 @@ SmartHomeBot
- [X] Fixed a bug with admin restricted commands. [`v0.3.1`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.3.1)
- [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.
- [X] Critical data like Telegram bot token, allowed users list and admin users list are stored in external separate JSON files. [`v0.5.0`](https://github.com/Geek-MD/SmartHomeBot/releases/tag/v0.5.0)
- [ ] 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.
Expand Down
1 change: 1 addition & 0 deletions admin_users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ADMINS":[user_id_1]}
1 change: 1 addition & 0 deletions allowed_users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"USERS":[user_id_1, user_id_2]}
1 change: 1 addition & 0 deletions smarthomebot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"AUTH_TOKEN" : "bot-token"}
22 changes: 13 additions & 9 deletions smarthomebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
Use /help to list available commands.
"""

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

# bot variables
AUTH_TOKEN = "bot-token" # string
ALLOWED_USERS = [user_id_1, user_id_2] # 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')]]
Expand Down Expand Up @@ -77,14 +73,22 @@ def not_admin(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Sorry, you\'re not an admin, you can\'t use admin restricted commands.')

def not_allowed_users(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Sorry you\'re not allowed to use this bot')
update.message.reply_text('Sorry you\'re not allowed to use this bot.')

def main() -> None:
# import data stored on external files.
with open('smarthomebot.json', 'r') as token:
auth_token = json.load(token)
with open('allowed_users.json', 'r') as users:
allowed_users = json.load(users)
with open('admin_users.json', 'r') as admins:
admin_users = json.load(admins)

# create the Updater and pass it your bot's token.
updater = Updater(AUTH_TOKEN)
updater = Updater(auth_token["AUTH_TOKEN"])

# not allowed users can't interact with the bot.
updater.dispatcher.add_handler(MessageHandler(~Filters.user(ALLOWED_USERS), not_allowed_users))
updater.dispatcher.add_handler(MessageHandler(~Filters.user(allowed_users["USERS"]), not_allowed_users))

# inline buttons.
updater.dispatcher.add_handler(CallbackQueryHandler(reboot_query))
Expand All @@ -93,7 +97,7 @@ def main() -> None:
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.text(commands), not_command))

# on admin command, run is_admin_command function.
updater.dispatcher.add_handler(MessageHandler(Filters.text(admin_commands) & ~Filters.user(ADMIN_USERS), not_admin))
updater.dispatcher.add_handler(MessageHandler(Filters.text(admin_commands) & ~Filters.user(admin_users["ADMINS"]), not_admin))

# commands.
updater.dispatcher.add_handler(CommandHandler("start", start_command))
Expand Down

0 comments on commit 6500523

Please sign in to comment.