fbot

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2017 License: MIT Imports: 12 Imported by: 0

README

fbot - An optionionated Go package for the Facebook Messenger API

GoDoc Build Status Go Report Card

Development

Make sure to use gofmt and create a Pull Request.

License

MIT

Documentation

Overview

Package fbot can be used to communicate with a Facebook Messenger bot. The supported API is limited to only features we use. Please open an issue for features you are missing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Type    string
	URL     string
	Sticker int64
}

Attachment describes an attachment. Type is one of "image", "video", audio, "location", "file" or "feedback". Currently only the URL field is loaded because we only use "file". If a sticker is sent the type is "image" and Sticker != 0. For more see: https://developers.facebook.com/docs/messenger-platform/webhook-reference/message

type Button

type Button interface{}

Button describes a button that can be send with a Button Template. Use URLButton or PayloadButton for initialization.

func LinkButton

func LinkButton(text, url string) Button

LinkButton returns a button that opens a URL in the browser.

func PayloadButton

func PayloadButton(text, payload string) Button

PayloadButton returns a button that posts a payload back to the bot.

func URLButton

func URLButton(text, url string) Button

URLButton returns a button that opens a URL in a full-screen webview.

type Client

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

Client can be used to communicate with a Messenger bot. Use New for initialization.

func New

func New(c Config) Client

New rerturns a new client with credentials set up.

func (Client) GetProfile

func (c Client) GetProfile(id int64) (Profile, error)

GetProfile fetches a user profile for an ID.

func (Client) Send

func (c Client) Send(id int64, message string, replies []Reply) error

Send a text message with a set of quick reply buttons to a user.

func (Client) SendWithButtons

func (c Client) SendWithButtons(id int64, message string, replies []Reply, buttons []Button) error

SendWithButtons sends a message with a set of buttons and quick replies to a user.

func (Client) SetGetStartedPayload

func (c Client) SetGetStartedPayload(p string) error

SetGetStartedPayload displays a "Get Started" button for new users. When a users pushes the button, a postback with the given payload is triggered.

func (Client) SetGreetings

func (c Client) SetGreetings(g []Greeting) error

SetGreetings sets the text displayed in the bot description. Include "default" locale as fallback for missing locales.

func (Client) Webhook

func (c Client) Webhook(handler func(Event), secret, verifyToken string) http.Handler

Webhook returns a handler for HTTP requests that can be registered with Facebook. The passed event handler will be called with all received events.

type Config

type Config struct {
	Token  string
	Secret string
	API    string // Optional. Overwrite the Facebook API URL.
}

Config is passed to New.

type Event

type Event struct {
	// Type helps to decide how to react to an event.
	Type EventType
	// ChatID identifies the user. It's a Facebook user ID.
	ChatID int64
	// Time describes when the event occured.
	Time time.Time
	// Text is a message a user send for EventMessage and error description for EventError.
	Text string
	// Payload is a predefined payload for a quick reply or postback sent with EventPayload.
	Payload string
	// MessageID is a unique ID for each message.
	MessageID string
	// Attachments are multiple attachment types.
	Attachments []Attachment
	// Ref contains the ref data from the URL for EventReferral.
	// Ref is also set for EventPayload if the Event was triggered through the Get Started button
	// and the user used a refferal link to get there.
	Ref string
}

Event contains information about a user action.

type EventType

type EventType int

EventType helps to distinguish the different type of events.

const (
	// EventError is triggered when the webhook is called with invalid JSON content.
	EventError EventType = 1 + iota
	// EventMessage is triggered when a user sends Text, stickers or other content.
	// Only text is available at the moment.
	EventMessage
	// EventPayload is triggered when a quickReply or postback Payload is sent.
	EventPayload
	// EventRead is triggered when a user read a message.
	EventRead
	// EventAttachment is triggered when attachemnts are send.
	EventAttachment
	// EventReferral is triggered when referring through a link or other source.
	EventReferral
)

type Greeting

type Greeting struct {
	// Locale should be 5 chars, like "en_US".
	// For supported locales see: https://developers.facebook.com/docs/messenger-platform/messenger-profile/supported-locales
	Locale string `json:"locale"`
	// Text can be any text, but it's restricted to 160 chars.
	// Supports a few template strings, see: https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
	Text string `json:"text"`
}

Greeting is a localized message describing the bot.

type Profile

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

Profile has all public user information we need; needs to be in sync with the URL above.

func (Profile) Locale

func (p Profile) Locale() string

Locale returns th locale in the form "en_GB".

func (Profile) Name

func (p Profile) Name() string

Name returns the first name.

func (Profile) Timezone

func (p Profile) Timezone() int

Timezone retunrs the timezone relative to UTC.

type Reply

type Reply struct {
	// Text is the text on the button visible to the user
	Text string
	// Payload is a string to identify the quick reply event internally in your application.
	Payload string
}

Reply describes a text quick reply.

Jump to

Keyboard shortcuts

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