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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With