Giter Site home page Giter Site logo

ownbot's Introduction

ownbot

Build Status Coverage Status PyPI version

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!

Install

pip install ownbot

Get Started

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.

How It Works

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?

Storage

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.

Admin Commands

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.

ownbot's People

Contributors

michaelimfeld avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ownbot's Issues

Custom users.yml / add by ID

Hello, first of all great project!

I have two questions/suggestions:

Is it possible to use a custom users.yml? Like /bot/bot1/users.yml.

And is it possible to add a user by his ID? For users without a telegram username.

Rewrite user manager

The user manager needs a clean rewrite. I really want to get rid of all those checks if a specific key does exists in the config dict.

Error on security log warning

There is a log warning for the seconde allowed group on the decorator. The command run without a problem, but puts a erroneous log entry

@requires_usergroup("users", "otherusers")

log example

2016-05-07 17:23:39,209 - __main__ - INFO - COMMAND acionado por username -- 2016-05-07 17:23:39.209001
2016-05-07 17:23:39,284 - ownbot.auth - WARNING - The user '@username' with id 'IDNUMBER' tried to execute the protected command '/COMMAND '!

Change parse_type to HTML

Hi! I've noticed, that users with _ character, crashes /users command. It happens, cause users list sends with Markdown parse mode, and user nickname with single _ char (unclosed tag) leads to the exception:

Bad Request: can't parse message text: Can't find end of the entity starting at byte offset ...

I guess changing parse mode to HTML will fix this problem. Can I send PR to you?

ERROR - IndexError: tuple index out of range

Whenever I run /start or one of the admin commands, I get the following error.

2019-06-24 09:25:02,591 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "C:\Users\benbridges\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telegram\ext\dispatcher.py", line 333, in process_update
    handler.handle_update(update, self, check, context)
  File "C:\Users\benbridges\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telegram\ext\conversationhandler.py", line 326, in handle_update
    new_state = handler.handle_update(update, dispatcher, check_result, context)
  File "C:\Users\benbridges\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telegram\ext\handler.py", line 117, in handle_update
    return self.callback(update, context)
  File "C:\Users\benbridges\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ownbot\auth.py", line 80, in call
    update = args[1 + offset]
IndexError: tuple index out of range

I'm not sure what I'm doing wrong or if it's caused by the fact I'm running on windows or beta 12.0.0b1 of python-telegram-bot

Complete code here: https://pastebin.com/76pEHnBZ

Possible to turn off the default implemented commands?

I mean, it's good, but I just simply don't want them, nor I want to fork(bit lazy recently). Is it possible to configure this project a bit, so that the default commands like /adminhelp and /adduser are inaccessible to anyone, including the owner/whatsoever-admins?

Feature request: Allow two different groups access the same command

Sometimes we have two or more groups, where we want to share permissions for a specify command

for example:
command /help and we have group1 and group2.

group1 has access for many command on bot, but we want the ppl on group2 has acess to some of this commands too, without adding them to group.

ImportError: No module named auth

I tried to run the sampel code, and it raised an error:

  File "ownbot2.py", line 11, in <module>
    from ownbot.auth import requires_usergroup, assign_first_to
  File "/home/xxx/ownbot2.py", line 11, in <module>
ImportError: No module named auth

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.