def extract_body(message: message.Message, remove_quotations: bool=True) -> 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 remove_quotations: return talon.quotations.extract_from_plain(plaintext_content) else: return 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 remove_quotations: return convert_html_to_markdown(talon.quotations.extract_from_html(html_content)) else: return convert_html_to_markdown(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_body(message: message.Message, remove_quotations: bool = True) -> 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 remove_quotations: return talon.quotations.extract_from_plain(plaintext_content) else: return 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 remove_quotations: return convert_html_to_markdown( talon.quotations.extract_from_html(html_content)) else: return convert_html_to_markdown(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 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 format_freshdesk_ticket_creation_message(ticket): # 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 extract_body(message): # 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: return 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: return convert_html_to_markdown(html_content) raise ZulipEmailForwardError("Unable to find plaintext or HTML message body")
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 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 extract_body(message: message.Message) -> 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: 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: return convert_html_to_markdown(talon.quotations.extract_from_html(html_content)) raise ZulipEmailForwardError("Unable to find plaintext or HTML message body")