From 9e2cb78d470c24b0b82d7079b0c659aca41cf73d Mon Sep 17 00:00:00 2001 From: c1tas Date: Thu, 13 Sep 2018 15:21:30 +0800 Subject: [PATCH 1/4] add support for directly send msg to room; example in README --- README.md | 14 ++++++++ zulip.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 108 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d3f37c7..5b1ba87 100644 --- a/README.md +++ b/README.md @@ -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" +) +``` diff --git a/zulip.py b/zulip.py index 8c07d4b..e551702 100644 --- a/zulip.py +++ b/zulip.py @@ -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] == "#": + 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) + 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) From 7213b4ed6c1dfe83df6eb2afd4111200b44d8d30 Mon Sep 17 00:00:00 2001 From: c1tas Date: Sat, 15 Sep 2018 16:28:55 +0800 Subject: [PATCH 2/4] add reason for why need errbot directly send msg to zulip --- README.md | 36 ++++++++++++++++++++++++++++++------ zulip.py | 23 ++++++++++++----------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5b1ba87..77f5745 100644 --- a/README.md +++ b/README.md @@ -48,16 +48,40 @@ Tips * Rooms in ErrBot are streams in Zulip. -#### directly send msg to room +Directly send message to room +---- +when a time you need errbot work like api + +* Example Code `#{{stream}}*{{topic}}` ```python self.send( self.build_identifier("#{{code_runtime_alert}}*{{hello}}"), - "test" -) -self.send( - self.build_identifier("#{{code_runtime_alert}}*{{hello}}"), - "test2" + "your message here" ) ``` + +* A demo errbot api for log your code exception in a specific zulip room + +``` python +@webhook +def cra(self, request): + self.log.debug(repr(request)) + if type(request) is dict: + # self.log.info(request) + if request['type'] == 'exception': + response = tenv().get_template('exception.md').render(request) + # self.log.info(response) + self.send( + self.build_identifier("#{{{{{stream}}}}}*{{{{{topic}}}}}".format( + stream=request['stream'], + topic=request['topic'] + )), + response + ) + return "{'status': '100'}" + else: + return "{'status': '999'}" + +``` diff --git a/zulip.py b/zulip.py index e551702..e15c794 100644 --- a/zulip.py +++ b/zulip.py @@ -260,7 +260,6 @@ def _handle_message(self, message): def send_message(self, msg): super().send_message(msg) - # import pdb ; pdb.set_trace() msg_data = { 'content': msg.body, } @@ -305,11 +304,13 @@ def check_email(email): def extract_identifiers_from_string(self, text): """ - + extract username streamname topicname from text + :param text: + :return username streamname topicname: """ exception_message = ( - 'Unparseable zulip identifier, should be of the format `#{{stream}}*{{topic}}`, `<@U12345>`, ' + 'Unparseable zulip identifier, should be of the format `#{{stream}}*{{topic}}`,' '`@U12345`. (Got `%s`)' ) text = text.strip() @@ -318,9 +319,7 @@ def extract_identifiers_from_string(self, text): raise ValueError(exception_message % '') username = None - userid = None streamname = None - streamid = None topicname = None regex_str_stream = r'#{{(.*)}}$' @@ -359,8 +358,9 @@ def extract_identifiers_from_string(self, text): if re_res: if len(re_res.groups()) > 0: username = re_res[1] + else: + raise ValueError(exception_message % text) - # import pdb ; pdb.set_trace() return username, streamname, topicname def build_identifier(self, txtrep): @@ -369,11 +369,12 @@ def build_identifier(self, txtrep): username, streamname, topicname = self.extract_identifiers_from_string(txtrep) if username: - - return ZulipPerson(id=txtrep, - full_name=txtrep, - emails=[txtrep], - client=self.client) + return ZulipPerson( + id=txtrep, + full_name=txtrep, + emails=[txtrep], + client=self.client + ) if streamname and not topicname: return ZulipRoom( id=streamname, From 15e47ea11efe8c2f8a2bc6e98db8b220bf103b8a Mon Sep 17 00:00:00 2001 From: c1tas Date: Wed, 26 Sep 2018 11:24:27 +0800 Subject: [PATCH 3/4] fix build email --- zulip.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/zulip.py b/zulip.py index e15c794..db06f39 100644 --- a/zulip.py +++ b/zulip.py @@ -366,6 +366,13 @@ def extract_identifiers_from_string(self, text): def build_identifier(self, txtrep): log.debug('building an identifier from %s.', txtrep) + if self.check_email(txtrep): + return ZulipPerson( + id=txtrep, + full_name=txtrep, + emails=[txtrep], + client=self.client + ) username, streamname, topicname = self.extract_identifiers_from_string(txtrep) if username: From 70d599c4166f29a44d8e773397a11deb2c500875 Mon Sep 17 00:00:00 2001 From: c1tas Date: Wed, 26 Sep 2018 15:14:00 +0800 Subject: [PATCH 4/4] fix build with botadmin --- zulip.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/zulip.py b/zulip.py index db06f39..53263b8 100644 --- a/zulip.py +++ b/zulip.py @@ -354,10 +354,13 @@ def extract_identifiers_from_string(self, text): 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] + if text[1] != '{': + username= text + else: + re_res = regex_user.search(text) + if re_res: + if len(re_res.groups()) > 0: + username = re_res[1] else: raise ValueError(exception_message % text)