Intoduction

Message flows are the ideal way te create input scenarios in Rogerthat, but sometimes that is not enough.

We extended these message flows with 2 things:

Javascript validation

This validation can only be used in form messages such as sliders, text input boxes, single and multi select…
So what we want to do here is validate the user input, if the value was not “john.doe@example.com” we return an error.

The return value needs to be true or a string in case of an error.

Example of a text input box:

var run = function(result, rogerthat){
    var v = result.value.trim();
    if (v == "") {
        return "Please enter an email address;
    } else if (v == "john.doe@example.com") {
        return "Please enter 'john.doe@example.com' to continue.";
    }
    return true;
};

Flow code

We placed a flow code block behind a text input an depending what u user entered we show another step.
1. show a text line
2. Show a text block
3. Show an auto complete
4. Show a single select
5. Show a multi select
6. Show a date select with mode date
7. Show a date select with mode date and time
8. Show a date select with mode time
9. Show a single slider
10. Show a range slider
11. Show an advanced order

The result value needs to be an object with the following properties:

  • outlet: The name of the outlet you want as your next step
  • defaultValue: The default value of your next step (optional)
  • message: The message of your next step (optional)

Example of a text input box:

var run = function(rogerthat, messageFlowRun){
    var user = rogerthat["user"];
    var service = rogerthat["service"];
    var system = rogerthat["system"];
    var flowId= messageFlowRun.flow_id;
    var steps = messageFlowRun.steps;

    var nextStepResult = {};
    nextStepResult.outlet = null;
    nextStepResult.defaultValue = null;
    nextStepResult.message = null;

    if (steps.length < 1) {
        nextStepResult.outlet = "1";
        nextStepResult.message = "Flow code was placed at the beginning of the flow. Nothing to validate";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "1") {
        nextStepResult.defaultValue = "1 text line widget";
        nextStepResult.outlet = "1";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "2") {
        nextStepResult.defaultValue = "2 \nText block  widget";
        nextStepResult.outlet = "2";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "3") {
        nextStepResult.defaultValue = "3 autcomplete widget";
        nextStepResult.outlet = "3";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "4") {
        nextStepResult.defaultValue = "ss2";
        nextStepResult.outlet = "4";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "5") {
        nextStepResult.defaultValue = ["ms2", "ms3", "ms4"];
        nextStepResult.outlet = "5";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "6") {
        var now_ = parseInt(new Date().getTime() / 1000);
        now_ = now_ - (now_ % 86400);
        nextStepResult.defaultValue = now_ + (2 * 86400);
        nextStepResult.outlet = "6";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "7") {
        var now_ = parseInt(new Date().getTime() / 1000);
        nextStepResult.defaultValue = now_ + (2 * 86400);
        nextStepResult.outlet = "7";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "8") {
        var now_ = parseInt(new Date().getTime() / 1000);
        nextStepResult.defaultValue = now_ + (2 * 86400);
        nextStepResult.outlet = "8";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "9") {
        nextStepResult.defaultValue = 55.55;
        nextStepResult.outlet = "9";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "10") {
        nextStepResult.defaultValue = [25.55, 78.12];
        nextStepResult.outlet = "10";
    } else if (steps[steps.length -1]["form_result"]["result"]["value"] == "11") {
        nextStepResult.defaultValue = {
            "categories": {
                "romance" : {
                    "items": {
                        "1" : {
                            "id": "1",
                            "name": "Fifty Shades of Grey",
                            "description": "by E.L. James",
                            "value": 2,
                            "unit": "pc.",
                            "unit_price": 999,
                            "step": 1,
                            "step_unit": "pc.",
                            "step_unit_conversion": 1,
                            "image_url": "https://d2arxad8u2l0g7.cloudfront.net/books/1385207843l/10818853.jpg",
                            "has_price": true
                        }
                    }
                }
            }
        };
        nextStepResult.outlet = "11";
    } else {
        nextStepResult.outlet = "1";
        nextStepResult.message = "Please enter a valid number (1 to 11)";
    }

    return nextStepResult;
};

Result object

Text line, text box and auto complete

  • type: unicode_result
  • value: “the value of the input”

Single select

  • type: unicode_result
  • value: “the selected radio button”

Multi select

  • type: unicode_list_result
  • values: [“selected checkbox 1”, “selected checkbox 2”]

Date select

  • type: long_result
  • value: 1331212164

Single slider

  • type: float_result
  • value: 5.64

Range slider

  • type: float_list_result
  • values: [5.64, 7.85]

Advanced order

Rogerthat object

{
    "rogerthat.user": {
        "name": "John Doe",
        "account": "john.doe@foo.com",
        "avatarUrl": "https://rogerth.at/unauthenticated/mobi/cached/avatar/4824964683268096",
        "language": "pt_BR",
        "data": {"xmpp_username": "john", "xmpp_password": "doe"}
    },
    "rogerthat.service": {
        "name": "Service identity 1",
        "email": "info@foo.com",
        "account": "s1@foo.com/i1",
        "data": {"xmpp_room": "chatroom1"}
    },
    "rogerthat.system": {
        "os": "ios",
        "version": "7.1",
        "appVersion": "1.0.150.I",
        "appName": "Rogerthat",
        "appId": "rogerthat"
    }
}

Message flow run object

{
    "flow_id": "1516f2a4-aefc-490c-b1e7-76ae631391f4",
    "steps": [
        {
            "received_timestamp": 1331212149,
            "message_flow_id": "ahNkZXZ-bW9iaWNhZ2VjbG91ZGhycj0LEgptYy10cmFja2VyIgxjYX",
            "step_type": "message_step",
            "step_id": "message_welcome",
            "acknowledged_timestamp": 1331212162,
            "answer_id": "button_yes"
        },
        {
            "received_timestamp": 1331212164,
            "message_flow_id": "ahNkZXZ-bW9iaWNhZ2VjbG91ZGhycj0LEgptYy10cmFja2VyIgxjYX",
            "step_type": "form_step",
            "step_id": "message_price",
            "answer_id": "positive",
            "form_result": {
                "type": "float_list_result",
                "result": {
                    "values": [
                        30.0,
                        50.0
                    ]
                }
            },
            "acknowledged_timestamp": 1331212173
        }
    ]
}

Advanced order object

{
    "currency": "$",
    "categories": [{
        "id": "romance",
        "name": "Romance",
        "items": [{
            "id": "1",
            "name": "Fifty Shades of Grey",
            "description": "by E.L. James",
            "value": 0,
            "unit": "pc.",
            "unit_price": 999,
            "step": 1,
            "step_unit": "pc.",
            "step_unit_conversion": 1,
            "image_url": "https://d2arxad8u2l0g7.cloudfront.net/books/1385207843l/10818853.jpg",
            "has_price": true
        }]
    }, {
        "id": "horror",
        "name": "Horror",
        "items": [{
            "id": "2",
            "name": "'Salem's Lot",
            "description": "by Stephen King",
            "value": 0,
            "unit": "pc.",
            "unit_price": 799,
            "step": 1,
            "step_unit": "pc.",
            "step_unit_conversion": 1,
            "image_url": "https://d2arxad8u2l0g7.cloudfront.net/books/1327891565l/11590.jpg",
            "has_price": true
        }]
    }, {
       "id": "whodunit",
       "name": "Whodunit",
       "items": [{
          "id": "3",
          "name": "Mirror Deep",
          "description": "by Joss Landry",
          "value": 0,
          "unit": "pc.",
          "unit_price": 899,
          "step": 1,
          "step_unit": "pc.",
          "step_unit_conversion": 1,
          "image_url": "https://d2arxad8u2l0g7.cloudfront.net/books/1368021554l/17901969.jpg",
          "has_price": true
      }]
  }]
}