Facebook Messenger bot object structure for java

不羁的心 提交于 2019-12-06 03:57:35

问题


Has anyone created an open source project that exposes the facebook messenger bot API in java? (or another language I could convert?)

Essentially an object hierarchy for the stack found in: https://developers.facebook.com/docs/messenger-platform/send-api-reference

I'd rather not just use JsonObjects, etc. nor Maps to extract the incoming JSON chat messages or to build the outgoing structured chat replies. If an open source project for this exists -- I have not found it.


回答1:


Take a look at FaceBot. The goal of FaceBot is making the Facebook's Messenger Platform easier: with FaceBot, you only need less than 5 lines of code to set up your own Messenger bot.

Here's an example:

public class MyFaceBotBehavior extends AbstractFaceBot {

  public void defineBehavior() {
     // Setting my tokens from Facebook (page token and validation token for webhook).
     FaceBotContext.getInstance().setup("myFacebookPageToken", "myFacebookWebhookValidationToken");

     // Defining a bot which will reply with "Hello World!" as soon as I write "Hi"
     addActionFrame(new MessageEvent("Hi"),
          new MessageAutoReply("Hello World!"));
 }
}

If you have questions or need help, feel free to contact me (I'm the developer).




回答2:


With the open source project messenger4j you will get all you need.

It's an easy to use Java library for building chatbots on the Messenger Platform.

It provides a rich builder API to construct the outgoing messages. Furthermore it parses the inbound messages to specific java objects and automatically detects their type. For each message type or event you can register corresponding event handlers.

Receiving:

String payload = ... // callback request body
String signature = ... // 'X-Hub-Signature' request header

// JDK 8 version
MessengerReceiveClient receiveClient = MessengerPlatform.newReceiveClientBuilder("APP_SECRET", "VERIFICATION_TOKEN")
        .onTextMessageEvent(event ->  System.out.printf("%s: %s", event.getSender().getId(), event.getText()))
        .build();

// JDK 7 version
MessengerReceiveClient receiveClient = MessengerPlatform.newReceiveClientBuilder("APP_SECRET", "VERIFICATION_TOKEN")
        .onTextMessageEvent(new TextMessageEventHandler() {
            @Override
            public void handle(TextMessageEvent event) {
                System.out.printf("%s: %s", event.getSender().getId(), event.getText());
            }
        })
        .build();

receiveClient.processCallbackPayload(payload, signature);

Sending (simple):

MessengerSendClient sendClient = MessengerPlatform.newSendClientBuilder("PAGE_ACCESS_TOKEN").build();
sendClient.sendTextMessage("RECIPIENT_ID", "Hi there, how are you today?");

Sending (complex):

ReceiptTemplate receipt = ReceiptTemplate.newBuilder("Stephane Crozatier", "12345678902", "USD", "Visa 2345")
        .orderUrl("http://petersapparel.parseapp.com/order?order_id=123456")
        .timestamp(1428444852L)
        .addElements()
            .addElement("Classic White T-Shirt", 50F)
                .subtitle("100% Soft and Luxurious Cotton")
                .quantity(2)
                .currency("USD")
                .imageUrl("http://petersapparel.parseapp.com/img/whiteshirt.png")
                .toList()
            .addElement("Classic Gray T-Shirt", 25F)
                .subtitle("100% Soft and Luxurious Cotton")
                .quantity(1)
                .currency("USD")
                .imageUrl("http://petersapparel.parseapp.com/img/grayshirt.png")
                .toList()
            .done()
        .addAddress("1 Hacker Way", "Menlo Park", "94025", "CA", "US").street2("").done()
        .addSummary(56.14F).subtotal(75.00F).shippingCost(4.95F).totalTax(6.19F).done()
        .addAdjustments()
            .addAdjustment()
                .name("New Customer Discount")
                .amount(20.00F)
                .toList()
            .addAdjustment()
                .name("$10 Off Coupon")
                .amount(10.00F)
            .toList()
        .done()
        .build();

sendClient.sendTemplate("RECIPIENT_ID", receipt);

BTW: I've built it.




回答3:


I am currently working on a bot framework in java called JBot and the fb portion is currently under development but the Slack part is done and is used by several developers already.



来源:https://stackoverflow.com/questions/36921056/facebook-messenger-bot-object-structure-for-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!