Easy to use python module to create private telegram bots using python-telegram-bot.
Ownbot provides some cool decorators to protect your command handler functions from unauthorized users!
pip install ownbot
Simply add the requires_usergroup
decorator to your handler function! That's it!
from ownbot.auth import requires_usergroup, assign_first_to
(...)
@assign_first_to("admin")
@requires_usergroup("user")
def start_handler(bot, update):
bot.sendMessage(chat_id=update.message.chat_id, text="Hello World")
- The
assign_first_to
decorator adds the first user who invokes the handler method to the specified group. - The second decorator
requires_usergroup
lets you define which usergroups will have permission to access the handler command.
Obviously admin
users have access to all protected commands. If a group passed to the requires_usergroup
decorator does not already exist, it will be created.
Ownbot saves new users added by Telegram username as unverified users. On first contact, when the user sends his first message to the bot, ownbot will store the user with his unique id as a verified user. A verified user will from now on always have access to his group even if he changes his username. The authorization checks are done only on the unique Telegram user_id
! Sounds good right?
For user/group storage ownbot uses a simple yaml file, which can be found in $HOMEDIR/.ownbot/users.yml
. This file can be edited manually, but it is recommended to use the AdminCommands
to add or remove users from groups.
The admin commands can be enabled by simply instantiating the AdminCommands
class and passing over the bot's dispatcher.
from ownbot.admincommands import AdminCommands
(...)
updater = Updater(TOKEN)
dispatcher = updater.dispatcher
AdminCommands(dispatcher)
If the admin commands are enabled, a user who is in the admin
group is able to perform the following actions:
Command | Arguments | Description |
---|---|---|
/adminhelp | - | Shows a list of available commands. |
/users | - | Shows a list of all registered users. |
/adduser | user group | Adds a user to a group. |
/rmuser | user group | Removes a user from a group. |