Skip to content

Code Snippets

Alvin Reyes edited this page Dec 30, 2016 · 12 revisions

Library of awesome code snippets to help you create the perfect response of your Facebook bot.

Events

Message Event

addActionFrame(new MessageEvent("text message"), new MessageAutoReply("simple text message"));

Pattern Event

addActionFrame(new MessagePatternEvent(Pattern.compile("(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)"), new MessageAutoReply("simple text message"));

Quick Reply Event

addActionFrame(new QuickReplyMessageEvent("useful_yes"), new AutoReply() {
		@Override
		public FbBotMillResponse createResponse(MessageEnvelope envelope) {
			return ReplyFactory.addTypingAction(TypingAction.TYPING_ON).build(envelope);
		}
	}
});

Location Event

AnyEvent

Template

Button Template

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);
	}
});

List Template

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);
	}
});

Generic Template

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);
	}
});

Airline Boarding Pass Template

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);
	}
});

Airline Checkin Template

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);
	}

});

Airline Itinerary Template

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);
	}
});

Airline Flight Update Template

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

URL Button

Postback Button

Call Button

Share Button

Buy Button

Log In Button

Log Out Button

Quick Reply

Replies

Location

Sender Action

//	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);
		}
	}
});

Content Type

Text Message

Audio Attachment

File Attachment

Image Attachment

Video Attachment

Web View

Thread Settings

Account Linking Url

Domain Whitelisting

Get Started Button

Greeting Text

Payment Settings

Persistent Menu

User Profile Retrieval

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);
}
});
Clone this wiki locally