jgorset / facebook-messenger

Definitely the best way to make Bots on Facebook Messenger with Ruby
MIT License
962 stars 211 forks source link

500 error on listening path #177

Closed nicolrx closed 7 years ago

nicolrx commented 7 years ago

Hello Johannes, Sometimes I get 500 errors from Facebook Post requests on my listening route. I don't get any Facebook error printed in my logs so I can't debug it.

Basically it looks like this:

24 Sep 2017 17:46:26.497298 <158>1 2017-09-24T12:16:26.193556+00:00 heroku router - - at=info method=POST path="/listen" host=www.xxx.com request_id=a1214213-9de3-4122-bd67-ebb92d966e20 fwd="173.252.123.133" dyno=web.1 connect=0ms service=117ms status=500 bytes=441 protocol=https

Do you know how I can find the issue for these bad requests?

Thanks.

nicolrx commented 7 years ago

Also, is it possible to retrieve the Facebook error to perform an action?

sorich87 commented 7 years ago

Hi Nicolas!

Did you check if 500 errors on other paths are shown in the logs?

What do you mean by "retrieve the Facebook error to perform an action"?

nicolrx commented 7 years ago

Hi Ulrich! There is no 500 errors on other paths. Most of the time there is a Facebook error attached but sometimes I just get 500 errors out of the blue on my listening path.

What do I mean by "retrieve the Facebook error to perform an action" is to be able to write an if statement in order to perform some action when a particular error is raised. For example, some of the users remove the permissions but don't desactivate the chatbot. Thus, there are FacebookErrors raised: I would like to be able to desactivate the bot for their page in case they removed the permissions.

Thanks!

sorich87 commented 7 years ago

There is no 500 errors on other paths. Most of the time there is a Facebook error attached but sometimes I just get 500 errors out of the blue on my listening path.

I would suggest you create a path that would give a 500 error just for testing and see if the error is output in the logs when you visit that path. My guess is there's some config you need to enable for Heroku to log the 500 errors trace. So try a 500 error on another path to confirm. With Rails 4, you had to add the rails_12factor gem to your Gemfile to enable errors logging in Heroku. Not sure if it's still necessary for Rails 5 or if there's something else to do. Haven't used Heroku for a while.

What do I mean by "retrieve the Facebook error to perform an action" is to be able to write an if statement in order to perform some action when a particular error is raised.

You'll need to rescue the exceptions raised (http://blog.honeybadger.io/a-beginner-s-guide-to-exceptions-in-ruby/). There are plenty of error codes that Facebook can return and they're barely documented so you'll need to monitor your logs and rescue them as needed. What we're doing in our app is:

rescue Facebook::Messenger::Bot::AccessTokenError, Facebook::Messenger::FacebookError,
       Facebook::Messenger::Bot::PermissionError => e
  # code 190: Access Token not valid anymore
  # code 230: Requires pages_messaging permission to manage the object
  # subcode 460: Error validating access token: The session has been invalidated because
  # the user changed their password or Facebook has changed the session for security reasons.
  if e.code == 190 || e.code == 230 || e.subcode == 460
    # deactivate page and email user
  else
    raise
  end

Hope that helps.

Feel free to reach out anytime (even by email) if there's anything I can help with. I like what you're doing with Botletter!

nicolrx commented 7 years ago

That's exactly what I needed thanks a lot! I'm not really experienced to rescue exceptions raised, I will probably do a pull request to add this to the documentation.

I really like Botamp's new version too, it seems really promising! Let's stay in touch ;)