1
0
mirror of synced 2024-11-23 13:26:06 +03:00
messenger/examples/basic/main.go

62 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net/http"
2016-04-13 09:14:23 +03:00
"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")
2016-04-13 10:01:42 +03:00
pageToken = conf.String("page-token", "not skrilla", "The token that is used to verify the page on facebook")
appSecret = conf.String("app-secret", "", "The app secret from the facebook developer portal")
port = conf.Int("port", 8080, "The port used to serve the messenger bot")
)
func main() {
conf.Use(configure.NewFlag())
conf.Use(configure.NewEnvironment())
conf.Use(configure.NewJSONFromFile("config.json"))
conf.Parse()
2016-04-15 03:34:10 +03:00
// Create a new messenger client
2016-04-15 03:16:52 +03:00
client := messenger.New(messenger.Options{
Verify: *verify,
AppSecret: *appSecret,
VerifyToken: *verifyToken,
2016-04-13 10:01:42 +03:00
Token: *pageToken,
})
2016-04-15 03:34:10 +03:00
// Setup a handler to be triggered when a message is received
2016-04-15 03:16:52 +03:00
client.HandleMessage(func(m messenger.Message, r *messenger.Response) {
2016-04-13 09:14:23 +03:00
fmt.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate))
2016-04-15 03:16:52 +03:00
p, err := client.ProfileByID(m.Sender.ID)
if err != nil {
fmt.Println("Something went wrong!", err)
}
r.Text(fmt.Sprintf("Hello, %v!", p.FirstName), messenger.ResponseType)
2016-04-13 09:14:23 +03:00
})
// Setup a handler to be triggered when a message is delivered
2016-04-15 03:16:52 +03:00
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
2016-08-19 12:47:04 +03:00
client.HandleRead(func(m messenger.Read, r *messenger.Response) {
fmt.Println("Read at:", m.Watermark().Format(time.UnixDate))
2016-04-13 12:12:23 +03:00
})
fmt.Printf("Serving messenger bot on localhost:%d\n", *port)
2016-04-13 09:14:23 +03:00
http.ListenAndServe(fmt.Sprintf("localhost:%d", *port), client.Handler())
}