Welcome to the restfb cookbook.
Our cookbook provides recipes for common problems. With these solutions, you can solve problems a Facebook Graph API developer is often faced with. These snippets and explanations can help save time so you can focus on creating great applications.
Suggestions are very welcome. If you need a solution for a problem or discovered an interesting approach to a problem, let us know and open an issue on Github.
Post with detailed reactions
Comment types provide access to user reactions, but Facebook provides only
a complete summary by default. In this recipe, we show how the a post needs to be fetched to have all reaction types and their summaries available.
First, we need a special post type that extends the RestFB
Post type. Let’s call it
PostWithDetailedReactions. For every reaction type, we need a special field, and these fields are filled in automatically as soon as one performs the correct Graph API call. Reaction types are
haha, and so on. Please consult the Facebook reference for details.
Normally you define a
fields string, because you have to tell Facebook which fields should be filled in the object. Now, we have some new fields in the object above and we let Facebook itself rename the returned fields so Facebook can return the same field name several times. Of course, we don’t want the data of the same field twice, and therefore we filter the
reactions field for its type. The types are defined in the Facebook Graph API reference and we use all of them.
Now we can see which reaction type is used how often. Additionally, we have the reactions field without a type filter to see a complete summary. For example, this means we know that 5 reactions are returned: 1 is a
LIKE, 3 are
HAHA, and 1 is
The complete string looks like this. The
reactions part can easily be extracted into a method and enhance the fields on demand afterwards. But this depends on your custom use case.
Now, we have a new post type and we can extend the fields. It’s time to connect everything and make the call. The returned object is an instance of
PostWithDetailedReactions and the fields parameter is replaced with our extended version. There is almost no difference compared to a normal post and the
fetchObject method works the same.
The post object returned works like a normal post, but you can simply call
post.getReactionsHaha() and get a Reactions object that only contains the summary data of the
HAHA reactions. The other reaction types work the same, and you can see the method names in the
PostWithDetailedReactions object above. Don’t forget: if you need the complete summary, just call
getReactions() like you do with the normal
Set complete welcome screen at once
The messenger welcome screen is the entry point for a user to interact with your Facebook chat bot.
Because every bot is different, there are several options for the welcome screen. But sometimes you need to
set more than one option. Normally, you need to send the
get_started button payload, the greeting text, and the persistent menu as discrete calls. However, there is a way to perform these 3 calls all at once:
As you can see, the
greeting, and the
persistent_menu parameter are passed in a single call.