-
Notifications
You must be signed in to change notification settings - Fork 24
Code Snippets
Alvin Reyes edited this page Mar 4, 2017
·
12 revisions
Library of awesome code snippets to help you create the perfect response of your Facebook bot.
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="Hi",caseSensitive = true)
public void sendMessage() {
reply(new MessageAutoReply("Hello World!"));
}
@FbBotMillController(eventType = FbBotMillEventType.MESSAGE_PATTERN, pattern = "(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)|(?i:home)")
public void replyWithQuickReply() {
reply(new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addTextMessageOnly("Text message with quick replies")
.addQuickReply("Quick reply 1", "Payload for quick reply 1").build(envelope);
}
});
@FbBotMillController(eventType=FbBotMillEventType.QUICK_REPLY_MESSAGE, quickReplyPayload="useful_yes")
public void sendMessage() {
reply(new MessageAutoReply("Hello World!"));
}
@FbBotMillController(eventType=FbBotMillEventType.LOCATION)
public void sendMessage() {
reply(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);
}
});
@FbBotMillController(eventType=FbBotMillEventType.ANY)
public void sendMessage() {
reply(new MessageAutoReply("Hello World!"));
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="Hi",caseSensitive = true)
public void sendMessage() {
reply(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);
}
});
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="list template")
public void sendMessage() {
reply(new AutoReply() {
@Override
public FbBotMillResponse createResponse(MessageEnvelope envelope) {
return ReplyFactory.addListTemplate()
.addElement("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"))
.endElement()
.addElement("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")).endElement()
.addElement("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")).endElement()
.addElement("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")).endElement()
.addButton(ButtonFactory.createPostbackButton("View more", "view")).build(envelope);
}
});
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="generic template")
public void sendMessage() {
reply(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);
}
});
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="airline boarding pass template")
public void sendMessage() {
reply(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);
}
});
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="airline checkin template")
public void sendMessage() {
reply(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);
}
});
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="airline itinerary template")
public void sendMessage() {
reply(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);
}
});
}
@FbBotMillController(eventType=FbBotMillEventType.MESSAGE, text="airline flight update template")
public void sendMessage() {
reply(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);
// or
ReplyFactory.addGenericTemplate()
.addElement("A simple Button Template with a Buy Button")
.addButton(ButtonFactory.createBuyButton("buy_button_payload")
.setPaymentSummary("USD",PaymentType.FIXED_AMOUNT,"BotMill.io")
.addPriceLabel("A price label", "2")
.setTestPayment(true)
.addRequestedUserInfo(RequestedUserInfo.CONTACT_PHONE)
.build()).endElement().
build(envelope);
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);
}
}
});
ReplyFactory.addTextMessageOnly("textmessage").build(envelope);
ReplyFactory.addAudioAttachment("audiourl").build(envelope);
ReplyFactory.addFileAttachment("fileurl").build(envelope);
ReplyFactory.addImageAttachment("imageurl").build(envelope);
ReplyFactory.addVideoAttachment("videourl").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!");
List<String> testers = new ArrayList<String>();
testers.add(mockId);
PaymentSettings settings = new PaymentSettings();
settings.setPaymentTesters(testers);
settings.setPaymentDevModeAction(PaymentDevModeAction.ADD);
settings.setPublicKey("-----BEGIN PGP PUBLIC KEY BLOCK-----***\n-----END PGP PUBLIC KEY BLOCK-----\n");
FbBotMillThreadSettingsConfiguration.addPaymentSettings(settings);
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