messenger

package module
v0.0.0-...-01dea8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2016 License: MIT Imports: 9 Imported by: 0

README

Messenger GoDoc

This is a Go library for making bots to be used on Facebook messenger. It is built on the Messenger Platform. One of the main goals of the project is to implement it in an idiomatic and easy to use fashion.

You can find an example of how to use it here

Tips

  • Follow the quickstart guide for getting everything set up!
  • You need a Facebook development app, and a Facebook page in order to build things.
  • Use ngrok to tunnel your locally runnning bot so that Facebook can reach the webhook.

Breaking Changes

paked/messenger is a pretty stable library however, changes will be made which might break backwards compatibility. For the convenience of its users, these are documented here.

  • 20/5/16: Leaving the WebhookURL field blank in Options will yield a URL of "/" instead of a panic.
  • 4/5/16: The URL to use for the webhook is changable in the Options struct.

Inspiration

Messenger takes design cues from:

Documentation

Index

Constants

View Source
const (
	// ProfileURL is the API endpoint used for retrieving profiles.
	// Used in the form: https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>
	ProfileURL = "https://graph.facebook.com/v2.6/"
)
View Source
const (
	// SendMessageURL is API endpoint for sending messages.
	SendMessageURL = "https://graph.facebook.com/v2.6/me/messages"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action is used to determine what kind of message a webhook event is.

const (
	// UnknownAction means that the event was not able to be classified.
	UnknownAction Action = iota - 1
	// TextAction means that the event was a text message (May contain attachments).
	TextAction
	// DeliveryAction means that the event was a previous recipient reading their respective
	// messages.
	DeliveryAction
	// PostBackAction represents post call back
	PostBackAction
)

type Attachment

type Attachment struct {
	// Type is what type the message is. (image, video or audio)
	Type string `json:"type"`
	// Payload is the information for the file which was sent in the attachment.
	Payload Payload `json:"payload"`
}

Attachment is a file which used in a message.

type Delivery

type Delivery struct {
	// Mids are the IDs of the messages which were read.
	Mids []string `json:"mids"`
	// RawWatermark is the timestamp contained in the message of when the read was.
	RawWatermark int64 `json:"watermark"`
	// Seq is the sequence the message was sent in.
	Seq int `json:"seq"`
}

Delivery represents a the event fired when a recipient reads one of Messengers sent messages.

func (Delivery) Watermark

func (d Delivery) Watermark() time.Time

Watermark is the RawWatermark timestamp rendered as a time.Time.

type DeliveryHandler

type DeliveryHandler func(Delivery, *Response)

DeliveryHandler is a handler used for responding to a read receipt.

type Entry

type Entry struct {
	// ID is the ID of the batch.
	ID int64 `json:"id,string"`
	// Time is when the batch was sent.
	Time int64 `json:"time"`
	// Messaging is the events that were sent in this Entry
	Messaging []MessageInfo `json:"messaging"`
}

Entry is a batch of events which were sent in this webhook trigger.

type Message

type Message struct {
	// Sender is who the message was sent from.
	Sender Sender `json:"-"`
	// Recipient is who the message was sent to.
	Recipient Recipient `json:"-"`
	// Time is when the message was sent.
	Time time.Time `json:"-"`
	// Mid is the ID of the message.
	Mid string `json:"mid"`
	// Seq is order the message was sent in relation to other messages.
	Seq int `json:"seq"`
	// Text is the textual contents of the message.
	Text string `json:"text"`
	// Attachments is the information about the attachments which were sent
	// with the message.
	Attachments []Attachment `json:"attachments"`
}

Message represents a Facebook messenge message.

type MessageData

type MessageData struct {
	Text         string       `json:"text,omitempty"`
	QuickReplies []QuickReply `json:"quick_replies,omitempty"`
}

MessageData is a text message with optional replies to be sent.

type MessageHandler

type MessageHandler func(Message, *Response)

MessageHandler is a handler used for responding to a message containing text.

type MessageInfo

type MessageInfo struct {
	// Sender is who the event was sent from.
	Sender Sender `json:"sender"`
	// Recipient is who the event was sent to.
	Recipient Recipient `json:"recipient"`
	// Timestamp is the true time the event was triggered.
	Timestamp int64 `json:"timestamp"`
	// Message is the contents of a message if it is a MessageAction.
	// Nil if it is not a MessageAction.
	Message *Message `json:"message"`
	// Delivery is the contents of a message if it is a DeliveryAction.
	// Nil if it is not a DeliveryAction.
	Delivery *Delivery `json:"delivery"`

	PostBack *PostBack `json:"postback"`
}

MessageInfo is an event that is fired by the webhook.

type Messenger

type Messenger struct {
	// contains filtered or unexported fields
}

Messenger is the client which manages communication with the Messenger Platform API.

func New

func New(mo Options) *Messenger

New creates a new Messenger. You pass in Options in order to affect settings.

func (*Messenger) HandleDelivery

func (m *Messenger) HandleDelivery(f DeliveryHandler)

HandleDelivery adds a new DeliveryHandler to the Messenger which will be triggered when a previously sent message is read by the recipient.

func (*Messenger) HandleMessage

func (m *Messenger) HandleMessage(f MessageHandler)

HandleMessage adds a new MessageHandler to the Messenger which will be triggered when a message is received by the client.

func (*Messenger) HandlePostBack

func (m *Messenger) HandlePostBack(f PostBackHandler)

HandlePostBack adds a new PostBackHandler to the Messenger

func (*Messenger) Handler

func (m *Messenger) Handler() http.Handler

Handler returns the Messenger in HTTP client form.

func (*Messenger) ProfileByID

func (m *Messenger) ProfileByID(id int64) (Profile, error)

ProfileByID retrieves the Facebook user associated with that ID

type Options

type Options struct {
	// Verify sets whether or not to be in the "verify" mode. Used for
	// verifying webhooks on the Facebook Developer Portal.
	Verify bool
	// VerifyToken is the token to be used when verifying the webhook. Is set
	// when the webhook is created.
	VerifyToken string
	// Token is the access token of the Facebook page to send messages from.
	Token string
	// WebhookURL is where the Messenger client should listen for webhook events. Leaving the string blank implies a path of "/".
	WebhookURL string
}

Options are the settings used when creating a Messenger client.

type Payload

type Payload struct {
	// URL is where the attachment resides on the internet.
	URL string `json:"url,omitempty"`
}

Payload is the information on where an attachment is.

type PostBack

type PostBack struct {
	// Sender is who the message was sent from.
	Sender Sender `json:"-"`
	// Recipient is who the message was sent to.
	Recipient Recipient `json:"-"`
	// Time is when the message was sent.
	Time time.Time `json:"-"`
	// PostBack ID
	Payload string `json:"payload"`
}

PostBack represents postback callback

type PostBackHandler

type PostBackHandler func(PostBack, *Response)

PostBackHandler is a handler used postback callbacks.

type Profile

type Profile struct {
	FirstName     string `json:"first_name"`
	LastName      string `json:"last_name"`
	ProfilePicURL string `json:"profile_pic"`
}

Profile is the public information of a Facebook user

type QuickReply

type QuickReply struct {
	// ContentType is the type of reply
	ContentType string `json:"content_type"`
	// Title is the reply title
	Title string `json:"title"`
	// Payload is the  reply information
	Payload string `json:"payload"`
}

QuickReply is a file which used in a message.

type Receive

type Receive struct {
	// Object should always be `page`. (I don't quite understand why)
	Object string `json:"object"`
	// Entry is all of the different messenger types which were
	// sent in this event.
	Entry []Entry `json:"entry"`
}

Receive is the format in which webhook events are sent.

type Recipient

type Recipient struct {
	ID int64 `json:"id,string"`
}

Recipient is who the message was sent to.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response is used for responding to events with messages.

func (*Response) ButtonTemplate

func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error

ButtonTemplate sends a message with the main contents being button elements

func (*Response) GenericTemplate

func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageElement) error

GenericTemplate is a message which allows for structural elements to be sent

func (*Response) Image

func (r *Response) Image(im image.Image) error

Image sends an image.

func (*Response) Text

func (r *Response) Text(message string) error

Text sends a textual message.

func (*Response) TextWithReplies

func (r *Response) TextWithReplies(message string, replies []QuickReply) error

TextWithReplies sends a textual message with some replies

type SendMessage

type SendMessage struct {
	Recipient Recipient   `json:"recipient"`
	Message   MessageData `json:"message"`
}

SendMessage is the information sent in an API request to Facebook.

type SendStructuredMessage

type SendStructuredMessage struct {
	Recipient Recipient             `json:"recipient"`
	Message   StructuredMessageData `json:"message"`
}

SendStructuredMessage is a structured message template.

type Sender

type Sender struct {
	ID int64 `json:"id,string"`
}

Sender is who the message was sent from.

type StructuredMessageAttachment

type StructuredMessageAttachment struct {
	// Type must be template
	Type string `json:"type"`
	// Payload is the information for the file which was sent in the attachment.
	Payload StructuredMessagePayload `json:"payload"`
}

StructuredMessageAttachment is the attachment of a structured message.

type StructuredMessageButton

type StructuredMessageButton struct {
	Type  string `json:"type"`
	URL   string `json:"url"`
	Title string `json:"title"`
}

StructuredMessageButton is a response containing buttons

type StructuredMessageData

type StructuredMessageData struct {
	Attachment StructuredMessageAttachment `json:"attachment"`
}

StructuredMessageData is an attachment sent with a structured message.

type StructuredMessageElement

type StructuredMessageElement struct {
	Title    string                    `json:"title"`
	ImageURL string                    `json:"image_url"`
	Subtitle string                    `json:"subtitle"`
	Buttons  []StructuredMessageButton `json:"buttons"`
}

StructuredMessageElement is a response containing structural elements

type StructuredMessagePayload

type StructuredMessagePayload struct {
	// TemplateType must be button, generic or receipt
	TemplateType string                      `json:"template_type"`
	Text         string                      `json:"text,omitempty"`
	Elements     *[]StructuredMessageElement `json:"elements,omitempty"`
	Buttons      *[]StructuredMessageButton  `json:"buttons,omitempty"`
}

StructuredMessagePayload is the actual payload of an attachment

Directories

Path Synopsis
cmd
bot

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL