Twilio liquid loop through parsed json data - twilio

I am using Twilio IVR studio. I am using http request widget and know I can call 'widgets.MY_WIDGET_NAME.parsed.[parsed variable name]' to get the returned json data.
Say like I have the above widget return 7 jobs inside the array of hashes. I just want to return the job_id for each one and not the other data inside it. How would I got about doing that with liquid? I know there is loop but having a hard time getting.
I can do something like widgets.MY_WIDGET_NAME.parsed.[parsed variable name][0].job_id and it returns the first id. Any help would be appreciated.
"parsed": {
"success": "7 visits found",
"visits": [
{
"job_id": "12344",
"check_in": "",
"check_out": ""
},
{
"job_id": "12344",
"check_in": "",
"check_out": ""
},
{
"job_id": "12344",
"check_in": "",
"check_out": ""
},
{

Twilio developer evangelist here.
Twilio Studio supports Liquid templates. You can iterate a number of ways in Liquid including using a for loop. So you could do something like:
{% for job in widgets.MY_WIDGET_NAME.parsed.[parsed variable name] %}
{{ job.job_id }}
{% endfor %}
Let me know if that helps.

this note might help: Parse JSON into array javascript with Twilio Function
if not, this might: https://www.twilio.com/docs/studio/widget-library/run-function (under the Response heading, it's the description for Content Type)
details: i asked the same question of the NickH at Twilio and got this gem back that i have permission to share: "(Twilio) automatically parses JSON payloads returned to Function widgets and HTTP Request widgets! However, it has to be a JSON object, not an array. (don't worry, it can still contain an array). Instead of this:
[
{},
{},
{}
]
try returning this:
{
"result":[
{},
{},
{}
]
}
You'll access these variables as {{widgets.NAME_OF_FUNCTION_WIDGET.parsed.result[index]}}​(index being an integer starting from 0, for this example, 0, 1, and 2)."
hth!

Related

Twilio Function: access variable from studio flow "set variable"

I'm sure this is a silly question, but I don't claim to be a developer, just someone with a wide ranging job description 😊
I am following this guide: https://www.twilio.com/blog/forward-voicemail-recordings-to-email
The transcription callback is firing correctly and sending me the email. That's great.
I'm looking to expand this to collect additional information fields through Gather widgets. I have the following being sent to my function:
{
"flow": {
"flow_sid": "FW07e11311d367...f8a0501c05e5108",
"variables": {
"CallerName": "Joe Bloggs"
},
"channel": {
"address": "+441...147"
},
"sid": "FN866c64beb9...f5bf349fa19ad3"
},
"widgets": {
"SetCallerNameVar": {
"CallerName": "Joe Bloggs"
},
"GatherVoicemail": {
"Called": "+4414....7",
"Digits": "#",
"RecordingUrl": "https://api.twilio.com/2010-04-01/Accounts/AC5fa2...12c7/Recordings/RE3a1d420de6db...2abb554c04f6",
"CallerCountry": "GB",
"Direction": "inbound",
[...]
I access the other (working) information through the ${event.variable} syntax. However, simply doing ${event.CallerName} results in "undefined". Can anyone advise how to access the CallerName variable that I have set in my flow?
If I was calling the function rather than using a transcription callback, it would be easy to pass the parameter, but doing so would result in duplicate emails per call.
I hope this makes sense and appreciate any advice.
Thank you
Twilio developer evangelist here.
The issue here is that your transcription callback does not have the same context as the Studio Flow, so does not come with all the other data. You can, however, add that context to the request by setting query parameters on the transcription callback URL.
Try setting your transcription callback URL to:
https://your-function-service.twil.io/?CallerName={{flow.variables.CallerName}}
You will then receive the CallerName in the event object.
Just to note, the Transcription Callback URL field does not highlight the liquid variable, but it does get interpolated. There is now an open issue to add the highlighting to this field.

Issue passing in custom parameters to Twilio API

I am attempting to build out a process that will do the following:
New contact lands in my CRM (Podio)
The CRM is triggered to send a POST containing a custom parameter to Twilio which will then initiate a flow
The Twilio flow will call my cell and prompt me to connect to the new contact
Once I press 1, it will take the custom parameter (new contact's phone number) and connect
me to them.
So far, I have been able to get 1-3 with no problem.
However, according to the the API documentation from Twilio I should be able to pass a 3rd parameter in the JSON body of my HTTP request containing custom "Parameters". When I attempt this the parameters aren't showing in the Twilio studio logs and thus I am unable to connect the call to the new lead.
This is the JSON body I am sending over to my Twilio Flow's API
{
"To": "+1xxxxxxxxx1",
"From": "+1xxxxxxxxx2",
"Parameters": {
"name": "example name",
"prospectphone": "+1xxxxxxxxx3"
}
}
When I view the logs in Twilio this is what I received.
{
"contact": {
"channel": {
"address": "+1xxxxxxxxx2"
}
},
"trigger": {
"request": {
"from": "+1xxxxxxxxx2",
"to": "+1xxxxxxxxx1",
"parameters": {}
}
},
"widgets": {},
"flow": {
"flow_sid": "FW936f72c0eb8d3945d0d55c62465e37a8",
"data": {},
"channel": {
"address": "+1xxxxxxxxx2"
},
"sid": "FN955cdb12827f84c8af8fc9b5264e5a28"
}
}
I really can't figure out what I'm missing here? Has anyone run into this before? Is this something you can only do if you're using the SDK or does the additional parameter need to be passed in some other way?
It should be sent x-www-form-urlencoded.
From the documentation:
"Note: the JSON value must explicitly be passed as a string, not as a hash object. Depending on your particular HTTP library, you may need to add quotes or URL encode the JSON string."
Trigger a Twilio Studio Flow Execution via the REST API

How can i parse this json output?

["from": /topics/nursery-a, "is_background": false, "flag": 1, "data": {"msg_head_id":{"msg_head_id":7,"name":"Section","created_at":"2017-01-24 19:34:43"},"msg_title":"hello","message":{"msg_head_id":7,"doc_url":"","msg_title":"hello","created_at":"2017-02-24 10:11:07","message_id":225,"message":"welcome"},"user":{"user_id":57,"gcm_registration_id":"","name":"Iron","created_at":"2016-12-27 12:41:18","email":"info#mail.in"}}, "title": Smart , "collapse_key": do_not_collapse]
it's not a valid JSON. it should be start and close with { and }, remove [ and ], also quotation mark is missing. After these changes try this in http://json.parser.online.fr/
According to the official site, json is built on two structures -- which named dictionary and array in iOS.
So this json output isn't valid json. The value of a json key should be one of array/object(key value pairs)/number/string/boolean/null.
The from, title, collapse_key should boxed in "" to be a string.
And the json output should start and end with {} not [].

Handling better response from Aws::Route53::Client.new

First time trying to use the Ruby AWS ADK V2 and I am trying to format the data i am getting back and it seems quiet hard getting it into useable format.
All I want to do is get a list of Hosted Zones and display in a table.
I have a helper that has:
def hosted_zones
r53 = Aws::Route53::Client.new
#convert to hash first so we can parse and covert to json
h = (r53.list_hosted_zones).to_hash
j = JSON.parse((h.to_json))
end
which then returns me the following JSON:
{
"hosted_zones": [{
"id": "/hostedzone/Z1HSDGASSSME",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:45.277646707+01:00",
"config": {
"comment": "Private DNS zone for stage",
"private_zone": true
},
"resource_record_set_count": 10
}, {
"id": "/hostedzone/ZJDGASSS0ZN3",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:41.290143511+01:00",
"config": {
"comment": "Public DNS zone for stage",
"private_zone": false
},
"resource_record_set_count": 7
}],
"is_truncated": false,
"max_items": 100
}
To which I am running a really but while statement to interact through all the hosted_zone entries into a table.
Is this the best way to get the response or can you request the response to be json already?
Why are you converting a hash to JSON, only to convert it to a hash again? JSON.parse(some_hash.to_json) will just give you some_hash.
That being said, I don't think it is possible to get JSON directly from AWS, mainly due to the fact that their API responds with XML. I think that your solution is ideal if that's all you're doing, but if you want, you can make a request with an HTTP client and then take the XML that you receive and use something like ActiveSupport's Hash.from_xml to create a hash that you can then convert to JSON.

Rails 4 API, how to create a URL from a JSON response?

I'm working on an API where I have a few routes setup, ie
http://localhost:3000/phone_number_lookup/1234567890
which can return a JSON response like so:
{
"AccountCode": "1234",
"AccountID": 13579,
"BalanceCurrent": "5000",
"Phone": "1234567890",
"Id": 123123,
"SerialNumber": "Y2K2000XY2016",
"MACADDRESS": "y2k2000xy2016",
"EQUIPMENTTYPE_Name": "Motorola DCX100 HD DVR",
"ADDRESS_Zip": "90210",
"ItemID": 12345,
"iVideoSystemID": 1000001
"id": null
}
The next 'step' of the API consumption would be, 'given the initially returned response, use 4 of those parameters and pass them into a remote URL that will then do something.'
Like so:
http://myremoteURL.com/Service/?Param1=sSerialNumber&Param2=iVideoSystemID&Param3=sMAC&Param4=ItemID
It would be one thing to just set up a route that takes 4 parameters, but the route needs to be contingent on what the initial JSON response was.
What is the proper way to do this?
First of all, you'll have to convert your JSON to hash. Something like this will do:
[7] pry(main)> hash=JSON.parse(json)
=> {"AccountCode"=>"1234",
"AccountID"=>13579,
"BalanceCurrent"=>"5000",
"Phone"=>"1234567890",
"Id"=>123123,
"SerialNumber"=>"Y2K2000XY2016",
"MACADDRESS"=>"y2k2000xy2016",
"EQUIPMENTTYPE_Name"=>"Motorola DCX100 HD DVR",
"ADDRESS_Zip"=>"90210",
"ItemID"=>12345,
"iVideoSystemID"=>1000001,
"id"=>nil}
Then you'll have to choose 4 parameters to send. I just took last 4 parameters
[14] pry(main)> chosen_params = hash.slice("ItemID", "id", "iVideoSystemID", "ADDRESS_Zip")
=> {"ItemID"=>12345, "id"=>nil, "iVideoSystemID"=>1000001, "ADDRESS_Zip"=>"90210"}
Then you'll have to pass them to your remote url. This can be done using a helper described here. Then you'll have to just do something like generate_url("YOUR-URL-ADDR-HERE", chosen_params).
At this point you might want to change the generate_url helper in a way you need it to be to generate the url you need. Maybe it should take third parameter called action which will then generate url like http://www.google.com/action?{chosen_params}
The result will be:
[23] pry(main)> generate_url("http://www.google.com", chosen_params)
=> "http://www.google.com?ADDRESS_Zip=90210&ItemID=12345&iVideoSystemID=1000001&id="
Hope it helps. Let me know about any questions.
Could you modify the JSON response?
{
"AccountCode": "1234",
"AccountID": 13579,
...
"id": null
"follow_up_url": "http://myremoteURL.com/Service/?Param1=sSerialNumber&Param2=iVideoSystemID&Param3=sMAC&Param4=ItemID"
}
This allows your JSON to tell the requester "where to go next".

Resources