How to conditional statements in action output in Bixby

171 Views Asked by At

Hi I want to go to another action by putting a conditional statement in the action output. What should I do?

For example

action (~~) {
  description (Validate items passed from javascript)
  collect {
    input (~~) {
      type (~~)
      min (~~) max (~~)
    }
  }

  type(~~~)

  output (items){
    on-empty(items.item1){ // if items.item1 is empty.
      replan { // go to case1Action.
            intent {
              goal : case1Action
              value : ~~~~~
            }
          }
    }else{ // else
      replan { // go to case2Action.
            intent {
              goal : case2Action
              value : ~~~~~
            }
          }
    }
  }

or I want to select the view according to the output value.(Actually this is the purpose of the question)

output (items){
    if(items.item1 == "goFirstCase"){
      // First case view
    }else{
      // Second case view
    }
  }
3

There are 3 best solutions below

1
On BEST ANSWER

I think by "select a different view according to the output value" I presume you mean you want to change what shows on the screen? because a "view" actually is comprised of the dialog, layout, and conversation-drivers. https://bixbydevelopers.com/dev/docs/dev-guide/developers/building-views.views

For majority of use cases, there's really only one result-view that will be used, and any of the three contents of a view can be changed based on your preferred conditions as the above answer suggests.

within a view you can have the three different components defined with these three blocks: message for dialog, render for layout, and conversation-drivers

using your example,

//in a result-view
message {
    if (items.item1 == "firstCase") {
        template-macro (firstCase-result-dialog) {//enter params}
    }
}
render {
    if (size(items) > 1) {
        list-of (items) {
            where-each (item) {
                if (item == "firstCase") {
                    layout-match (item) {
                        mode (Summary)
                    }
                    // OR use layout-macro
                    layout-macro (firstCase-result-summary-thumbnail-card) {//enter params}
                }
            }
        }
    }
}

similar conditional work can be done on conversation-drivers of course.

0
On

Another option. instead to use 'on-empty' block, you can use 'throws - error'

in action model,

output (items) {
  throws {
    error (case1ActionError) {
      on-catch {
        replan {
          intent {
            goal : case1Action
            value : ~~~~~
          }
        }
      }
    }
    error (case2ActionError) {
      on-catch {
        replan {
          intent {
            goal : case2Action
            value : ~~~~~
          }
        }
      }
    }
  }
}

And, in js code,,

if (error condition1) {
  throw fail.checkedError('case 1 error', 'case1ActionError')
} else if (error condition2) {
  throw fail.checkedError('case 2 error', 'case2ActionError')
}
  • For fail, refer to https://bixbydevelopers.com/dev/docs/reference/JavaScriptAPI/fail#failcheckederrormessage-errorid-errorobj
0
On

In your case, you would not need on-empty in action, but use template-macro-def as briefly explained in https://corp.bixbydevelopers.com/dev/docs/dev-guide/developers/refining-dialog.dialog-macros

// template file one
template-macro-def (id1) {
  params {
   param (x) {
    Type (Items) .... 
}

// template file two
template-macro-def (id2) {
  // param x is type Items 
}

//view file 
result-view {
 match: Items(this) 
 // after some checking make sure is single item 
 // it is possible to use other condition like if (this.sub1 == 2) 
 if (exists(this.sub1)) {
   template-macro (id1) {
     param (x) { expression (this) }
   }
 }
 else {
   template-macro (id2) {
     param (x) { expression (this) }
   }
 }

The above would be the recommended way to handle different views for same concept in Bixby.

on-empty in action would be used for the purpose of either replan or relax some search condition in order to avoid 0 result. Bixby does not support on-empty(key) {} syntax according to https://corp.bixbydevelopers.com/dev/docs/reference/type/action.output.on-empty and on-empty would only apply in your case if output(items) itself is empty and would not check any sub-property of items.