Home > Back-end >  How can i select an item from a HTTP result in Twilio Liquid?
How can i select an item from a HTTP result in Twilio Liquid?

Time:08-04

I'm having some trouble retrieving data from an array using Twilio's Liquid implementation.

Currently i have something that works for lists with size < 15, which is something along the lines of:

{%- assign list = widgets.http_request.parsed.list -%} 
{%- assign returnVal = -1 -%} 
{%- assign input = widgets.reply_msg_input.inbound.Body | downcase | strip -%} 
  {%- for item in list -%} 
    {%- assign current = item.description | downcase -%}
    {%- if current == input -%}
      {%- assign returnVal = item.id -%} 
    {%- endif -%} 
  {%- endfor -%}
{{- returnVal -}}

But this has 2 problems.

  1. Twilio's Liquid implementation is VERY limited, as in i can't use the WHERE filter to get this nor can i have a loop go through more than 15 iterations.
  2. My list has 81 items, which is my biggest problem currently...

I've tried creating a "loop" made out of widgets, and that worked, but any unexpected value left untreated would instantly cause an infinite loop, ending the execution. Is there any way of doing this without using any functions? I'm trying really hard to reduce this flow's cost due to it being intended as a cheap channel for health access.

Thanks in advance!

CodePudding user response:

You are right that there are limits to Twilio Studio's handling of lists in liquid, specifically that limit of 15 iterations.

However, aside from what you have suggested (building a widget loop in Studio, passing it to a Function) I don't think there's anything else you can do within Twilio to help this.

If you have control over the API you are calling with the HTTP widget, then perhaps you could build an endpoint that accepted the user input as a parameter and only returned the item you are looking for.

Otherwise, I would recommend working with a Function to do this. If you are looking to reduce costs, then I recommend trying to talk to sales or, if your service is a non-profit, applying to Twilio.org.

  • Related