fbot

package module
v0.0.0-...-397d147 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2017 License: MIT Imports: 9 Imported by: 1

README

fbot

Build Status

Bots for Facebook Messenger.

Description

A simple library for making bots for the Messenger Platform.

Installation

$ go get github.com/frodsan/fbot

Usage

package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/frodsan/fbot"
)

func main() {
	bot := fbot.NewBot(fbot.Config{
		AccessToken: os.Getenv("ACCESS_TOKEN"),
		AppSecret:   os.Getenv("APP_SECRET"),
		VerifyToken: os.Getenv("VERIFY_TOKEN"),
	})

	bot.On(fbot.EventMessage, func(event *fbot.Event) {
		fmt.Println(event.Message.Text)

		bot.Deliver(fbot.DeliverParams{
			Recipient: event.Sender,
			Message: &fbot.Message{
				Text: event.Message.Text,
			},
		})
	})

	http.Handle("/bot", fbot.Handler(bot))

	http.ListenAndServe(":4567", nil)
}

API

fbot.NewBot(c Config)

NewBot creates a new instance of a bot with the application's access token, app secret, and verify token.

bot := fbot.NewBot(fbot.Config{
	AccessToken: os.Getenv("ACCESS_TOKEN"),
	AppSecret:   os.Getenv("APP_SECRET"),
	VerifyToken: os.Getenv("VERIFY_TOKEN"),
})

fbot.Handler(bot *fb.Bot)

Returns the http.Handler that receives the request sent by the Messenger platform.

http.Handle("/bot", fbot.Handler(bot))

(*fbot.Bot) On(eventName string, callback func(*Event))

Registers a callback for the given eventName.

bot.On(fbot.EventMessage, func(event *fbot.Event) {
	event.Sender.ID    // => 1234567890
	event.Recipient.ID // => 0987654321
	event.Timestamp    // => 1462966178037

	event.Message.Mid  // => "mid.1234567890:41d102a3e1ae206a38"
	event.Message.Seq  // => 41
	event.Message.Text // => "Hello World!"

	event.Message.Attachments[0].Type        // => "image"
	event.Message.Attachments[0].Payload.URL // => https://scontent.xx.fbcdn.net/v/t34.0-12/...
})

bot.On(fbot.EventDelivery, func(event *fbot.Event) {
	event.Delivery.Mids[0]   // => "mid.1458668856218:ed81099e15d3f4f233"
	event.Delivery.Watermark // => 1458668856253
	event.Delivery.Seq       // => 37
})

bot.On(fbot.EventPostback, func(event *fbot.Event) {
	event.Postback.Payload // => "{foo:'foo',bar:'bar'}"
})

(fbot.Bot) Deliver(params fbot.DeliverParams) error

Sent messages through the Messenger Platform.

bot.Deliver(fbot.DeliverParams{
	Recipient: &fbot.User{
		ID: 1234567890
	},
	Message: &fbot.Message{
		Text: "Hey!",
	},
})

Configuration

Follow the Messenger Platform quickstart guide for set up the needed Facebook page and development app.

Development

To test the bot locally, use ngrok.

Design

The API is heavily inspired by hyperoslo/facebook-messenger.

License

fbot is released under the MIT License.

Documentation

Index

Constants

View Source
const (
	// EventMessage represents the message event.
	EventMessage = "message"
	// EventDelivery represents the message-delivery event.
	EventDelivery = "delivery"
	// EventPostback represents the postback event.
	EventPostback = "postback"
)
View Source
const Version = "0.0.2"

Version is the version of the library.

Variables

This section is empty.

Functions

func Handler

func Handler(bot Bot) http.HandlerFunc

Handler returns the handler to use for incoming messages from the Facebook Messenger Platform.

Types

type Attachment

type Attachment struct {
	Type    string   `json:"type"`
	Payload *Payload `json:"payload"`
}

Attachment represents the attachment object included in the message.

type Bot

type Bot struct {
	Config    *Config
	Callbacks map[string]func(*Event)
}

Bot is the object that receives and sends messages to the Messenger Platform.

func NewBot

func NewBot(config Config) Bot

NewBot creates a new instance of Bot.

func (Bot) Deliver

func (bot Bot) Deliver(params DeliverParams) error

Deliver uses the Send API to deliver messages.

func (*Bot) On

func (bot *Bot) On(eventName string, callback func(*Event))

On registers a callback for the given eventName.

type Config

type Config struct {
	AccessToken string
	AppSecret   string
	VerifyToken string
}

Config represents the required configuration to receive and send message to the Messenger Platform.

type DeliverParams

type DeliverParams struct {
	Recipient *User    `json:"recipient"`
	Message   *Message `json:"message"`
}

DeliverParams represents the message params sent by deliver.

type Delivery

type Delivery struct {
	Mids      []string `json:"mids"`
	Watermark int64    `json:"watermark"`
	Seq       int      `json:"seq"`
}

Delivery represents the message-delivered callback object.

type Event

type Event struct {
	Sender    *User     `json:"sender"`
	Recipient *User     `json:"recipient"`
	Timestamp int64     `json:"timestamp,omitempty"`
	Message   *Message  `json:"message"`
	Delivery  *Delivery `json:"delivery"`
	Postback  *Postback `json:"postback"`
}

Event represents the event fired by the webhook.

type Message

type Message struct {
	Mid         string        `json:"mid,omitempty"`
	Seq         int           `json:"seq,omitempty"`
	Text        string        `json:"text,omitempty"`
	Attachment  *Attachment   `json:"attachment,omitempty"`
	Attachments []*Attachment `json:"attachments,omitempty"`
}

Message represents the message callback object.

type Payload

type Payload struct {
	URL string `json:"url,omitempty"`
}

Payload represents the attachment payload data.

type Postback

type Postback struct {
	Payload string `json:"payload"`
}

Postback respresents the postback callback object.

type User

type User struct {
	ID          int64  `json:"id,omitempty"`
	PhoneNumber string `json:"phone_number,omitempty"`
}

User represents the user that acts like sender or recipient.

Jump to

Keyboard shortcuts

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