Bots for Facebook Messenger.
A simple library for making bots for the Messenger Platform.
$ go get github.com/frodsan/fbot
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)
}
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"),
})
Returns the http.Handler
that receives the request sent by the Messenger platform.
http.Handle("/bot", fbot.Handler(bot))
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'}"
})
Sent messages through the Messenger Platform.
bot.Deliver(fbot.DeliverParams{
Recipient: &fbot.User{
ID: 1234567890
},
Message: &fbot.Message{
Text: "Hey!",
},
})
Follow the Messenger Platform quickstart guide for set up the needed Facebook page and development app.
To test the bot locally, use ngrok.
The API is heavily inspired by hyperoslo/facebook-messenger.
fbot is released under the MIT License.