krl

Passing variables to an event from the "twilio:gather_start" command in KRL


I'm editing a KRL/Twilio app, and I have an event that gathers input from the user. Is it possible to pass a variable into the the event that "gather_start" fires? Here are the ways I've tried so far that don't work (in this case it's trying to pass the var "color" as "red"):

twilio:gather_start("choice") with action="choice?color=red" and numDigits = "1" and timeout = "5" and color = "red" and parameters = {"color":"red"};

Seems like persistent vars might be best (set something like "ent:color" to "red"), but it sounds like application persistent vars aren't available yet? TIA.


Solution

  • The right way to do this is persistent variables. App variables are one option, but what you probably want is entity variables. Kynetx Webhooks work with Twilio's cookie jar, resulting in a session that maintains entity variables in kynetx apps.

    Each phone call gets a session of it's own, so you don't need to worry about multiple simultaneous calls stepping on each other.

    App persistent variables (use app:myvar instead of ent:myvar) will work, but are global to the application, so they should only be used when the variables are scoped to the app.

    Here's a few rules that demonstrate this:

     rule firstquestion {
        select when twilio firstquestion
        {
          twilio:gather_start("firstanswer");
            twilio:say("Question One");
          twilio:gather_stop();
        }
      }
    
      rule firstanswer {
        select when twilio firstanswer
        pre {
          firstchoice = event:param("Digits");
        }
        {
          twilio:gather_start("secondanswer");
            twilio:say("Question Two");
          twilio:gather_stop();
        }
        fired {
          set ent:firstchoice firstchoice;
        }
      }
    
      rule secondanswer {
        select when twilio secondanswer
        pre {
          firstchoice = ent:firstchoice;
          secondchoice = event:param("Digits");
        }
        noop();
      }