-
Notifications
You must be signed in to change notification settings - Fork 397
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
Add bot-initiated messaging ability #95
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ slackbot_test_settings.py | |
/dist | ||
/*.egg-info | ||
.cache | ||
.idea | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from slackbot.manager import PluginsManager | ||
from slackbot.slackclient import SlackClient | ||
from slackbot.dispatcher import MessageDispatcher | ||
from slackbot.utils import optional_arg_decorator | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -65,6 +66,17 @@ def wrapper(func): | |
return wrapper | ||
|
||
|
||
# use optional_arg_decorator so users can either do @idle or @idle() | ||
@optional_arg_decorator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
def idle(func): | ||
"""Run a function once/second whenever no other actions were taken. | ||
The function must take one parameter, a SlackClient instance.""" | ||
# match anything, the text doesn't apply for "idle" | ||
PluginsManager.idle_commands.append(func) | ||
logger.info('registered idle plugin "%s"', func.__name__) | ||
return func | ||
|
||
|
||
# def default_reply(matchstr=r'^.*$', flags=0): | ||
def default_reply(*args, **kwargs): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,13 +135,43 @@ def filter_text(self, msg): | |
return msg | ||
|
||
def loop(self): | ||
# once/second, check events | ||
# run idle handlers whenever idle | ||
while True: | ||
events = self._client.rtm_read() | ||
for event in events: | ||
if event.get('type') != 'message': | ||
continue | ||
self._on_new_message(event) | ||
time.sleep(1) | ||
|
||
# run idle handlers as long as we've been idle | ||
for func in self._plugins.get_idle_plugins(): | ||
if not func: | ||
continue | ||
|
||
# if actions are pending, don't do anything | ||
if not self._pool.queue.empty(): | ||
break | ||
|
||
# if some action was taken, don't run the remaining handlers | ||
if self._client.idle_time() < 1: | ||
break | ||
|
||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the next few lines should be in loop() or elsewhere. Unlike regular messages, this would mean that idle functions are run in the loop thread. Other messages are handled in their own thread, being added to the pool. |
||
func(self._client) | ||
except: | ||
logger.exception( | ||
'idle handler failed with plugin "%s"', | ||
func.__name__) | ||
reply = u'[{}] I had a problem with idle handler\n'.format( | ||
func.__name__) | ||
tb = u'```\n{}\n```'.format(traceback.format_exc()) | ||
# no channel, so only send errors to error user | ||
if self._errors_to: | ||
self._client.rtm_send_message(self._errors_to, | ||
'{}\n{}'.format(reply, | ||
tb)) | ||
time.sleep(1.0) | ||
|
||
def _default_reply(self, msg): | ||
default_reply = settings.DEFAULT_REPLY | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,9 @@ def __init__(self): | |
commands = { | ||
'respond_to': {}, | ||
'listen_to': {}, | ||
'default_reply': {} | ||
'default_reply': {}, | ||
} | ||
idle_commands = [] | ||
|
||
def init_plugins(self): | ||
if hasattr(settings, 'PLUGINS'): | ||
|
@@ -72,3 +73,6 @@ def get_plugins(self, category, text): | |
|
||
if not has_matching_plugin: | ||
yield None, None | ||
|
||
def get_idle_plugins(self): | ||
yield from self.idle_commands | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @csaftoiu: Not compatible with Python2.7. Can be changed to the following - |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need
global last_bored
to write to the outer scoped var?