Create A Workflow to Send Email triggered by the Mobile App

Let’s get you started creating your own workflow:

In this short tutorial you will build your own workflow which sends an email report.  You will also utilize the mobile app and dashboard to send JSON events to trigger the workflow.

Prerequisite:

Getting Started Guide

Mobile App Quick Start Guide

 

Step 1: Create a Tag

We’ll be using a tag that doesn’t exist in the account.  To create a tag, you first need to send an event with the JSON key representing that tag.  The key and tag will automatically be detected and created.

Let’s use the mobile app to send an event to create the tag.

Please refer to the Mobile App Quick Start Guide to learn the basics of the Mobile App.

Login with the mobile API credentials for your account.

 

unnamed

 

 

 

workflow2

Using the mobile app, select a JSON input widget to send an event with the following:

stream = raw

tag = send_email

value = <your name>

workflow3

Send this event once using the mobile app by clicking on “Send”

workflow4

 

Step 2: Activate the Tag

Next, will need to activate the tag.  From your Renesas IoT Sandbox web account, click Config -> DataStream and click the raw stream.  Look for the send_email tag you just created, check it to activate it, then Save at the bottom.

workflow5

 

Step 3: Create a new Workflow

Now we’re going to create a workflow.

  • Go to Workflow Studio, and click Create.

workflow6

 

  • To specify the name for this workflow, enter “Send Email” in the top input box.

workflow7

Note: Workflows save automatically.

 

Step 4: Adding a tag as trigger

Select the Tag & Triggers option on the Right menu, expand the raw stream input and drag the send_email tag onto the canvas.  Note: If you do not see send_email, that tag may not have been activated in Step 2 or you may need to refresh your browser to reload all available tags.

workflow8

 

Step 5: Add a Workflow Module

On the right panel, click the Modules menu, open Foundation and drag the Base Python module onto the canvas.

workflow9

 

Step 6: Connect the Tag to the Base Python Module

Click the tag node output anchor and drag a connection to the input node anchor on the Base Python module.

workflow10

 

Step 7: Edit the Python code

Double click on the Base Python module to open the code editor.

10-open-the-code-editor

Paste the following code into the code editor, making sure the indentation is preserved.  Note: Python is sensitive to indentation and Copy/Paste may modify the alignment.

—— copy below this line —–

# import Email library

import Email

 

# Get input data from input named 'in1'

name = IONode.get_input('in1')['event_data']['value']

 

# build a basic html message

html = "<html><body>Hello, my name is {}</body></html>".format(name)

 

try:

    email = Email.Email("support@medium.one", 'My workflow', '<your-email-here>', "You've got mail!")

    email.html_message(html)

    email.send()

except Exception:

      log("Unable to send Email")

—— copy above this line —–

 

Enter your own email in place of <your-email-here>inside the ‘quotes’.  Example: ‘me@medium.one’.

The code above simply gets the data from the input, which will contain the data in the key send_email.  It then uses it to build a simple html message, and send an email.

See the Email Library reference http://renesas-docs.mediumone.com/?libraries/email for more details on how this library works.

Click Check Syntax and make sure it passes.  If the syntax fails, you may have an indentation issue or no ‘quotes’ around your email.  If the syntax check passes, click Save and Activate.   This will save your workflow and activate it so it’s ‘live’ on your project.

You can check that the workflow was activated by looking into the Revision menu.  Note the green box is “activated”. Previous revisions of your workflow are stored here as well.

11-here-as-well

 

Step 8: Test your workflow

Now we are going to test our workflow by sending a debug event.  Let’s activate debug mode: go to the debug menu and click the Debug Logging Enabled toggle.  This will allow logs to be captured while Workflows are run.  Important Note: workflows in debug mode will consume 1 extra Workflow credit and may impact performance while running, be sure to disable debug mode when it’s no longer required.

workflow11

 

In the debugger, you can send a test event. Those events do not get propagated through or show in the data; they are just for testing.

Under ‘Stream’ select raw and under user select any API user.  Note: workflows are run in context to a specific user.  The commands in the Python code are executed with respect to that user, for example, analytics.  Reference renesas-docs.mediumone.com to learn more about Workflows.

Specify the test JSON event to send with your name in place of your name.

{"send_email": "your name"}

12-5

Click Send to send the test event.

Since the raw stream was selected, sending an event that includes the send_email key will cause our workflow to be triggered by the raw:send_email key.

 

Step 9: Check the logs

Now let’s check that everything worked by looking at the logs.

Click the refresh button in the same debug panel, you should see a new log appear.  

13-new-log-appear

If there are errors, they will show in the error log below

14-error-log-elow

If your workflow log is under “Last 10 logs”, then you have successfully created the workflow! Check your email to make sure you received it.  Note, your account will allow up to 10 emails per hour to be sent.  After that, they will queue up.  If you require more, you can link your own mailchimp account.  Refer to renesas-docs.mediumone.com.

TIP: To help troubleshoot issues, you can use log statements in the code.  For example, note the two added lines beginning with log:

# Get input data from input named ‘in1’

name = IONode.get_input(‘in1’)[‘event_data’][‘value’]

log(name) #log name

 

# build a basic html message

html = <html><body>Hello, you just triggered an email.</body></html>.format(name)

log(html) #log html

 

The log data will appear under the log on debug panel, simply click on the log to view.  

Note: Although debug events will trigger workflows they will not be saved in the database.

 

Step 10: Run the workflow

You can go back to the mobile app to send the same event again to test the workflow.  However for demonstration purposes, let’s see how we can send an event directly from the Dashboard using the User Input widget.  

Go to Dashboard and scroll down the page to the widget selection toolbox.

16

16-5

It will create a new User Input widget. Select a user in the drop down.

16-user-in-the-drop-down

Then click the gear icon to configure the tag to send data to.  You can type in the search box to find the raw:send_email tag

17-raw-send-email-tag

The widget will show a String Input box.  Type in your name, and click Send to send the event.

18-send-the-event

Back at the top of Dashboard, click the Save icon to save this view.  We will come back here later.

19-back-here-later

If you go back to your workflow, with debug activated, you should see another new log created.

TIP: You can add additional input triggers by opening the Base Python, click the Inputs / Outputs drop down and click Add Input.

20

 

The rest is up to your imagination!