Skip to content
This repository has been archived by the owner. It is now read-only.
RestFB Messenger
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper Updated Gradle version Nov 17, 2017
restfbmessenger-core/src Upgraded to latest restfb 2.0.0 Dec 18, 2017
samples Preparing 1.3.0 release Apr 27, 2017
LICENSE.txt Updated docs Oct 5, 2016 Update Aug 29, 2018
build.gradle 2.2.1 release Dec 18, 2017
gradlew Preparing 1.2.0 release Feb 13, 2017

RestFB Messenger

Gitter Maven Central Javadocs License

This project is not actively maintained. PR's are welcome and change of ownership considered.

RestFB Messenger is a Java library for implementing chatbots on the Facebook Messenger Platform.

It utilizes the RestFB client for the low level communication with the Facebook Graph API. It also uses the RestFB Java types for input arguments and return types.

The RestFB Messenger library does basically all the low level plumbing and provides the user a high level API to implement the chatbot.

The user has to implement the com.github.marsbits.restfbmessenger.webhook.CallbackHandler interface or extending the convenient com.github.marsbits.restfbmessenger.webhook.AbstractCallbackHandler like below:

public class MyCallbackHandler extends AbstractCallbackHandler {

    public void onMessage(Messenger messenger, MessagingItem messaging) {
        String senderId = messaging.getSender().getId();
        IdMessageRecipient recipient = new IdMessageRecipient(senderId);
        MessageItem message = messaging.getMessage();
            format("Hi human, I received your message: %s", message.getText()));

    public void onPostback(Messenger messenger, MessagingItem messaging) {
        // TODO implement..

    // TODO override other methods the chatbot need to respond to..


Add the restfbmessenger-core dependency to your project:




dependencies {
    compile "com.github.marsbits.restfbmessenger:restfbmessenger-core:2.2.1"

If you want to use RestFB Messenger in a Spring Boot application see the Spring Boot configuration instead.


Servlet 2.5/3 web.xml

In a standard webapp the WebhookServlet can be configured in the web.xml like:

        <param-value>[fully-qualified class name of the MessengerProvider]</param-value>


The messengerProviderClass init param must point to a custom class implementing the com.github.marsbits.restfbmessenger.MessengerProvider to provide the Messenger instance to the WebhookServlet.

See the RestFB Messenger Echo App Engine sample for a full sample using the web.xml configuration.

Servlet 3 @WebListener

In a Servlet 3 environment the WebhookServlet can also be configured programmatically like:

public class EchoInitializer implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        Messenger messenger = new EchoMessengerProvider().getMessenger();
        WebhookServlet webhookServlet = new WebhookServlet(messenger);
        ServletRegistration.Dynamic webhook =
                event.getServletContext().addServlet("WebhookServlet", webhookServlet);

    public void contextDestroyed(ServletContextEvent sce) {

Another option is to extend the com.github.marsbits.restfbmessenger.webhook.WebhookServlet and annotate it with the @WebServlet annotation.

See the RestFB Messenger Echo Servlet 3 sample for a full sample using a @WebListener to do the configuration programmatically.


When using Spring one option is to simply configure the WebhookServlet like the previous examples.

In a Spring environment the Messenger bean will typically be created manually in the Spring application context and use other injected beans as well.

In that case the MessengerProvider implementation must find a way to retrieve the Messenger instance from the Spring application context.

Another option is to create a custom controller and use that instead of the WebhookServlet. This custom controller should then listen to Facebook callbacks and act on them appropriately.

This could look like:

public class EchoWebhookController {

    private static final Logger logger = Logger.getLogger(EchoWebhookController.class.getName());

    private Messenger messenger;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<String> get(
            @RequestParam(HUB_MODE_PARAM_NAME) String mode,
            @RequestParam(HUB_VERIFY_TOKEN_PARAM_NAME) String verifyToken,
            @RequestParam(HUB_CHALLENGE_PARAM_NAME) String challenge) {"Validating webhook...");
        if (HUB_MODE_SUBSCRIBE_VALUE.equals(mode) && messenger.verifyToken(verifyToken)) {
  "Validating webhook succeeded");
            return new ResponseEntity<>(challenge, HttpStatus.OK);
        } else {
            logger.warning("Validating webhook failed");
            return new ResponseEntity<>("Failed validation. Make sure the validation tokens match.",

    @RequestMapping(method = RequestMethod.POST)
    public void post(
            @RequestHeader(HUB_SIGNATURE_HEADER_NAME) String signature,
            @RequestBody String payload) {"Webhook received");
        try {
            messenger.handleCallback(payload, signature);
        } catch (Exception e) {
            logger.severe(format("Exception handling webhook: %s", e.getMessage()));
            throw e;

See the RestFB Messenger Echo Spring sample for a full sample using a custom Spring controller.

Spring Boot

Add the restfbmessenger-spring-boot-starter dependency to your project:




dependencies {
    compile "com.github.marsbits.restfbmessenger:restfbmessenger-spring-boot-starter:2.2.1"

The restfbmessenger-spring-boot-starter will automatically add the restfbmessenger-core dependency and trigger the auto configuration to create the com.github.marsbits.restfbmessenger.Messenger instance and register the com.github.marsbits.restfbmessenger.webhook.WebhookServlet servlet.

Only a class implementing the CallbackHandler need to be added to the Spring context like:

public class MyCallbackHandler extends AbstractCallbackHandler {

    public void onMessage(Messenger messenger, MessagingItem messaging) {
        // TODO implement..

The auto configuration will automatically hook the CallbackHandler in the Messenger instance.

The following properties can be specified inside your file:

  enabled: true # Enable RestFB Messenger
  verify-token: # The Facebook verify token (required the verify the webhook)
  access-token: # The Facebook access token (required to send messages)
  app-secret: # The Facebook app secret (if not provided the payload signature will not be validated; useful in e.g. dev environment)
  api-version: v2.8 # The Facebook api version
    enabled: true # Enable the webhook servlet
    path: /webhook # The path of the webhook servlet

See the RestFB Messenger Echo Spring Boot sample for a full sample using the RestFB Messenger Spring Boot Starter.


The RestFB Messenger library is released under version 2.0 of the Apache License.

You can’t perform that action at this time.