Using ElasticOCR with Microsoft Flow
Note: This sample Flow lacks error checking or retry logic, and should not be used in a production setting. It is intended purely to illustrate the core concepts required to create a Flow that consumes the ElasticOCR service.
In this example, we'll create a Flow to automatically OCR email attachments as they are received, and store the OCR'd versions in OneDrive for Business. From the homepage of the Flow portal, click "Create from blank" and follow the steps below.
From the triggers screen, select "Office 365 Outlook", then select the "Office 365 Outlook - When a new email arrives" trigger.
Click "Show advanced options" to expand the trigger's options, and set "Has Attachment" to "Yes" and "Include Attachments" to "Yes". This ensures that our trigger only fires if an email has an attachment, and makes that attachment available to our Flow.
Click "New Step", "Add an action", and search for the "Initialize Variable" action then add the "Variables - Initialize variable" action.
Rename the action by clicking on the "..." in the action's header, setting the new header to "License ID". Enter a name of "License ID", type of "String", and enter your ElasticOCR License ID in the value field.
Repeat the previous step to create another variable for your App ID.
Next, click "New step" > "More" > "Add an apply to each". This loop will allow us to look at each attachment in the email (if there happens to be more than one).
In the "Select an output from previous steps" field, select "Attachments" under "When new email arrives".
Inside the "Apply to each" loop, click "Add a condition".
Within this condition, we want to ensure that we only send PDF files to the ElasticOCR service. In the "Condition" box, select "Content-Type" under "When a new email arrives", set the condition logic to "is equal to", and set the value to "application/pdf".
All of our future work within this Flow will go inside the "If yes" side of this condition, so while there are two "Add an action" buttons visible, be sure to click the one in the "Yes" side of the condition moving forward.
Click the "Add an action" button with in the "Yes" side of the condition, search for "ElasticOCR", and add the "ElasticOCR - Create a job with a file" action.
Populate the "Filename", "License Id", and "App Id" fields, by selecting the email attachment name, License ID variable, App ID variable respectively. Set the "File Data" field to the attachment's file contents by clicking "See more" for the "When a new email arrives" operation, and selecting "Content".
Click the "... More" button in the "Yes" side of the condition, and click "Add a do until". This loop will poll the ElasticOCR service every 5 minutes, checking the status of the job until the job status is "Available".
Inside the "Do until", click "Add an action". Search for "ElasticOCR" and add the "ElasticOCR - Retrieve a job" action. Set the "Job Id" field to the "Job Id" output from the "Create a job with a file" operation, and set the License Id and App Id fields to the License ID and App ID variables.
After the "Retrieve a job" operation, add a new action by searching for "Schedule" and adding the "Schedule - Delay" action. Set the "Count" field to "5", and the "Unit" field to "Minute".
At the top of the "Do until" loop, click "Choose a value", and select "Retrieve a job" > "Status". Ensure the condition is set to "is equal to", and enter "Available" in the "Choose a value" field.
After the "Do until" loop, but inside the "Yes" side of the condition, add a new action. Search for "HTTP" and add the "HTTP - HTTP" action. Rename the action to "Get file" by clicking the "..." then clicking "Rename". Set the Method to "GET", and set the Uri to the ElasticOCR file download URL by clicking the "Retrieve a job" action's "See more" link and selecting "Download URL".
After the HTTP action, click "Add an action", search for "OneDrive for Business" and add the "OneDrive for Business - Create file" action. Construct a "Folder Path" by typing in "Email Attachments/", then clicking "Job Id" under "Create a job with a file". Set "File Name" to the "Filename" property under "Create a job with a file", and set "File Content" by clicking "See more" next to "Get file", and selecting "Body".
Lastly, we need to complete the ElasticOCR job now that the file has been successfully downloaded. Click "Add an action" one more time, and add the "ElasticOCR - Complete a job" action. Set the "Job Id" to the "Create job with a file" "Job Id", and set the License Id and App Id fields.
Your Flow is now complete. Click "Save" at the top right corner and test it.
Consider enhancing this Flow with error handling, or a response mechanism to notify you that the file has been processed with a link to where it is stored.