Facebook Messenger Webhook continuously sending messages - facebook

I am learning to work with Facebook Messenger API. I have successfully setup a webhook, I have subscribe to the application. But when I send message to my Page I receive multiple instances of the same message like a burst.
I am storing messages which I send from my application when it is receive at the Webhook.
A view of my database look like in the screenshot below.
http://prntscr.com/au3emz
I am guessing it may be because the message is still unread? Just a wild guess may be someone else know for sure. I tried exact example of the Facebook Office (Node) and it is happening there as well.
Thanks.

When someone send a message to your Facebook Page, Facebook transfer that message to your Webhook address and waits for a http response like OK. And it keeps sending the same message to your webhook until the response arrives.
From Facebook webhook docs (Webhook docs;
Required 200 OK Response
When you receive a webhook event, you must always return a 200 OK HTTP response. The Messenger Platform will resend the webhook event every 20 seconds, until a 200 OK response is received. Failing to return a 200 OK may cause your webhook to be unsubscribed by the Messenger Platform.
A delivery receipt will automatically be sent to the user after your webhook has acknowledged the message. You can also use Sender Actions to notify that the message has been seen.
You can see that all of those messages have the same timestamp.
As a solution; you can define a message queue to store those messages, and you add message to the queue if there is no such message in queue with the message sender id and timestamp.
Hope this helps,

This is often caused if you subscribe to the echo message callback event.
From the documentation;
This callback will occur when a message has been sent by your page.
What this implies is that you will get back a copy of any message your bot sends. From your code, this will cause a form of infinite loop; you would keep receiving Response 1.
You can easily confirm this by inspecting the value of text; you'll find it's also Response 1 and on inspecting the entire payload it will have the field "is_echo":true.
Solution:
Edit your page subscription to exclude message_echoes

I had the same issue with my test application. I am still new to Messenger API and my solution may be naive.
Initially, the code was like:
if (text) {
sendTextMessage(sender, 'Response 1');
} else {
queryDB(
sendTextMessage(sender, 'Response 2');
)
}
res.sendStatus(200);
Which kept sending 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 have to sendStatus always after sending a message.

I detect that betwen facebook invocation and my response with 200 statuscode my code was waiting 2 seconds, my code is syncronous, thus , my total code responds with 200 code 2 seconds after..
Many probes results for me that if i dont send 200 status code to facebook in one second facebook resend message to webhook. For me the aolution was analyze the incoming message and obtain the timestamp key (A), then i get a timestamp.from a database (B). compare A==B.. if is true, i do nothig and ends.my webhook processing, if is false then write in the database the new timestamp (A) and continue with webhook proceasing.

Solution
I sent a subscription request again to my application to stop these messages.
So essentially what I experienced is that webhook re-subscribe app after having received message will stop any further receives until next message.
This is although the solution the core of "Why this happens?" is not known yet. Will be glad if someone can contribute.
Thanks.

Related

Google Smart Home - Report State Real-Time Feed

I have an application that receives and run correctly the SYNC, QUERY, EXECUTE sent by Google Actions.
I too configured Report State in HomeGraph console. It works fine, but when I send a change report command to HomeGraph and it's accepted, nothing is updated in the Google home app, is this right? is the correct behavior?
I forget to do something else?
My sync response sends the device with willRportSate: true
tks in advance
If you are getting an HTTP 200 response from your report state command, you're doing it correctly.
I have recently implemented HomeGraph to my action. I found that if there is something wrong with the request google will return some error in the form of JSON with a different status code.
But as it is said above, if you got 200 OK, then everything is OK from your side. For my requests, I get a response with the uuid that I give with a 200 OK status code.

GitHub's Webhooks is sending nonsense events

I have a webhook on issues for github and I update the body of the issue from within my application using PATCH request. The rate is not very fast, like a request every few seconds. Most of the times it works, yet some of the times GitHub sends my hook a nonsense payload which has the body text before my edit in both body and changes.body.from fields as if there was a change which changed nothing. At the same time:
The responce to my PATCH displays new body correctly.
The issue itself on GitHub gets updated properly.
No follow up webhook procs with correct body happen.
Anybody has an idea what I could be hitting here?
Thanks in advance.

How to implement PayPal IPN listener (controller) in Play Framework Scala

I am currently using PayPal buttons in my application written in Scala (Play Framework).
Now I am trying to implement PayPal IPN instead of redirect.
According to PayPal document
https://developer.paypal.com/docs/classic/ipn/gs_IPN/
After the user finished the payment, the flow of events is:
PayPal posts a message to my application's listener with all sorts of variables.
My listener returns an empty HTTP 200 response.
My listener performs an HTTP POST to send the complete, unaltered notification back to PayPal.
PayPal sends a single word back - either VERIFIED or INVALID.
So, my controller's method should look like:
def payPalIpn() = Action { implicit request =>
.... Get all PayPal Variables .......
/* Send empty response with empty HTTP 200 response */
Ok("")
.... Make a POST request to PayPal site with all original PayPal's variables
}
Here is what I stumbled upon - How can I make a POST request after I return OK("")?
Is that even possible in Play?
Appreciate if someone can give me any idea.
Andrei.
This would be an excellent use case for an Akka Actor:
def payPalIpn() = Action { implicit request =>
.... Get all PayPal Variables .......
actorSystem.actorSelection("paypal-ipn-actor") ! ValidateIPN(request.body)
/* Send empty response with empty HTTP 200 response */
Ok("")
}
If I understand it correct, you want to make a POST request to PayPal in your action. For that you can use WS Client library from Play. Detailed tutorial is given at
https://www.playframework.com/documentation/2.4.x/ScalaWS

Facebook Realtime Updates - Dispute - Response

Noobish question: Are you supposed to respond to realtime updates (RTUs) somehow? Keep getting RTUs on the same dispute which FB has already refunded. So, should one echo something in the RTU callback? Just been through all the documentation I can find (again) and can't seem to find anything on response.
Yes, you need to return a 200. The content type or response body do not matter. Anything else results in Facebook retrying the callback until a 200 is received, so you'll get lots of spam

How can I send the same Twitter message multiple times?

I am trying to add the iOS 5 Twitter API to my app, and Twitter is giving me crap about sending the same direct message a second time.
Twitter response, HTTP response: 403
Response Data
{"error":"There was an error sending your message: Whoops! You already said that.","request":"\/1\/direct_messages\/new.json"}
Is there anyway to get around this so I can test my app?
Why don't you try sending a random message or the (stringified) time? It would be unique an each occasion, and it wouldn't require you to hack/work around/otherwise violate Twitter's this restirction.
You can tweet the same message multiple times if the text is never exactly the same.
For example, 1001tweets helps you to post multiples times some of your tweets. To do that, it keeps only the tweets containing a link and changes it using an URL shortener.