0

I am currently using the facebook bulk api to send multiple messages to users (messenger api) with the following request (access tokens and user ids are censored):

curl \                                                                   
-F "access_token=XXXXXXX" \
-F 'batch=[{"method":"POST", "relative_url":"me/messages","body":"message=%7B%27text%27%3A+%27AAA%27%7D&recipient=%7B%27id%27%3A+XXXXXXX%7D"}, {"method":"POST", "relative_url":"me/messages","body":"message=%7B%27text%27%3A+%27BBB%27%7D&recipient=%7B%27id%27%3A+XXXXXXX%7D"}, {"method":"POST", "relative_url":"me/messages","body":"message=%7B%27text%27%3A+%27CCC%27%7D&recipient=%7B%27id%27%3A+XXXXXXX%7D"}]' \                                            
https://graph.facebook.com/

What I expect are three messages, in order "AAA", "BBB", "CCC". When executing the query multiple times, the messages are delivered in random order which makes using the bulk api for sending multiple messages to one user pretty useless.

If I understand the paragraph cited below correctly, the requests should be executed in order, which obviously is not the case.

The ordering of responses correspond with the ordering of operations in the request, so developers should process responses accordingly to determine which operations were successful and which should be retried in a subsequent operation.

Is there anything I am doing wrong or is this an error on facebook's side?

Birne94
  • 636
  • 8
  • 21
  • That is expected. The order of responses you get is in the right order. The order in which the calls are made is random or parallel. You can't control that – WizKid Jan 03 '17 at 16:54
  • @WizKid I see, so there is no way of sending multiple messages in a single request then? I thought this would be a nice way to reduce the amount of requests sent and shorten delays between consecutive messages. – Birne94 Jan 03 '17 at 16:58
  • Not that I know of – WizKid Jan 03 '17 at 16:59

1 Answers1

0

By default, the operations specified in the batch API request are independent - they can be executed in arbitrary order on the server and an error in one operation does not affect execution of other operations.

Often, the operations in the request are dependent - for example, the output of one operation may be used in the input of the next operation. The graph Batch API has support for this using the JSONPath expression format (http://code.google.com/p/jsonpath/). The JSONPath expression format provides a simple way to reference the data in a JSON object. In order to reference the results of a previous operation in another operation, you need to give a name to the previous (parent) operation, and then reference it inside the query string parameter or form post parameter using the JSONPath format. The syntax for embedding a JSONPath expression in a query string or form post parameter is {result=(parent operation name):(JSONPath expression)}. Note that for security reasons filter and script JSONPath constructs are not allowed in the JSONPath expression.

Example

curl \
   -F 'access_token=...' \
   -F 'batch=[{ "method":"GET","name":"get-friends","relative_url":"me/friends?limit=5",},{"method":"GET","relative_url":"?ids={result=get-friends:$.data.*.id}"}]' \
   https://graph.facebook.com/
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Oles
  • 11
  • 1