All Projects → marceloverdijk → restfbmessenger

marceloverdijk / restfbmessenger

Licence: Apache-2.0 license
RestFB Messenger

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to restfbmessenger

chatbot
A fast responsive, machine learning, conversational dialog engine for creating chat bots, written in Go.
Stars: ✭ 347 (+1726.32%)
Mutual labels:  chatterbot
chatterbot-weather
A ChatterBot logic adapter that returns information about the current weather
Stars: ✭ 27 (+42.11%)
Mutual labels:  chatterbot
Whatsapp Android App
This is sample code for layout for chatting app like Whatsapp.
Stars: ✭ 32 (+68.42%)
Mutual labels:  facebook-messenger-platform
FashionShopApp
Fashion Shop App : Flask, ChatterBot, ElasticSearch, Recommender-System
Stars: ✭ 28 (+47.37%)
Mutual labels:  chatterbot
chatterbot-live-example
[Maintainer Required] 📘 Notes to deploy chatterbot on Heroku cloud ☁️
Stars: ✭ 22 (+15.79%)
Mutual labels:  chatterbot
rtfmbot
Because we're all tired of answering questions when people should clearly RTFM.
Stars: ✭ 14 (-26.32%)
Mutual labels:  chatterbot
botnaut
Chatbot Framework for Facebook Messenger platform
Stars: ✭ 13 (-31.58%)
Mutual labels:  facebook-messenger-platform
Awesome Bots
The most awesome list about bots ⭐️🤖
Stars: ✭ 2,864 (+14973.68%)
Mutual labels:  facebook-messenger-platform
Facebook-Auto-Pilot
Automate common Facebook activities such as posting to groups and pages walls. Effortlessly post to multiple groups or pages.
Stars: ✭ 126 (+563.16%)
Mutual labels:  facebook-messenger-platform
messenger-bot
A Node bot server for the Facebook Messenger Platform
Stars: ✭ 23 (+21.05%)
Mutual labels:  facebook-messenger-platform
fbemissary
A bot framework for the Facebook Messenger platform, built on asyncio and aiohttp
Stars: ✭ 30 (+57.89%)
Mutual labels:  facebook-messenger-platform
hubot-messenger-bot
🔌 A Hubot adapter for Facebook Messenger Platform (Bots) with zero external dependencies.
Stars: ✭ 44 (+131.58%)
Mutual labels:  facebook-messenger-platform
facebook-messenger
Go (GoLang) package for Facebook Messenger API and Chat bot
Stars: ✭ 62 (+226.32%)
Mutual labels:  facebook-messenger-platform
Chatterbot
ChatterBot is a machine learning, conversational dialog engine for creating chat bots
Stars: ✭ 11,813 (+62073.68%)
Mutual labels:  chatterbot
Wechat Admin
Wechat Management System
Stars: ✭ 1,716 (+8931.58%)
Mutual labels:  chatterbot
YouTube-Tutorials--Italian
📂 Source Code for (some of) the Programming Tutorials from my Italian YouTube Channel and website ProgrammareInPython.it. This is just a small portion of the content: please visit the website for more.
Stars: ✭ 28 (+47.37%)
Mutual labels:  chatterbot
NLP-chatbot
A medical chatbot that asks patients about their health and books a doctor's appointment on demand.
Stars: ✭ 43 (+126.32%)
Mutual labels:  chatterbot
siPintar
Indonesian Chatbot built by Multi Layer Perceptron(Neural Network)
Stars: ✭ 37 (+94.74%)
Mutual labels:  chatterbot

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 {

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

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

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

Installation

Add the restfbmessenger-core dependency to your project:

Maven:

<dependency>
  <groupId>com.github.marsbits.restfbmessenger</groupId>
  <artifactId>restfbmessenger-core</artifactId>
  <version>2.2.1</version>
</dependency>

Gradle:

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.

Configuration

Servlet 2.5/3 web.xml

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

<servlet>
    <servlet-name>webhook</servlet-name>
    <servlet-class>com.github.marsbits.restfbmessenger.webhook.WebhookServlet</servlet-class>
    <init-param>
        <param-name>messengerProviderClass</param-name>
        <param-value>[fully-qualified class name of the MessengerProvider]</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>webhook</servlet-name>
    <url-pattern>/webhook</url-pattern>
</servlet-mapping>

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:

@WebListener
public class EchoInitializer implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        Messenger messenger = new EchoMessengerProvider().getMessenger();
        WebhookServlet webhookServlet = new WebhookServlet(messenger);
        ServletRegistration.Dynamic webhook =
                event.getServletContext().addServlet("WebhookServlet", webhookServlet);
        webhook.setLoadOnStartup(1);
        webhook.addMapping("/webhook");
    }

    @Override
    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.

Spring

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:

@Controller
@RequestMapping("/webhook")
public class EchoWebhookController {

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

    @Autowired
    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) {
        logger.info("Validating webhook...");
        if (HUB_MODE_SUBSCRIBE_VALUE.equals(mode) && messenger.verifyToken(verifyToken)) {
            logger.info("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.",
                    HttpStatus.FORBIDDEN);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public void post(
            @RequestHeader(HUB_SIGNATURE_HEADER_NAME) String signature,
            @RequestBody String payload) {
        logger.info("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:

Maven:

<dependency>
  <groupId>com.github.marsbits.restfbmessenger</groupId>
  <artifactId>restfbmessenger-spring-boot-starter</artifactId>
  <version>2.2.1</version>
</dependency>

Gradle:

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:

@Component
public class MyCallbackHandler extends AbstractCallbackHandler {

    @Override
    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 application.properties/application.yml file:

restfbmessenger:
  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
  webhook:
    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.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].