Use case: I want to consume google emails through their api for use in my app.
I already have the data. It’s currently in base64url which I think mailparser can handle. Their documentation is very sparse, so, if needed, I can decode in a pre-workflow step before sending to the mailparser action.
gmail creates rfc822 emails and base64url encodes them. I’m getting the raw body and trying to pass it to mailparser for processing. Then, send the different parts of the email (subject, body, to, from, etc.) to a table in my database through the plugin’s outputs that I’ve defined for the different parts (e.g. subject, body, to, from). One output will equal one field in the table.
Thanks @antony. The problem I am unable to figure out is, how to pass the values from the various items back to the outputs I’ve defined in the plugin. Here is some simple code, which works in repl.it, but I’m not able to figure out how to pass the values (e.g. parsed.subject) into my plugin’s output(s) (e.g. subject).
var my_title_out, my_email_out, my_messaging_out;
// lots of data processing...
return {"title_out":my_title_out, "email_out":my_email_out, "messaging_out":my_messaging_out, "error_out":false};
Passing Values Back Out - Element Based Client Side
I have created a similar element based client side plugin, so it has the same outputs (or “Exposed States” in this case) defined as follows:
I encountered exactly the same issue with another node module over the weekend. I tried a number of different “async” approaches to no avail; although I did manage to trigger several server (500 status code) errors.
I wonder if @marca, the “node meister”, might be able to shed some light. Knowing how to get such values back into Bubble would certainly open up some possibilities with SSA’s.
@bubble’s documentation is severely lacking for this. I had to enlist three different node/javascript experts, and the first two said it couldn’t be done or that Bubble’s documentation needed more details and to contact them for help.
The final person was able to figure it out after much trial and error with the below code being the end result that I hope will help others. So, I’d like to give a big recommendation to Nigel Peck on the Codementors site. If you need any help with coding, he knows his stuff. https://www.codementor.io/nigelbpeck
TLDR: You have to do a “return context.async…” with a callback.
function(properties, context) {
var simpleParser = require('mailparser').simpleParser;
var email = "From: 'Sender Name' <sender@example.com>\r\n"+
"To: 'Receiver Name' <receiver@example.com>\r\n"+
"Subject: Hello world!\r\n"+
"\r\n"+
"How are you today?";
return context.async(function(cb){
simpleParser(email)
.then(parsed => {
cb(undefined, {
subject: parsed.subject,
to: JSON.stringify(parsed.to.value),
from: JSON.stringify(parsed)
});
})
.catch(err => {
cb(err);
});
});
}
The undefined value is there because, per the docs, the callback expects both err and res arguments. The bubble_return_object represents the object defined in the Return values (output values) section of the plugin editor for server-side actions.
Is context.async blocking or non-blocking?
Edit: and why use context.async instead of just using promises or async/await? Does it forces blocking on that workflow action before proceeding to the next one?
Here’s the next issue that I could use some help with. Is there a way to define if statements in the callback? I’m defining a bunch of outputs and sometimes a few of them may not have values, so I need/want to wrap them in if statements, but Bubble is complaining that it’s not correct syntax.
What I don’t know, though, is whether Bubble requires all fields defined in the plugin editor to be present in the returned object. I guess we’ll find out.