Skip to content

Commit e997365

Browse files
Adam GoughAdam Gough
authored andcommitted
fixed bodytext getting truncated
1 parent 150c51e commit e997365

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

apps/sim/lib/webhooks/outlook-polling-service.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,70 @@ export interface OutlookWebhookPayload {
7979
rawEmail?: OutlookEmail // Only included when includeRawEmail is true
8080
}
8181

82+
/**
83+
* Convert HTML content to a readable plain-text representation.
84+
* Keeps reasonable newlines and decodes common HTML entities.
85+
*/
86+
function convertHtmlToPlainText(html: string): string {
87+
if (!html) return ''
88+
89+
let working = html
90+
91+
// Remove script and style content
92+
working = working.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, '')
93+
working = working.replace(/<style[\s\S]*?>[\s\S]*?<\/style>/gi, '')
94+
95+
// Line breaks for common block-level tags
96+
working = working
97+
.replace(/<br\s*\/?>(?=\s|$)/gi, '\n')
98+
.replace(/<\/(p|div|li|h[1-6]|tr)>/gi, '\n')
99+
.replace(/<(p|div|li|h[1-6]|tr)[^>]*>/gi, '')
100+
.replace(/<\/(td|th)>/gi, '\t')
101+
102+
// Remove all remaining tags
103+
working = working.replace(/<[^>]+>/g, '')
104+
105+
// Decode common HTML entities
106+
const entityMap: Record<string, string> = {
107+
'&nbsp;': ' ',
108+
'&amp;': '&',
109+
'&lt;': '<',
110+
'&gt;': '>',
111+
'&quot;': '"',
112+
'&#39;': "'",
113+
}
114+
for (const [entity, char] of Object.entries(entityMap)) {
115+
working = working.split(entity).join(char)
116+
}
117+
// Numeric entities (decimal)
118+
working = working.replace(/&#(\d+);/g, (_, dec: string) => {
119+
const code = Number(dec)
120+
return Number.isFinite(code) ? String.fromCharCode(code) : _
121+
})
122+
// Numeric entities (hex)
123+
working = working.replace(/&#x([0-9a-fA-F]+);/g, (_, hex: string) => {
124+
const code = Number.parseInt(hex, 16)
125+
return Number.isFinite(code) ? String.fromCharCode(code) : _
126+
})
127+
128+
// Normalize whitespace
129+
working = working
130+
.replace(/\r\n/g, '\n')
131+
.replace(/\r/g, '\n')
132+
.replace(/\t/g, ' ')
133+
.replace(/\u00A0/g, ' ')
134+
135+
// Collapse excessive blank lines and trim
136+
working = working
137+
.split('\n')
138+
.map((line) => line.trimEnd())
139+
.join('\n')
140+
.replace(/\n{3,}/g, '\n\n')
141+
.trim()
142+
143+
return working
144+
}
145+
82146
export async function pollOutlookWebhooks() {
83147
logger.info('Starting Outlook webhook polling')
84148

@@ -357,7 +421,18 @@ async function processOutlookEmails(
357421
to: email.toRecipients?.map((r) => r.emailAddress.address).join(', ') || '',
358422
cc: email.ccRecipients?.map((r) => r.emailAddress.address).join(', ') || '',
359423
date: email.receivedDateTime,
360-
bodyText: email.bodyPreview || '',
424+
bodyText: (() => {
425+
const content = email.body?.content || ''
426+
const type = (email.body?.contentType || '').toLowerCase()
427+
if (!content) {
428+
return email.bodyPreview || ''
429+
}
430+
if (type === 'text' || type === 'text/plain') {
431+
return content
432+
}
433+
// Default to converting HTML or unknown types
434+
return convertHtmlToPlainText(content)
435+
})(),
361436
bodyHtml: email.body?.content || '',
362437
hasAttachments: email.hasAttachments,
363438
isRead: email.isRead,

apps/sim/triggers/outlook/poller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const outlookPollingTrigger: TriggerConfig = {
7979
},
8080
bodyText: {
8181
type: 'string',
82-
description: 'Plain text email body (preview)',
82+
description: 'Plain text email body',
8383
},
8484
bodyHtml: {
8585
type: 'string',

0 commit comments

Comments
 (0)