Facebook Messenger Webhook continuously sends messages

I am learning to work with the Facebook Messenger API. I successfully configured webhook, I signed up for the application. But when I send a message to my page, I get multiple instances of the same message as the package.

I save the messages that I send from my application when it receives in Webhook.

The view of my database looks in the screenshot below.

http://prntscr.com/au3emz

I suppose this could be because the message has not yet been read? Just a wild hunch, maybe someone else knows for sure. I tried exactly the example of Facebook Office (Node), and it happens there too.

Thanks.

+5
source share
4 answers

This often occurs if you are subscribed to an echo message callback event .

From the documentation;

This callback will occur when a message is sent by your page.

This means that you will return a copy of any message that your bot sends. From your code, this will cause an infinite loop form; You will receive Response 1 .

You can easily confirm this by checking the value of text ; you will find it also Response 1 and when checking the entire payload it will have the field "is_echo":true .

Solution: Edit the page subscription to exclude message_echoes

enter image description here

+2
source

I had the same problem with my test application. I'm still new to the Messenger API, and my solution may be naive.

Initially, the code looked like this:

 if (text) { sendTextMessage(sender, 'Response 1'); } else { queryDB( sendTextMessage(sender, 'Response 2'); ) } res.sendStatus(200); 

Which continued to send Response 1 forever. The correct code is:

 if (text) { sendTextMessage(sender, 'Response 1'); res.sendStatus(200); } else { queryDB( sendTextMessage(sender, 'Response 2'); res.sendStatus(200); ) } 

You must send Status always after sending the message.

+1
source

I found that betwen facebook invocation and my response with a status code of 200, my code expected 2 seconds, my code is synchronous, so my common code answers 200 code 2 seconds after ..
Many probes lead to the fact that if I do not send the 200 status code to facebook in one second, send the message again to webhook. For me, Evolution parsed the incoming message and got the timestamp key (A), then I get the timestamp. From the database (B). compare A == B .. if it is true, I do nothig and ends.my processing webhook, if it is false, then write a new timestamp (A) to the database and continue the webhook procedure.

+1
source

Decision

I again sent a subscription request to my application to stop these messages.

Thus, in essence, I experienced that the webhook intercept application after receiving the message will stop receiving further until the next message.

This, although the solution, is the core of "Why is this happening?" not yet known. We will be happy if someone can contribute.

Thanks.

0
source

Source: https://habr.com/ru/post/1247413/


All Articles