Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable bot to ignore messages from specified senders #29

Merged
merged 6 commits into from
Jul 17, 2018

Conversation

seLain
Copy link
Collaborator

@seLain seLain commented Jul 14, 2018

Enhancement

Now we can have IGNORE_USERS in settings.py. All the sender names specified in IGNORE_USERS will be ignored by bots. The sender names can be set case-insensitively.

Usage

Developers can set IGNORE_USERS in your mmpy_bot_settings.py, and it will be loaded into mmpy_bot settings.

Example

In mmpy_bot_settings.py, we set:

IGNORE_USERS = ['betty', 'CARTER']

Then messages from Betty and Carter will be ignored by the bots.

Notice

If you have multiple bots and expecting them to ignore different sets of senders, you have to organize bots in different folders with their own mmpy_bot_settings.py. And execute them independently. For example :

my_bots /
    bot_1 /
        run_bot1.py
        mmpy_bot_settings.py
    bot_2/
        run_bot2.py
        mmpy_bot_settings.py

seLain added 5 commits July 12, 2018 01:09
If there are two plugin functions listening to the same regex pattern, only the latest funciton will be loaded in plugin manager. Details can be found in bot.listen_to(). So there sould not be test for loading two plugin functions listening to the same regex pattern.
append test to ensure the latest loaded function replaces previous loaded function which listening to the same regexp statement.
@seLain seLain added the enhancement New feature or request label Jul 14, 2018
@attzonko
Copy link
Owner

@seLain Do you want to make _ignore_sender a @classmethod to keep Codacy happy. I can do that if you want.

@seLain
Copy link
Collaborator Author

seLain commented Jul 17, 2018

I moved the code to get sender as a static method to fix this.

@staticmethod
def get_sender(msg):
    return msg.get('data', {}).get('sender_name', '').strip()

def _ignore_sender(self, _msg):
        # ignore message from senders specified in settings
        sender_name = self.get_sender(_msg)
        return True if sender_name.lower() in (name.lower() for name in settings.IGNORE_USERS) else False

It's the same as MessageDispatcher._ignore_notifies(self, _msg) and MessageDispatcher.get_message(msg).

I didn't do this in previous commits because MessageDispatcher.get_message really bothers me. It does almost the same thing as Message.get_message. So I tried not to add another MessageDispatcher.get_sender.

Maybe later we can find a way refactoring it by moving MessageDispatcher.get_message and MessageDispatcher.get_sender to Message ?

@attzonko attzonko merged commit 7849600 into attzonko:master Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants