def _break_apart_message(bot, raw_message, m_body, m_to_me): m_bodies = [] was_split = False if 'files' not in raw_message or not m_to_me: m_targets, m_text_pieces = su.extract_targets( su.parse(raw_message.get("text", ''))) m_text = "".join(t.text for t in m_text_pieces) m_text_no_links = su.drop_links(m_text_pieces) tmp_m_body = m_body.copy() tmp_m_body.text = m_text.lstrip() tmp_m_body.text_no_links = m_text_no_links.lstrip() m_bodies.append(tmp_m_body) else: slack_client = bot.clients.slack_client for f in raw_message.get('files', []): # TODO: get some native api in the slack client library # so we can avoid doing this... just to get at a file that the # library should be able to fetch... resp = requests.get(f['url_private_download'], headers={ 'Authorization': 'Bearer %s' % slack_client.server.token, }, timeout=bot.config.slack.get('timeout')) resp.raise_for_status() resp_text = resp.text.strip() for line in resp_text.splitlines(): tmp_m_body = m_body.copy() tmp_m_body.text = line.strip() tmp_m_body.text_no_links = tmp_m_body.text m_bodies.append(tmp_m_body) was_split = True return m_bodies, was_split
def rewrite(self, text_aliases=None): if not text_aliases: return self message_raw_text = self.body.text message_text = self.body.text_no_links for tmp_message_text, may_have_links in [(message_text, True), (message_raw_text, False)]: try: tmp_message_text = text_aliases[tmp_message_text] except KeyError: pass else: new_me = self.copy() if may_have_links: tmp_message_text_pieces = su.parse(tmp_message_text) new_me.body.text = tmp_message_text new_me.body.text_no_links = su.drop_links( tmp_message_text_pieces) new_me.body.text_pieces = tmp_message_text_pieces else: new_me.body.text = tmp_message_text new_me.body.text_no_links = tmp_message_text new_me.body.text_pieces = su.parse(tmp_message_text) return new_me return self
def _cook_slack_text(text, replace_mrkdwn=True): def replace_bolding(m): return BOLD + m.group(1) + RESET text_pieces = su.parse(text) text = su.drop_links(text_pieces) text = _escape_caret(text) if replace_mrkdwn: text = re.sub(r"[*](.*?)[*]", replace_bolding, text, flags=re.MULTILINE) if isinstance(text, six.text_type): for u_ch, b_ch in UNICODE_REPLACEMENTS: text = text.replace(u_ch, b_ch) text = text.encode(UNICODE_TARGET_ENCODING, errors='replace') return text
def test_drop_links_no_label(self): text = "hello <http://google.com|google>" text_pieces = su.parse(text) text = su.drop_links(text_pieces, use_label=False) self.assertEqual("hello http://google.com", text)
def test_drop_links(self): text = "hello <http://google.com|google>" text_pieces = su.parse(text) text = su.drop_links(text_pieces) self.assertEqual("hello google", text)