skip to Main Content

I have searched around and couldn’t find any kind of Facebook API for use with Java. Do any of you guys know how to do this?

2

Answers


  1. Their official API/SDK documentation lists multiple 3rd party java sdks. https://developers.facebook.com/docs/apis-and-sdks

    Login or Signup to reply.
  2. You can use this library to connect with Facebook messenger
    https://github.com/BotMill/fb-botmill

    Steps:
    Add dependency to pom file

    <dependency>
      <groupId>co.aurasphere.botmill</groupId>
      <artifactId>fb-botmill</artifactId>
      <version>2.0.0-RC3</version>
    </dependency>
    

    Then add following mapping to your web.xml

    <servlet>
        <servlet-name>myFbBot</servlet-name>
        <servlet-class>co.aurasphere.botmill.fb.FbBotMillServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myFbBot</servlet-name>
        <url-pattern>/myFbBot</url-pattern>
    </servlet-mapping>
    

    Create botmill.properties file in your classpath and add the your tokens.

    fb.page.token=<PAGE_TOKEN>
    fb.validation.token=<VALIDATION_TOKEN>
    

    the create a FbBotConfiguration below and put all your initial configuration (one time config) on the constructor. This will also initialize the fb authentication.

    @BotConfiguration
    public class MyBotConfiguration extends FbBotConfiguration {
    
        public MyBotConfiguration() {
    
            MessengerProfileApi.setGetStartedButton("get_started");
            MessengerProfileApi.setGreetingMessage("Hello!");
    
            List<PersistentMenu> persistentMenus = new ArrayList<PersistentMenu>();
            PersistentMenu persistentMenu = new PersistentMenu("default", false);
    
            persistentMenu.addCallToAction(ButtonFactory.createPostbackButton("Menu 1", "menu1"));
            persistentMenu.addCallToAction(ButtonFactory.createPostbackButton("Menu 2", "menu2"));
    
            CallToActionNested theNestedMenu = new CallToActionNested("Menu 3 Nested");
            theServices.addCallToActionButton(ButtonFactory.createPostbackButton("Nested1", "nested1"));
            theServices.addCallToActionButton(ButtonFactory.createPostbackButton("Nested2", "nested2"));
            theServices.addCallToActionButton(ButtonFactory.createPostbackButton("Nested3", "nested3"));
            persistentMenu.addCallToAction(theNestedMenu);
    
            persistentMenus.add(persistentMenu);
    
            MessengerProfileApi.setPersistentMenus(persistentMenus);
    
            HomeUrl homeUrl = new HomeUrl();
            homeUrl.setInTest(true);
            homeUrl.setUrl("https://extensionlink.co");
            homeUrl.setWebviewHeightRatio(WebViewHeightRatioType.TALL);
            homeUrl.setWebviewShareButton(WebViewShareButton.SHOW);
    
            MessengerProfileApi.setHomeUrl(homeUrl);
    
        }
    
    }
    

    Then create the following class to add responses.

    @Bot
    public class MyBotClass extends FbBot {
    
        @FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="Hi",caseSensitive = true)
        public void sendMessage(MessageEnvelope envelope) {
            reply(new MessageAutoReply("Hello World!"));
        }
    }
    

    Configure Facebook App

    1. open developers.facebook.com/apps
    2. click the ‘Add a New App’ button
    3. enter the Display Name, e.g. messenger4j-fb-app
    4. select the Category: ‘Apps for Messenger’
    5. click the ‘Create App ID’ button
    6. Section ‘Token Generation’: Select your created FB Page, e.g. Demo

    Use ‘Page Access Token’ as and use a randomly generated string as ‘Verify Token’ in botmill.properties file

    Note: To connect with Facebook Messenger we need a SSL enabled server.
    So to test the application with Heroku fallow the following steps.

    1. First Login to Heroku and create an account.
    2. execute git push heroku master
    3. navigate back to ‘Messenger’
    4. Section ‘Webhooks’: Click the ‘Setup Webhooks’ button enter the
      Callback URL: .herokuapp.com/callback, e.g.
      demo-heroku-app.herokuapp.com/callback
    5. enter the generated Verify Token, e.g. retgdkfjsjklsklj34qdfs
    6. select the following Subscription Fields: messages,
      messaging_postbacks, messaging_optins, message_deliveries,
      message_reads, messaging_account_linking, message_echoes
    7. click the ‘Verify and Save’ button
    8. Section ‘Webhooks’: Select your created FB Page to subscribe your
      webhook to the page events, e.g. Demo
    9. click the ‘Subscribe’ button

    Test your new Chatbot

    1. open messenger.com
    2. search for your Chatbot using the name of your created FB Page, e.g.
      Demo
    3. send a message
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search