Skip to content

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

Closed
wants to merge 4 commits into from

Conversation

neiljp
Copy link
Contributor

@neiljp neiljp commented Aug 12, 2017

This aims to implement #55.

These commits:

  • Modify lib.py to intercept empty messages, if the bot class does not explicitly define ACCEPT_EMPTY_MESSAGES (and evaluates to True)
  • Amend a local doc to mention this optional element of bot classes
  • Amend the wikipedia bot, as a trial bot.

Outstanding points regarding this level of implementation:

  • Other than the wikipedia bot, all other bots respond to empty messages via lib.py, since they've not all been updated.
  • All the tests pass! This is since the tests just test the bot code, and so aren't aware of this interception.
  • Is this document the accurate bot document? What about docs/bot-guide.md in the main repo?
  • Is ACCEPT_EMPTY_MESSAGES a typical name/format to use in this case?

Copy link
Collaborator

@roberthoenig roberthoenig left a 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.
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@@ -18,6 +18,8 @@

from zulip import Client

default_empty_message_response = "Oops. Your message was empty."
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

try:
handle_empty_here = not (lib_module.handler_class.ACCEPT_EMPTY_MESSAGES)
except AttributeError:
handle_empty_here = True
Copy link
Collaborator

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)

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me 👍

@@ -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:
Copy link
Collaborator

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

# explicitly request to do so
if len(message['content']) == 0 and handle_empty_here:
restricted_client.send_reply(message, default_empty_message_response)
return
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

@neiljp
Copy link
Contributor Author

neiljp commented Aug 15, 2017

Pushed some changes. Last commit is also in separate PR #66, which might be easier place to discuss.

Thanks for the review @derAnfaenger 👍

neiljp added 4 commits August 15, 2017 11:39
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.
@roberthoenig
Copy link
Collaborator

roberthoenig commented Aug 16, 2017

@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.

@neiljp
Copy link
Contributor Author

neiljp commented Aug 23, 2017

As per the comment by @derAnfaenger, this is a backup PR which was the origin of the work now in #66, in case #66 is considered too extensive. I'll update this when a decision is made wrt that.

@zulipbot
Copy link
Member

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 upstream/master branch and resolve your pull request's merge conflicts accordingly.

@@ -18,6 +18,8 @@ class WikipediaHandler(object):
kind of external issue tracker as well.
'''

ACCEPT_EMPTY_MESSAGES = 1
Copy link
Contributor

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?

@zulipbot
Copy link
Member

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 upstream/master branch and resolve your pull request's merge conflicts accordingly.

@zulipbot
Copy link
Member

zulipbot commented Dec 9, 2017

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!

@neiljp
Copy link
Contributor Author

neiljp commented Dec 10, 2017

Superseded by #225, so closing this previous alternative implementation.

@neiljp neiljp closed this Dec 10, 2017
@zulipbot zulipbot removed the reviewed label Dec 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants