Facebook Bulk API: Request order is not preserved

317 views Asked by At

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?

1

There are 1 answers

0
Oles On

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/