Hi everyone,
I'm building an action step to pull email bodies from a Gmail thread (the normal ‘find email’ action step finds an individual message, where I want every message in a given thread, and there’s no ‘find thread’ action step, so I’m making it). It's mostly working, but I'm running into a weird formatting issue when I try to handle the response.
I’m getting the message body in Base64, and when I decode it to a utf-8 string, it works, in that I get the messages, but they’re split by character. I’ve tried a few things to recombine the result, but it’s not working, and I don’t really know what I’m doing (I’m doing the code with ChatGPT. Sorry in advance for the ignorance I’m no doubt showing here).
Here's the result I'm getting:
{
"0": "H",
"1": "e",
"2": "y",
"3": " ",
"4": "E",
"5": "l",
"6": "i",
"7": "j",
"8": "a",
"9": "h",
"10": "!",
etc.
Here's the code I'm using:
const options = {
url: `https://www.googleapis.com/gmail/v1/users/me/threads/${bundle.inputData.Thread_ID}`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
return z.request(options)
.then((response) => {
const results = response.json;
// Extracting email bodies from each message in the thread
const messages = results.messages.map(message => {
const parts = message.payload.parts || []; // Some messages might not have parts, handle that case
const body = parts.find(part => part.body && part.body.data); // Find the part with body.data
// If a body is found, base64 decode it
if (body && body.body.data) {
const decodedBody = Buffer.from(body.body.data, 'base64').toString('utf-8');
return decodedBody;
} else {
return ''; // If no body is found, return an empty string
}
});
return messages; // Return only the decoded email bodies
});
As you can see, instead of getting a normal string like:Hey Elijah!
...I'm getting an object where each character is stored separately with a numeric key.
Anyone seen this before? Or know a good pattern for handling?
Thanks so much in advance =)