-
-
Notifications
You must be signed in to change notification settings - Fork 393
Handle empty messages in bot framework by default #58
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
Conversation
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.
Thanks for working on this @neiljp ! I've only looked into the first three commits, left a couple stylistic comments.
Hmm, because of the fourth commit, it show the three older ones as outdated. Maybe we try first to merge the first three, and then discuss the fourth one?
@@ -107,9 +107,11 @@ Each bot library simply needs to do the following: | |||
|
|||
- Define a class that supports the methods `usage` | |||
and `handle_message`. | |||
- Optionally add a class member `ACCEPT_EMPTY_MESSAGES`, to | |||
specify that empty messages will be passed to the bot. |
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.
It would be awesome if you could also add this to bots_guide.md
. The guide is quite outdated, but it will be the foundation for a major refactoring.
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.
If these commits go through, rather than the other, then I'll look into that; I wasn't sure if one was outdated, or both should be updated.
zulip_bots/zulip_bots/lib.py
Outdated
@@ -18,6 +18,8 @@ | |||
|
|||
from zulip import Client | |||
|
|||
default_empty_message_response = "Oops. Your message was empty." |
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.
Maybe we can plug that string directly in the function, since it only appears one time and your comment down below explains that it is used as a default empty message response.
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.
Sure; I wasn't sure if I'd end up with more strings further down the line.
zulip_bots/zulip_bots/lib.py
Outdated
try: | ||
handle_empty_here = not (lib_module.handler_class.ACCEPT_EMPTY_MESSAGES) | ||
except AttributeError: | ||
handle_empty_here = True |
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.
handle_empty = getattr(lib_module.handler_class, 'ACCEPT_EMPTY_MESSAGES', True)
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.
Nice catch of a pattern, but if this is changed, I think it probably should be
handle_empty = not getattr(lib_module.handler_class, 'ACCEPT_EMPTY_MESSAGES', False)
That is, if it gets the attr, then True there (bot handles it) implies False to handle_empty
(lib handles it); if not present then the result is the same as with yours.
RIght?
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.
Aah, you're right. Complicated boolean logic :) I'd add a comment explaining what's going on here.
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.
Maybe the exception approach is clearer here?
- Take inverse of the found variable
- ...or default to True
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.
I found both confusing, but I prefer gettr
just for the sake that it is more conventional.
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.
Fine by me 👍
zulip_bots/zulip_bots/lib.py
Outdated
@@ -197,6 +205,12 @@ def handle_message(message): | |||
if message['content'] is None: | |||
return | |||
|
|||
# Handle empty message directly if bot (handler_class) does not | |||
# explicitly request to do so | |||
if len(message['content']) == 0 and handle_empty_here: |
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.
if not message['content'] and handle_empty
zulip_bots/zulip_bots/lib.py
Outdated
# explicitly request to do so | ||
if len(message['content']) == 0 and handle_empty_here: | ||
restricted_client.send_reply(message, default_empty_message_response) | ||
return |
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.
I'm not sure, but wouldn't the return
here cause the whole script to stop?
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.
a) It doesn't seem to ;)
b) This return is from handle_message()
, which is called by the lib for each message;
c) A few lines above, it returns, and not for exceptional circumstances, which indicates it should be fine.
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.
All good points, somehow missed that it was in handle_message
.
Pushed some changes. Last commit is also in separate PR #66, which might be easier place to discuss. Thanks for the review @derAnfaenger 👍 |
Empty messages sent to the bot now trigger a response from within the bot library by default. To handle empty messages explicitly within a bot, a bot class must now define ACCEPT_EMPTY_MESSAGES such that it evaluates to True.
@neiljp I like your default commands PR, will give that one a look first, so this one might actually become superfluous :) If we are going to look furter into this PR here, the last commit should be removed. |
Heads up @neiljp, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the |
@@ -18,6 +18,8 @@ class WikipediaHandler(object): | |||
kind of external issue tracker as well. | |||
''' | |||
|
|||
ACCEPT_EMPTY_MESSAGES = 1 |
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.
This should be True
, correct?
Heads up @neiljp, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the |
Hello @neiljp, a Zulip maintainer reviewed this pull request over 10 days ago, but you haven't updated your pull request since. Please take a look at the requested changes and update your pull request accordingly. Thank you for your valuable contributions to Zulip! |
Superseded by #225, so closing this previous alternative implementation. |
This aims to implement #55.
These commits:
lib.py
to intercept empty messages, if the bot class does not explicitly defineACCEPT_EMPTY_MESSAGES
(and evaluates toTrue
)Outstanding points regarding this level of implementation:
lib.py
, since they've not all been updated.docs/bot-guide.md
in the main repo?ACCEPT_EMPTY_MESSAGES
a typical name/format to use in this case?