-
Notifications
You must be signed in to change notification settings - Fork 24
Code Snippets
Alvin Reyes edited this page Dec 31, 2016
·
12 revisions
Library of awesome code snippets to help you create the perfect response of your Facebook bot.
FBotContext.getInstance().setup(pageAccessToken, verifyCode);
addActionFrame(new MessageEvent("text message"), new MessageAutoReply("simple text message"));
addActionFrame(new MessagePatternEvent(Pattern.compile("(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)"), new MessageAutoReply("simple text message"));
addActionFrame(new QuickReplyMessageEvent("useful_yes"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addTypingAction(TypingAction.TYPING_ON).build(envelope);
}
}
});
addActionFrame(new LocationEvent(), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
Location loc = new Location(); // custom Location object
loc.setLatitude(((QuickReplyLocationPayload)envelope.getMessage().getAttachments().get(0).getPayload()).getCoordinates().getLatitude());
loc.setLongitude(((QuickReplyLocationPayload)envelope.getMessage().getAttachments().get(0).getPayload()).getCoordinates().getLongitude());
return ReplyFactory.addTextMessageOnly("Your Location " + loc.getLatitude() + " and " + loc.getLongitude()).build(envelope);
}
});
addActionFrame(new MessageEvent("button template"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addButtonTemplate("Test button template")
.addPostbackButton("postback button", "postback button payload")
.addPhoneNumberButton("phone number button", "+393541247844")
.addUrlButton("web url button", "https://github.com/BotMill/fb-botmill").build(envelope);
}
});
addActionFrame(new MessageEvent("list template"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addListTemplate()
.addElement(new ListTemplateElement("Classic T-Shirt Collection").setSubtitle("See all our colors")
.addButton(ButtonFactory.createUrlButton("View",
"https://peterssendreceiveapp.ngrok.io/collection"))
.setImageUrl("https://peterssendreceiveapp.ngrok.io/img/collection.png")
.setDefaultAction(ButtonFactory.createDefaultActionButton(
"https://peterssendreceiveapp.ngrok.io/shop_collection")))
.addElement(new ListTemplateElement("Classic White T-Shirt")
.setSubtitle("100% Cotton, 200% Comfortable")
.addButton(ButtonFactory.createUrlButton("Shop Now",
"https://peterssendreceiveapp.ngrok.io/shop?item=100"))
.setImageUrl("https://peterssendreceiveapp.ngrok.io/img/white-t-shirt.png")
.setDefaultAction(ButtonFactory.createDefaultActionButton(
"https://peterssendreceiveapp.ngrok.io/view?item=100")))
.addElement(new ListTemplateElement("Classic Blue T-Shirt")
.setSubtitle("100% Cotton, 200% Comfortable")
.addButton(ButtonFactory.createUrlButton("Shop Now",
"https://peterssendreceiveapp.ngrok.io/shop?item=101"))
.setImageUrl("https://peterssendreceiveapp.ngrok.io/img/blue-t-shirt.png")
.setDefaultAction(ButtonFactory.createDefaultActionButton(
"https://peterssendreceiveapp.ngrok.io/view?item=101")))
.addElement(new ListTemplateElement("Classic Black T-Shirt")
.setSubtitle("100% Cotton, 200% Comfortable")
.addButton(ButtonFactory.createUrlButton("Shop Now",
"https://peterssendreceiveapp.ngrok.io/shop?item=102"))
.setImageUrl("https://peterssendreceiveapp.ngrok.io/img/black-t-shirt.png")
.setDefaultAction(ButtonFactory.createDefaultActionButton(
"https://peterssendreceiveapp.ngrok.io/view?item=102")))
.addButton(ButtonFactory.createPostbackButton("View more", "view")).build(envelope);
}
});
addActionFrame(new MessageEvent("generic template", false), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addGenericTemplate().addElement("Generic Template Element 1")
.addPostbackButton("postback button", "postback button payload")
.addPhoneNumberButton("phone number button", "+393541247844")
.addUrlButton("web url button", "https://alvinjayreyes.com")
.setSubtitle("Subtitle of element 1").setRedirectUrl("www.alvinjayreyes.com").endElement()
.addQuickReply("Quick Reply 1", "Payload of Quick Reply 1").build(envelope);
}
});
addActionFrame(new MessageEvent("boarding pass airline template"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addAirlineBoardingPassTemplate("TEST TEMPLATE", "en_US")
.setThemeColor("#009023")
.addBoardingPass("Passenger 1", "13",
"http://seeklogo.com/images/A/Airport-logo-912173495E-seeklogo.com.gif",
"http://seeklogo.com/images/A/Airport-logo-912173495E-seeklogo.com.gif")
.addFlightInfo("19099922").setArrivalAirport("SR", "Siracusa")
.setDepartureAirport("CT", "Catania")
.setFlightSchedule(Calendar.getInstance(), Calendar.getInstance()).endFlightInfo()
.addAuxiliaryField("LABEL1", "VALUE1").addAuxiliaryField("LABEL2", "VALUE2")
.addSecondaryField("SEC1", "SEC2")
.setBarcodeImageUrl("http://www.qrstuff.com/images/sample.png")
.setHeaderImageUrl("http://seeklogo.com/images/A/Airport-logo-912173495E-seeklogo.com.gif")
.setHeaderTextField("HEADER TEXT").setQrCode("12345").setSeat("12")
.setTravelClass(TravelClass.FIRST_CLASS).endBoardingPass()
.addBoardingPass("Passenger 2", "213",
"http://seeklogo.com/images/A/Airport-logo-912173495E-seeklogo.com.gif",
"http://seeklogo.com/images/A/Airport-logo-912173495E-seeklogo.com.gif")
.addFlightInfo("19099922").setArrivalAirport("SR", "Siracusa")
.setDepartureAirport("CT", "Catania")
.setFlightSchedule(Calendar.getInstance(), Calendar.getInstance()).endFlightInfo()
.endBoardingPass().build(envelope);
}
});
addActionFrame(new MessageEvent("checkin airline template"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory
.addAirlineCheckinTemplate("Here's your checkin info", "en_US", "assaf", "www.aurasphere.co")
.addFlightInfo("1234").setArrivalAirport("SR", "SR").setDepartureAirport("BG", "BG")
.setFlightSchedule(Calendar.getInstance(), Calendar.getInstance()).endFlightInfo()
.build(envelope);
}
});
addActionFrame(new MessageEvent("itinerary airline template"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory
.addAirlineItineraryTemplate("Here's your itinerary", "en_US", "D0FQTK",
new BigDecimal(4032.54), "USD")
.setBasePrice(new BigDecimal(200.71)).addPassengerInfo("1", "Sarah Hum")
.addPassengerInfo("2", "Jeremy Goldberg").addFlightInfo("123", "123", "1", TravelClass.BUSINESS)
.setAircraftType("Boeing").setArrivalAirport("BG", "Bergamo")
.setDepartureAirport("CT", "Catania")
.setFlightSchedule(Calendar.getInstance(), Calendar.getInstance()).endFlightInfo()
.addPassengerSegmentInfo("1", "2", "14A", "Economy").addProductInfo("Cabin", "Coach")
.endPassengerSegmentInfo().addPriceInfo("Cabin", new BigDecimal(100))
.addPriceInfo("Ticket", new BigDecimal(200)).setTax(new BigDecimal(200))
.addQuickReply("OK", "OK").build(envelope);
}
});
addActionFrame(new MessageEvent("flight update airline template"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory
.addAirlineFlightUpdateTemplate("Your flight has an update", "en_US", "121212",
UpdateType.CANCELLATION)
.addFlightInfo("120").setArrivalAirport("U2", "Bergamo").setDepartureAirport("D12", "Catania")
.setFlightSchedule(Calendar.getInstance(), Calendar.getInstance()).endFlightInfo()
.build(envelope);
}
});
Buttons can be added to different templates. The framework has a class that represents a factory of different kinds of buttons that's available on the facebook messenger platform.
ReplyFactory.addButtonTemplate("This is a URL button")
.addButton(ButtonFactory.createUrlButton("I'm a URL button", "http://www.technowebhub.com"))
.addButton(ButtonFactory.createPostbackButton("Next up : PostBack", "botpostb_demo"))
.build(envelope);
ButtonFactory.createUrlButton("I'm a URL button", "http://www.technowebhub.com")
ButtonFactory.createPostbackButton("Next up : Url Button", "boturl_demo")
.addPhoneNumberButton("phone number button", "+393541247844")
.addShareButton()
ButtonFactory.createBuyButton(ButtonType type, String payload, PaymentSummary paymentSummary);
ButtonFactory.createLoginButton(String url);
ButtonFactory.createLogoutButton();
addActionFrame(new PostbackPatternEvent("search_menu"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addTextMessageOnly("Choose one").addQuickReply("Search", "keysearch").addQuickReply("Categories", "loadcateg").build(envelope);
}
});
addActionFrame(new PostbackPatternEvent("search_menu"), new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addTextMessageOnly("Share your location:").addQuickLocationReply("Send location").build(envelope);
}
});
// TypingAction.TYPING_ON OR TYPING_OFF, OR MARK_SEEN
addActionFrame(new MessagePatternEvent(Pattern.compile("(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)|(?i:home)")),
new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addTypingAction(TypingAction.TYPING_ON).build(envelope);
}
}
});
addActionFrame(new PostbackEvent("start"), new AutoReply() {
@Override
public FBotResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addGenericTemplate().addElement("Logon to BBank")
.setImage("http://www.free-icons-download.net/images/bank-logo-icon-70261.png")
.addLoginButton("https://www.technowebhub.com/login.php")
.endElement().build(envelope);
}
});
List<String> whitelist = new ArrayList<String>();
whitelist.add("https://www.technowebhub.com/");
FbBotMillThreadSettingsConfiguration.setWhiteListDomains(whitelist);
OR
FBotThreadSettingsConfiguration.addWhiteListDomain("https://www.technowebhub.com/login.php");
FBotThreadSettingsConfiguration.setGetStartedButton("Hello");
FBotThreadSettingsConfiguration.setGreetingMessage("Just little me giving you your daily dose of Knowledge!");
TBD
List<Button> buttons = new ArrayList<Button>();
buttons.add(ButtonFactory.createPostbackButton("Random Trivia", "randomtriv"));
buttons.add(ButtonFactory.createPostbackButton("My Score", "my_score"));
buttons.add(ButtonFactory.createPostbackButton("Trivia Quiz!", "quiz_start"));
FBotThreadSettingsConfiguration.setPersistentMenu(buttons);
addActionFrame(new MessageEvent("getprofile"), new AutoReply() {
@Override
public FBotResponse createResponse(MessageEnvelope envelope) {
FacebookUserProfile userProfile = getUserProfile(envelope.getSender().getId());
return ReplyFactory.addTextMessageOnly("Hi " + userProfile.getFirstName()).build(envelope);
}
});
FB-BotMill - Copyright (c) 2016 BotMill.io