Skip to content

Commit 7849600

Browse files
seLainattzonko
authored andcommitted
Enable bot to ignore messages from specified senders (attzonko#29)
* fix incorrect test 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. * test for 1 to 1 function-regexp mapping append test to ensure the latest loaded function replaces previous loaded function which listening to the same regexp statement. * unit test for ignoring senders specified in settings.IGNORE_USERS * enable dispatcher to ignore messages from specified senders * move get_sender as static method
1 parent 1d5ecc8 commit 7849600

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

docs/settings.rst

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Settings
2424
2525
# Ignore broadcast message
2626
IGNORE_NOTIFIES = ['@channel', '@all']
27+
# Ignore message from specified senders (case-insensitive), ex: IGNORE_USERS = ['john', 'Mary']
28+
IGNORE_USERS = []
2729
2830
# Threads num
2931
WORKERS_NUM = 10

mmpy_bot/dispatcher.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ def start(self):
3535
def get_message(msg):
3636
return msg.get('data', {}).get('post', {}).get('message', '').strip()
3737

38+
@staticmethod
39+
def get_sender(msg):
40+
return msg.get('data', {}).get('sender_name', '').strip()
41+
3842
def ignore(self, _msg):
43+
return self._ignore_notifies(_msg) or self._ignore_sender(_msg)
44+
45+
def _ignore_notifies(self, _msg):
46+
# ignore message containing specified item, such as "@all"
3947
msg = self.get_message(_msg)
40-
if any(item in msg for item in settings.IGNORE_NOTIFIES):
41-
return True
48+
return True if any(item in msg for item in settings.IGNORE_NOTIFIES) else False
49+
50+
def _ignore_sender(self, _msg):
51+
# ignore message from senders specified in settings
52+
sender_name = self.get_sender(_msg)
53+
return True if sender_name.lower() in (name.lower() for name in settings.IGNORE_USERS) else False
4254

4355
def is_mentioned(self, msg):
4456
mentions = msg.get('data', {}).get('mentions', [])

mmpy_bot/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
SSL_VERIFY = True
1919

2020
IGNORE_NOTIFIES = ['@here', '@channel', '@all']
21+
IGNORE_USERS = []
2122
WORKERS_NUM = 10
2223

2324
DEFAULT_REPLY_MODULE = None
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
from mmpy_bot.dispatcher import MessageDispatcher
3+
from mmpy_bot import settings
4+
5+
#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
6+
logger = logging.getLogger(__name__)
7+
8+
def test_messagedispatcher__ignore_sender():
9+
dispatcher = MessageDispatcher(None, None)
10+
event = {'event': 'posted', 'data': {'sender_name': 'betty'}}
11+
event2 = {'event': 'posted', 'data': {'sender_name': 'Carter'}}
12+
# test by lowercase / uppercase settings
13+
# set as 'Betty'
14+
settings.IGNORE_USERS = [] # clean up
15+
settings.IGNORE_USERS.append('Betty')
16+
if dispatcher._ignore_sender(event) is not True:
17+
raise AssertionError()
18+
# set as 'CARTER'
19+
settings.IGNORE_USERS.append('CARTER')
20+
if dispatcher._ignore_sender(event2) is not True:
21+
raise AssertionError()

0 commit comments

Comments
 (0)