de3a5d0949
* Added Read message support Delivered message suppport already existed but was commented as indicating that a message was read by a user. As of the 1 July 2016 API release this isn't true, there is another event. This commit adds read event support and corrects the description of delivered events. Also added to the example bot. * go fmt * Added new profile fields from July 1st API update * gofmt
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/paked/configure"
|
|
"github.com/paked/messenger"
|
|
)
|
|
|
|
var (
|
|
conf = configure.New()
|
|
verifyToken = conf.String("verify-token", "mad-skrilla", "The token used to verify facebook")
|
|
verify = conf.Bool("should-verify", false, "Whether or not the app should verify itself")
|
|
pageToken = conf.String("page-token", "not skrilla", "The token that is used to verify the page on facebook")
|
|
)
|
|
|
|
func main() {
|
|
conf.Use(configure.NewFlag())
|
|
conf.Use(configure.NewEnvironment())
|
|
conf.Use(configure.NewJSONFromFile("config.json"))
|
|
|
|
conf.Parse()
|
|
|
|
// Create a new messenger client
|
|
client := messenger.New(messenger.Options{
|
|
Verify: *verify,
|
|
VerifyToken: *verifyToken,
|
|
Token: *pageToken,
|
|
})
|
|
|
|
// Setup a handler to be triggered when a message is received
|
|
client.HandleMessage(func(m messenger.Message, r *messenger.Response) {
|
|
fmt.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate))
|
|
|
|
p, err := client.ProfileByID(m.Sender.ID)
|
|
if err != nil {
|
|
fmt.Println("Something went wrong!", err)
|
|
}
|
|
|
|
r.Text(fmt.Sprintf("Hello, %v!", p.FirstName))
|
|
})
|
|
|
|
// Setup a handler to be triggered when a message is delivered
|
|
client.HandleDelivery(func(d messenger.Delivery, r *messenger.Response) {
|
|
fmt.Println("Delivered at:", d.Watermark().Format(time.UnixDate))
|
|
})
|
|
|
|
// Setup a handler to be triggered when a message is read
|
|
client.HandleDelivery(func(m messenger.Read, r *messenger.Response) {
|
|
fmt.Println("Read at:", m.Watermark().Format(time.UnixDate))
|
|
})
|
|
|
|
fmt.Println("Serving messenger bot on localhost:8080")
|
|
|
|
http.ListenAndServe("localhost:8080", client.Handler())
|
|
}
|