Categorygithub.com/jensdevloo/fbmessenger
modulepackage
1.1.7
Repository: https://github.com/jensdevloo/fbmessenger.git
Documentation: pkg.go.dev

# README

Package fbmessenger

CircleCI GoDoc

Go (golang) package for writing bots on the Facebook Messenger Platform.

Key Features

  • Fluent API makes building messages easy.
  • Timeoutable, cancellable requests using context.Context.
  • Designed for use with one or many subscribed pages.

Installation

go get gopkg.in/ekyoung/fbmessenger.v1

Quick Start

The primary types in the package are CallbackDispatcher and Client. CallbackDispatcher is used to handle the callbacks Facebook sends to your webhook endpoint. Client is used to send messages and to get user profiles.

CallbackDispatcher

Unmarshal the json received at your webhook endpoint into an instance of type Callback.

cb := &fbmessenger.Callback{}
err := json.Unmarshal(requestBytes, cb)

Use type CallbackDispatcher to route each MessagingEntry included in the callback to an appropriate handler for the type of entry. Note that due to webhook batching, a handler may be called more than once per callback.

dispatcher := &fbmessenger.CallbackDispatcher{
	MessageHandler: MessageReceived
}

err := dispatcher.Dispatch(cb)

Callback handlers should have a signature mathing the MessageEntryHandler type.

func MessageReceived(cb *fbmessenger.MessagingEntry) error {
	//Do stuff
}

Client

Create a Client to make requests to the messenger API.

client := fbmessenger.Client{}

There are structs for the different types of messages you can send. The easiest way to create them is with the fluent API.

request := fbmessenger.TextMessage("Hello, world!").To("USER_ID")

Then send your request and handle errors in sending, and errors returned from Facebook.

response, err := client.Send(request, "YOUR_PAGE_ACCESS_TOKEN")
if err != nil {
	//Got an error. Request never got to Facebook.
} else if response.Error != nil {
	//Request got to Facebook. Facebook returned an error.
} else {
	//Hooray!
}

Get a user's profile using their userId.

userProfile, err := client.GetUserProfile("USER_ID", "YOUR_PAGE_ACCESS_TOKEN")

For more control over requests (timeouts, etc.) use the *WithContext version of the above methods.

ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
response, err := client.SendWithContext(ctx, request, "YOUR_PAGE_ACCESS_TOKEN")
userProfile, err := userProfileGetter.GetUserProfileWithContext(ctx, "USER_ID", "YOUR_PAGE_ACCESS_TOKEN")

Inspiration

Some ideas where pulled from Go Client Library Best Practices by Jack Lindamood.

# Functions

ButtonTemplateMessage is a fluent helper method for creating a SendRequest containing text and buttons to request input from the user.
GenericTemplateMessage is a fluent helper method for creating a SendRequest containing a carousel of elements, each composed of an image attachment, short description and buttons to request input from the user.
ImageDataMessage is a fluent helper method for creating a SendRequest containing a message with an image attached by uploading the bytes of the image.
ImageMessage is a fluent helper method for creating a SendRequest containing a message with an image attached using the URL of the image.
LocationReply is a fluent helper method for creating a QuickReply with content type "location".
PostbackButton is a fluent helper method for creating a button with type "payload" for use in a message with a button template or generic template attachment.
ReceiptTemplateMessage is a fluent helper method for creating a SendRequest containing a detailed order confirmation.
SavedImageMessage is a fluent helper method for creating a SendRequest containing a message with an image attached using the identifier of the image asset.
SavedVideoMessage is a fluent helper method for creating a SendRequest containing a message with an video attached using the identifier of the video asset.
TextMessage is a fluent helper method for creating a SendRequest containing a text message.
TextReply is a fluent helper method for creating a QuickReply with content type "text".
TextReplyWithImage is a fluent helper method for creating a QuickReply with content type "text" and an attached image.
URLAction is a fluent helper method for creating an action with type "web_url" for use in a message with generic template attachment.
URLButton is a fluent helper method for creating a button with type "web_url" for use in a message with a button template or generic template attachment.
VideoMessage is a fluent helper method for creating a SendRequest containing a message with an video attached using the URL of the video.
WebviewButtonTemplateMessage is a fluent helper method for creating a SendRequest containing text and webview buttons to request input from the user.
WebviewURLButton is a fluent helper method for creating a webview button with type "web_url" for use in a message with a button template or generic template attachment.

# Structs

Action represents a default action for element in a structured message using the generic template.
Address represents a physical mailing address.
Attachment is used to build a message with attached media, or a structured message.
Button represents a single button in a structured message using the button template.
ButtonPayload is used to build a structured message using the button template.
Callback is the top level structure that represents a callback received by your webhook endpoint.
CallbackAttachment holds the type and payload of an attachment sent by a user.
CallbackAttachmentPayload holds the URL of a multimedia attachment, or the coordinates of a location attachment sent by the user.
CallbackDispatcher routes each MessagingEntry included in a callback to an appropriate handler for the type of entry.
CallbackMessage represents a message a user has sent to your page.
CallbackQuickReply holds the developer defined payload of a quick reply sent by the user.
Client is used to send messages and get user profiles.
Coordinates holds the latitude and longitude of a location.
DataPayload is used to hold the bytes of a resource (image, file, etc.) to upload and attach to a message.
Delivery holds information about which of the messages that you've sent have been delivered.
Entry
Entry is part of the common format of callbacks.
GenericElement represents one item in the carousel of a generic template message.
GenericPayload is used to build a structured message using the generic template.
Message can represent either a text message, or a message with an attachment.
MessagingEntry is an individual interaction a user has with a page.
OptIn holds the data defined for the Send-to-Messenger plugin.
Postback holds the data defined for buttons the user taps.
Principal holds the Id of a sender or recipient.
QuickReply represents a quick reply to a message.
ReceiptAdjustment represents discounts applied to a receipt.
ReceiptElement represents a line item for one purchased item (not tax or shipping) on a receipt.
ReceiptHeader holds just the top level fields for a ReceiptPayload.
ReceiptPayload is used to build a structured message using the receipt template.
ReceiptSummary represents the line items for totals and additional costs (tax and shipping) on a receipt.
Recipient identifies the user to send to.
ResourcePayload is used to hold the URL of a resource (image, file, etc.) to attach to a message.
SavedAssetPayload is used to hold the identifier of an already saved asset (image, file, etc.) to attach to a message.
SendError indicates an error returned from Facebook.
SendRequest is the top level structure for representing any type of message to send.
SendResponse is returned when sending a SendRequest.
UserProfile represents additional information about the user.
WebviewButton represents a single webview button in a structured message using the button template.
WebviewButtonPayload is used to build a structured message using the button template.

# Type aliases

MessageEntryHandler functions are for handling individual interactions with a user.