def extract_body(message: message.Message, include_quotations: bool=False) -> str: import talon global talon_initialized if not talon_initialized: talon.init() talon_initialized = True # If the message contains a plaintext version of the body, use # that. plaintext_content = get_message_part_by_type(message, "text/plain") if plaintext_content: if include_quotations: return plaintext_content else: return talon.quotations.extract_from_plain(plaintext_content) # If we only have an HTML version, try to make that look nice. html_content = get_message_part_by_type(message, "text/html") if html_content: if include_quotations: return convert_html_to_markdown(html_content) else: return convert_html_to_markdown(talon.quotations.extract_from_html(html_content)) if plaintext_content is not None or html_content is not None: raise ZulipEmailForwardUserError("Email has no nonempty body sections; ignoring.") logging.warning("Content types: %s" % ([part.get_content_type() for part in message.walk()],)) raise ZulipEmailForwardUserError("Unable to find plaintext or HTML message body")
def extract_html_body(message: message.Message, include_quotes: bool=False) -> Optional[str]: import talon global talon_initialized if not talon_initialized: # nocoverage talon.init() talon_initialized = True html_content = get_message_part_by_type(message, "text/html") if html_content is not None: if include_quotes: return convert_html_to_markdown(html_content) else: return convert_html_to_markdown(talon.quotations.extract_from_html(html_content)) else: return None
def format_freshdesk_ticket_creation_message(ticket: TicketDict) -> str: """They send us the description as HTML.""" cleaned_description = convert_html_to_markdown(ticket.description) content = TICKET_CREATION_TEMPLATE.format(name=ticket.requester_name, email=ticket.requester_email, ticket_id=ticket.id, ticket_url=ticket.url, description=cleaned_description, type=ticket.type, priority=ticket.priority, status=ticket.status) return content
def format_freshdesk_ticket_creation_message(ticket: TicketDict) -> str: """They send us the description as HTML.""" cleaned_description = convert_html_to_markdown(ticket.description) content = "%s <%s> created [ticket #%s](%s):\n\n" % ( ticket.requester_name, ticket.requester_email, ticket.id, ticket.url) content += """~~~ quote %s ~~~\n """ % (cleaned_description,) content += "Type: **%s**\nPriority: **%s**\nStatus: **%s**" % ( ticket.type, ticket.priority, ticket.status) return content
def test_reply_is_extracted_from_html(self) -> None: # build dummy messages for stream # test valid incoming stream message is processed properly email = self.example_email('hamlet') self.login(email) user_profile = self.example_user('hamlet') self.subscribe(user_profile, "Denmark") stream = get_stream("Denmark", user_profile.realm) stream_to_address = encode_email_address(stream) html = """ <html> <body> <p>Reply</p> <blockquote> <div> On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote: </div> <div> Quote </div> </blockquote> </body> </html> """ incoming_valid_message = MIMEText(html, 'html') incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject' incoming_valid_message['From'] = self.example_email('hamlet') incoming_valid_message['To'] = stream_to_address incoming_valid_message['Reply-to'] = self.example_email('othello') process_message(incoming_valid_message) # Hamlet is subscribed to this stream so should see the email message from Othello. message = most_recent_message(user_profile) self.assertEqual(message.content, 'Reply') # Don't extract if Subject indicates the email has been forwarded into the mirror: del incoming_valid_message['Subject'] incoming_valid_message[ 'Subject'] = 'FWD: TestStreamEmailMessages Subject' process_message(incoming_valid_message) message = most_recent_message(user_profile) self.assertEqual(message.content, convert_html_to_markdown(html))
def format_freshdesk_ticket_creation_message(ticket: WildValue) -> str: """They send us the description as HTML.""" cleaned_description = convert_html_to_markdown( ticket["ticket_description"].tame(check_string)) content = TICKET_CREATION_TEMPLATE.format( name=ticket["requester_name"].tame(check_string), email=ticket["requester_email"].tame(check_string), ticket_id=ticket["ticket_id"].tame(check_string), ticket_url=ticket["ticket_url"].tame(check_string), description=cleaned_description, type=ticket["ticket_type"].tame(check_string), priority=ticket["ticket_priority"].tame(check_string), status=ticket["ticket_status"].tame(check_string), ) return content