How to regex Zapier and get output?

2019-09-25 11:53发布

问题:

I regularly receive emails from the same person, each containing one or more unique identifying codes. I need to get those codes.

The email body contains a host of inconsistent email content, but it is the strings I am interested in. They look like...

  • loYm9vYzE6Z-aaj5lL_Og539wFer0KfD
  • FuZTFvYzE68y8-t4UgBT9npHLTGmVAor
  • JpZDRwYzE6dgyo1legz9sqpVy_F21nx8
  • ZzZ3RwYzE63P3UwX2ANPI-c4PMo7bFmj

What the strings seem to have in common is, they are all 32 characters in length and all composed of a mixture of both uppercase, lowercase, numbers and symbols. But a given email may contain none, one or multiple, and the strings will be in an unpredictable position, not on adjacent lines as above.

I wish to make a Zap workflow in Zapier, the linking tool for web services, to find these strings and use them in another app - ie. whenever a string is found, create a new Trello card.

I have already started the workflow with Zapier's "Gmail" integration as a "trigger", specifically a search using the "from:" field corresponding to the regular sender. That's the easy part.

But the actual parsing of the email body is foxing me. Zapier has a rudimentary email parser, but it is not suitable for this task. What is suitable is using Zapier's own "Code" integration to execute freeform code - namely, a regular expression to identify those strings.

I have never done this before and am struggling to formulate working code. Zapier Code can take either Python (documentation) or Javascript (documentation). It supports data variables "input_data" (Python) or "inputData" (Javascript) and "output" (both).

See, below, how I insert the Gmail body in to "body" for parsing...

I need to use the Code box to construct a regular expression to find each unique identifier string and output it as input to the next integration in the workflow, ie. Trello.

For info, in the above screengrab, the existing "hello world" code in the box is Zapier's own test code. The fields "id" and "hello" are made available to the next workflow app in the chain.

But I need to do my process for all of the strings found within an email body - ie. if an email contains just one code, create one Trello card; but if an email contains four codes, create a Trello card for each of the four.

That is, there could be multiple outputs. I have no idea how this could work, since I think these workflows are only supposed to accommodate one action.

I could use some help getting over the hill. Thank-you.

回答1:

David here, from the Zapier Platform team.

I'm glad you're showing interest in the code step. Assuming your assumptions (32 characters exactly) is always going to be true, this should be fairly straightforward.

First off, the regex. We want to look for a character that's a letter, number, or punctuation. Luckily, javascript's \w is equivalent to [A-Z0-9a-z_], which covers the bases in all of your examples besides the -, which we'll include manually. Finally, we want exactly 32 character length strings, so we'll ask for that. We also want to add the global flag, so we find all matches, not just the first. So we have the following:

/[\w-]{32}/g

You've already covered mapping the body in, so that's good. The javascript code will be as follows:

// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/[\w-]{32}/g) 

// the .map function executes the nameless inner function once for each 
// element of the array and returns a new array with the results
// [{str: 'loYm9vYzE6Z-aaj5lL_Og539wFer0KfD'}, ...]
return (matches || []).map(function (m) { return {str: m} })

Here, you'll be taking advantage of an undocumented feature of code steps: when you return an array of objects, subsequent steps are executed once for each object. If you return an empty array (which is what'll happen if no keys are found), the zap halts and nothing else happens. When you're testing, there'll be no indicator that anything besides the first result does anything. Once your zap is on and runs for real though, it'll fan out as described here.

That's all it takes! Hopefully that all makes sense. ​Let me know if you've got any other questions!