-
Notifications
You must be signed in to change notification settings - Fork 15
add support for directly send msg to room; example in README #10
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -47,3 +47,17 @@ Tips | |
| ---- | ||
|
|
||
| * Rooms in ErrBot are streams in Zulip. | ||
|
|
||
| #### directly send msg to room | ||
|
|
||
| `#{{stream}}*{{topic}}` | ||
| ```python | ||
| self.send( | ||
| self.build_identifier("#{{code_runtime_alert}}*{{hello}}"), | ||
| "test" | ||
| ) | ||
|
||
| self.send( | ||
| self.build_identifier("#{{code_runtime_alert}}*{{hello}}"), | ||
| "test2" | ||
| ) | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from errbot.rendering.ansiext import enable_format, TEXT_CHRS | ||
|
|
||
| from urllib.parse import quote | ||
| import re | ||
|
|
||
| # Can't use __name__ because of Yapsy. | ||
| log = logging.getLogger('errbot.backends.zulip') | ||
|
|
@@ -259,6 +260,7 @@ def _handle_message(self, message): | |
|
|
||
| def send_message(self, msg): | ||
| super().send_message(msg) | ||
| # import pdb ; pdb.set_trace() | ||
|
||
| msg_data = { | ||
| 'content': msg.body, | ||
| } | ||
|
|
@@ -293,11 +295,99 @@ def change_presence(self, status: str = ONLINE, message: str = '') -> None: | |
| # At this time, Zulip doesn't support active presence change. | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def check_email(email): | ||
| regex_email = re.compile(r'^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?))$') | ||
| if regex_email.match(email) != None: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
| def extract_identifiers_from_string(self, text): | ||
| """ | ||
|
|
||
|
||
| """ | ||
|
|
||
| exception_message = ( | ||
| 'Unparseable zulip identifier, should be of the format `#{{stream}}*{{topic}}`, `<@U12345>`, ' | ||
| '`@U12345`. (Got `%s`)' | ||
| ) | ||
| text = text.strip() | ||
|
|
||
| if text == '': | ||
| raise ValueError(exception_message % '') | ||
|
|
||
| username = None | ||
| userid = None | ||
| streamname = None | ||
| streamid = None | ||
| topicname = None | ||
|
|
||
| regex_str_stream = r'#{{(.*)}}$' | ||
| regex_str_topic = r'\*{{(.*)}}$' | ||
| regex_str_user = r'@{{(.*)}}$' | ||
|
|
||
| regex_stream = re.compile(regex_str_stream) | ||
| regex_topic = re.compile(regex_str_topic) | ||
| regex_user = re.compile(regex_str_user) | ||
|
|
||
| if self.check_email(text): | ||
| username = text | ||
|
|
||
| if text[0] == "#": | ||
|
Collaborator
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. The logic in this if-statement is rather complicated. Perhaps there is a simpler way to parse this? Maybe something that involves extracting every occurrence of
Author
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. sry for my poor regex skill, if you have some cool idea, just instead of using my poor code |
||
| if '*{{' in text: | ||
| tmp_text = text.split('*{{') | ||
| try: | ||
| tmp_text[1] = '*{{' + tmp_text[1] | ||
| except Exception as e: | ||
| pass | ||
| re_res = regex_stream.search(tmp_text[0]) | ||
| if re_res: | ||
| if len(re_res.groups()) > 0: | ||
| streamname = re_res[1] | ||
| re_res = regex_topic.search(tmp_text[1]) | ||
| if re_res: | ||
| if len(re_res.groups()) > 0: | ||
| topicname = re_res[1] | ||
| else: | ||
| re_res = regex_stream.search(text) | ||
| if re_res: | ||
| if len(re_res.groups()) > 0: | ||
| streamname = re_res[1] | ||
| elif text[0] == '@': | ||
| re_res = regex_user.search(text) | ||
| if re_res: | ||
| if len(re_res.groups()) > 0: | ||
| username = re_res[1] | ||
|
|
||
| # import pdb ; pdb.set_trace() | ||
|
||
| return username, streamname, topicname | ||
|
|
||
| def build_identifier(self, txtrep): | ||
| return ZulipPerson(id=txtrep, | ||
| full_name=txtrep, | ||
| emails=[txtrep], | ||
| client=self.client) | ||
|
|
||
| log.debug('building an identifier from %s.', txtrep) | ||
|
Collaborator
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. Another debugging artifact.
Collaborator
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. (Or do other backends actually log something here?) |
||
| username, streamname, topicname = self.extract_identifiers_from_string(txtrep) | ||
|
|
||
| if username: | ||
|
|
||
|
||
| return ZulipPerson(id=txtrep, | ||
| full_name=txtrep, | ||
| emails=[txtrep], | ||
| client=self.client) | ||
| if streamname and not topicname: | ||
| return ZulipRoom( | ||
| id=streamname, | ||
| title=streamname, | ||
| subject='', | ||
| client=self.client, | ||
| ) | ||
| elif streamname and topicname: | ||
| return ZulipRoom( | ||
| id=streamname, | ||
| title=streamname, | ||
| subject=topicname, | ||
| client=self.client, | ||
| ) | ||
|
|
||
| def build_reply(self, msg, text=None, private=False, threaded=False): | ||
| response = self.build_message(text) | ||
|
|
||
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.
In user-facing documentation, words like "message" shouldn't be abbreviated. It'd also be nice to have a paragraph or two explaining what goes into the double brackets, and when you would want to "directly send msg to room"