Using ElasticOCR with Microsoft Flow

Last Updated: 31 May, 2018

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.

2018-05-29 16_27_55-Create your flow _ Microsoft Flow.png

Click "New Step", "Add an action", and search for the "Initialize Variable" action then add the "Variables - Initialize variable" action.

2018-05-29 16_28_39-Create your flow _ Microsoft Flow.png

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.

2018-05-29 16_31_21-Create your flow _ Microsoft Flow.png

Repeat the previous step to create another variable for your App ID.

2018-05-29 16_32_23-Create your flow _ Microsoft Flow.png

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).

2018-05-29 16_32_39-Create your flow _ Microsoft Flow.png

In the "Select an output from previous steps" field, select "Attachments" under "When new email arrives".

2018-05-29 16_33_19-Create your flow _ Microsoft Flow.png

Inside the "Apply to each" loop, click "Add a condition".

2018-05-29 16_33_54-Create your flow _ Microsoft Flow.png

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".

2018-05-29 16_34_56-Create your flow _ Microsoft Flow.png

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.

2018-05-29 16_35_21-Create your flow _ Microsoft Flow.png

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.

2018-05-29 16_36_03-Create your flow _ Microsoft Flow.png

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".

2018-05-29 16_40_19-Create your flow _ Microsoft Flow.png

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".

2018-05-29 16_41_48-Create your flow _ Microsoft Flow.png

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.

2018-05-29 16_44_56-Create your flow _ Microsoft Flow.png
2018-05-29 16_45_10-Create your flow _ Microsoft Flow.png

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".

2018-05-29 16_47_41-Create your flow _ Microsoft Flow.png

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.

2018-05-29 16_49_14-Create your flow _ Microsoft Flow.png
2018-05-29 16_50_23-Create your flow _ Microsoft Flow.png

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".

2018-05-29 16_55_50-Create your flow _ Microsoft Flow.png

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".

2018-05-29 17_10_46-Create your flow _ Microsoft Flow.png

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.

2018-05-29 17_12_43-Create your flow _ Microsoft Flow.png

Your Flow is now complete. Click "Save" at the top right corner and test it.

Next Steps

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.