Loop function or the like for performing actions?

#1

Hi Guys,
Is there a way to “Loop” a given action?
For example I am returning the following data rows from a webhook, and want to send the user a message for each “row” returned by the webhook.
Currently the flow only sends the first row. Example data below.
Thanks

Data
[
{
“ReplyType”: “msg”,
“ReplyText”: “insert text here”,
“ResponsePath”: “…”
},
{
“ReplyType”: “msg”,
“ReplyText”: “insert text2 here”,
“ResponsePath”: “…”
}
]

#2

Also how would you get the row count in order to know how many times to loop?

(Joe Hatch) #3

Are you using the Code Service to do this?

(Kellsey Shaw) #4

Hi there,

You would need to use the code service as Joe has pointed out there. This would enable you to get the number of rows and loop through the collection.

Alternatively, to this, if you know the maximum number of rows that there could be, you can set up a task for each possible row and use Data Outputs to populate the message details. Whenever there is no message content, the message can’t be sent so you won’t ever send a blank message.

#5

can the code service be used to send a message or card?

(John Jackson) #6

It can actually, you can either make the request with the Code service, or the Webhook & HTTP service.

Take a look at this thread, there’s a shared flow you can install that sends a call button, which is not one of our built-in features. This should give you an idea how to make any kind of custom (none Flow XO) Messenger API request.

#7

hmm, this seems to be messenger specific.
Can you send me just plain javascript that would perform the send a message action?
Something I could drop into the code service and test.
Appreciated.

(Kellsey Shaw) #8

Hi there,

You could just use the webhook (HTTP) service. And there’s a easy to follow Guide for the basic functions of the Telegram Bot API.

(John Jackson) #9

Sorry I think I misunderstood slightly. Your original request was to look through an array and send a message for each item? So you were thinking of using the code service to loop the array and send a message each time?

It’s not quite possible. You can of course loop the array with the code service and make a HTTP request to any messaging platform to send a message, but you would have to form the raw request yourself - you unfortunately can’t (yet) perform a send a message action from within the code service (although that would be nice).

You can use our collections notation on JSON data - so using {{ webhook.data # }} should give you the total number of items in the array. But if you were using the code service that would be easy enough anyway.

#10

John or Kellsey,
I am working on doing something similar as the OP. I would like to use the HTTP Service to get data in JSON format, that I can then parse and turn into questions? Here is a workflow example:

  1. Bot - Ask Initial question with options (A, B, C)
  2. HTTP Request Service - Send Answer of question 1 to Webhook
  3. Code Service - Store Webhook response data: [{“question1”: “Question 1 text”,“question2”:“Question 2 text”}]
  4. Bot - Ask question 1
  5. Attributes Service - Store answer to question 1
  6. Attributes Service - Store answer to questino 2
  7. And so on… with all questions from HTTP Request data
  8. Send all responses via HTTP Request to webhook and end session.

Is this doable?

Basically, I have a service already that would already have questions built and arranged. I just need a way to dynamically ask the questions via a bot.

(John Jackson) #11

You have all the tools you need to do this, except the ability to “do something n times”. What I mean is, you can’t sort of say, “ask each of the questions”. Instead, you’ll need to have some upper limit and expect up to, say, 5 questions.

Sounds like you have a good grasp of what your trigger and actions need to be, and it sounds fine.

What you might not have thought of, and could help you here, is our Flow service. Using this, you can split out the asking of a question into a separate flow, and then trigger it between 1-5 times. At the end of each flow, you can run the same flow again.

You’ll need to play around with variations on this but hopefully you get the idea and can go from here. Good luck :smiley:

#12

Thanks for the answer @johnjackson.

So let me see if I understand you correctly. You are saying that what I can do is actually begin the initial flow with a bot trigger and the flow would go something like this.
Trigger Bot -> direct message

  1. Bot Action -> ask question

  2. HTTP Request -> Send answer response

  3. Code Service -> get HTTP Request response and store it

  4. Flow Action -> trigger flow by keyword based off of HTTP Response data.

  5. New Flow Trigger -> Filter for keyword (from step 4)

  6. HTTP Request -> send keyword from flow trigger

  7. Code Service -> get HTTP Response store data (question)

  8. Bot -> ask question from step 7

  9. Attributes Service -> store Answer

  10. HTTP Request -> send answer with all other data

  11. Code Service -> Get HTTP Request store data
    11a. Bot Service -> Send message. Filter:Check stored data for ending this flow…

  12. Flow Service Action -> Trigger the same flow and pass through all answers

If what you are saying is correct, couldn’t I repeat steps 5-12 indefinitely and end the conversation based off of data returned by the HTTP request in step 11?

Is there a limit to how many times I can trigger the same flow? Also, if I trigger a flow from an action, is that considered another interaction?

1 Like
(Sune Kaae) #13

Also curious on the answer to this.

And related:

  • when I trigger a flow what variables can I pass along? Does the response path context get passed?
  • can I decide whether the initial flow should block or not while the triggered flow executes ?
(John Jackson) #14

@fastyzfr1 Broadly speaking I think these steps seem OK. Maybe you can repeat the steps indefinitely until you come across some empty data, then you know that you’ve reached the last one and can move on and finish the flow? That could easily work.

There’s no limit to how many times the same flow can be triggered, but each trigger does use an interaction, so you would need to be careful not to cause an infinite loop!

@suneaaae When you trigger the Flow Service the response path and other context does get passed along.

There isn’t a way to block the originating flow, although you could call another flow after the second flow (i.e. chain flows together).

(Sedat Eratan) #15

if your https request return multiple answer use thats, possible all messenger message types.Example my http rquest return 5 answer, send to customer 5 message.

Code use that:

let arr = [];

try {
var url = inputs.url;
var data=inputs.data;

arr = JSON.parse(data);
arr.forEach(function (item) {

 var opts = {
     uri: url,
     method: 'POST',
     json:item
 };

 utils.request(opts, function(err, resp, body) {
 if(err) { 
      return reject(err);
 }
     resolve(body);
 });

})
} catch(e) {
return reject(‘Input must be valid JSON’);
}

Inputs:

url -> https://graph.facebook.com/me/messages?access_token=“access_token”
data -> {{make_a_http_request.data}}

(Kellsey Shaw) #16

Hi Sedat,

You can’t use the Code service to do a HTTP request, sorry :slight_frown:

You can use the Webhook & HTTP Service to send a single webhook. If you know how many possible answers the user could give, then you can set up a bot > send message for each possible responses and filter them out based on whether you received the answer you were expecting to send the message in response to :thumbsup:

(Sedat Eratan) #17

You are not allowed to use code service with http request or you are saying that the usage is wrong

(Kellsey Shaw) #18

Hi Sedat,

The code service uses a sandbox environment so it’s not yet possible to send requests from the code service.

(John Jackson) #19

Please check the docs it is possible to make HTTP requests from the code service.

(Kellsey Shaw) #20

Hi Sedat,

Let me clear up a few things to get the points across clearly :slight_smile:

You can actually use the Code service to do a single request but it would be easier to do that with the Webhook service that we offer.

The request library that you can use in the code service is not the raw request library and so not all the functions are available.

So as yet, you can’t send multiple message requests from the code service. You wanted to send 5 answers’ didn’t you? So, you would be looking for the same solution as described in this earlier post? Perhaps, the same solution that was suggested to this post would work for you too :thumbsup:

1 Like