MCPcopy Index your code
hub / github.com/simstudioai/sim / extractMessageBody

Function extractMessageBody

apps/sim/tools/gmail/utils.ts:151–186  ·  view source on GitHub ↗
(payload: any)

Source from the content-addressed store, hash-verified

149
150// Helper function to recursively extract message body from MIME parts
151export function extractMessageBody(payload: any): string {
152 // If the payload has a body with data, decode it
153 if (payload.body?.data) {
154 return Buffer.from(payload.body.data, 'base64').toString()
155 }
156
157 // If there are no parts, return empty string
158 if (!payload.parts || !Array.isArray(payload.parts) || payload.parts.length === 0) {
159 return ''
160 }
161
162 // First try to find a text/plain part
163 const textPart = payload.parts.find((part: any) => part.mimeType === 'text/plain')
164 if (textPart?.body?.data) {
165 return Buffer.from(textPart.body.data, 'base64').toString()
166 }
167
168 // If no text/plain, try to find text/html
169 const htmlPart = payload.parts.find((part: any) => part.mimeType === 'text/html')
170 if (htmlPart?.body?.data) {
171 return Buffer.from(htmlPart.body.data, 'base64').toString()
172 }
173
174 // If we have multipart/alternative or other complex types, recursively check parts
175 for (const part of payload.parts) {
176 if (part.parts) {
177 const nestedBody = extractMessageBody(part)
178 if (nestedBody) {
179 return nestedBody
180 }
181 }
182 }
183
184 // If we couldn't find any text content, return empty string
185 return ''
186}
187
188// Helper function to extract attachment information from message payload
189export function extractAttachmentInfo(

Callers 1

processMessageFunction · 0.85

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected