How can I call an API & parse JSON data for use?

(Keranm) #1

Hi there

I am trying to build a Bot for Slack that can talk to Keen.IO to fetch data, parse the result and send that data back to Slack in a message.

I can’t find anyway to call a Keen API
https://api.keen.io/3.0/projects/{{project_ID}}/queries/extraction?api_key={{read_api_key}}&event_collection={{collection_name}}&timezone=UTC&latest=100&timeframe=this_1_days

The result would be
{ "result": [{ "keen": { "timestamp": "2016-04-26T09:35:53.792Z", "created_at": "2016-04-26T09:35:53.792Z", "id": "{{ID}}" }, "domain": "{{domain}}", "method": "GET", "time": 142.0 }, { "keen": { "timestamp": "2016-04-26T09:35:53.792Z", "created_at": "2016-04-26T09:35:53.792Z", "id": "{{ID}}" }, "domain": "{{domain}}", "method": "PUT", "time": 102.0 }, { "keen": { "timestamp": "2016-04-26T09:35:53.791Z", "created_at": "2016-04-26T09:35:53.791Z", "id": "{{ID}}" }, "domain": "{{domain}}", "method": "GET", "time": 341.0 }, { "keen": { "timestamp": "2016-04-26T09:35:53.791Z", "created_at": "2016-04-26T09:35:53.791Z", "id": "{{ID}}" }, "domain": "{{domain}}", "method": "PUT", "time": 1171.0 }, { "keen": { "timestamp": "2016-04-26T09:35:53.791Z", "created_at": "2016-04-26T09:35:53.791Z", "id": "{{ID}}" }, "domain": "{{domain}}", "method": "GET", "time": 377.0 }] }

I want to be able to parse out things like the domain, method and time - I just can’t find a flow that allows fetching & then processing this am I missing something simple?

(John Jackson) #2

Thanks for trying out the product!

Getting data by webhook and then parsing out data from the response is something that we still have some work to do on.

For now, the options are to either 1) use our Text (String) service to try to extract strings from the body of the response, or 2) wrap the API call in some code that alters the response format that makes it easier to manipulate/use inside Flow XO, possibly try a service like https://www.webscript.io/.

We are thinking carefully about this and will have some solutions soon.

(Perry Ismangil) #3

Another wrapper service: https://hook.io

(John Jackson) #4

Ah yes, I stumbled upon that recently too. Perfect for this kind of use case.

(Mohit Goyal) #5

@johnjackson : Can you please elaborate with an example on ‘2) wrap the API call in some code that alters the response format that makes it easier to manipulate/use inside Flow XO’.

I need to parse the json and then call an API ? How can I do that.

(John Jackson) #6

Actually were working on something to solve this very problem now, ready in the next week. I’ll keep you updated.

(Mohit Goyal) #7

Thanks. Looking forward to update.

Also, for different keywords or phrase that my bot understands, I need to
send correspondingly different messages via bot.

How do I configure multiple SEND MESSAGE bot after one NEW MESSAGE bot

(Kellsey Shaw) #8

If I’m understanding you correctly, you want to use a bot to Send Message depending on what keyword/phrase was used?

If that’s the case you can add multiple Send Message tasks to your Flow and apply filters to these. You would filter based on what the New Message bot’s Message was.

I’ve done one like this already.

In my New Message, I have a few keywords

And in my Send Message tasks I have applied filters to filter on the New Message > Message

File.json to Bot
(John Jackson) #9

We just released a new feature that solves this problem (using JSON/XML data you get back from a ‘Send a Webhook’ task).

It’s called a data output. If you add a (new) webhook action from now on, you’ll see an extra output, data. That will hold a representation of any structured data that’s found in the response, and you can edit that output to get to whatever data you need.

The docs for this new feature are here.

It’s worth pointing out that if the data contains arrays, you can use our collections notation to do things like sum all values, create a list of values, find the last value, etc…

Good luck with it and I hope it helps you build out your flows!

2 Likes
(Vova Nest) #10

This is an incredibly useful feature, but I have a case when double underscore doesn’t work – it seems to be just removed from the name of the variable and get no output as a result. See attached. Please help…

(Karen Barker) #11

Hi Vova,

I see you forwarded this into our support team as well and we have replied there. Don’t worry about the double underscores being removed in the messaging view, this is normal. :slight_smile:

(Vova Nest) #12

Thank you, Karen. I just noticed that the contact properties are provided as an arrow, so I just added __ 0 __ to the string and that solved the issue. Sorry for stealing your time.

1 Like