2016-04-13 05:03:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-18 22:54:43 +03:00
|
|
|
"flag"
|
2016-04-13 05:03:26 +03:00
|
|
|
"fmt"
|
2018-03-18 22:54:43 +03:00
|
|
|
"log"
|
2016-04-13 05:03:26 +03:00
|
|
|
"net/http"
|
2018-03-18 22:54:43 +03:00
|
|
|
"os"
|
2016-04-13 09:14:23 +03:00
|
|
|
"time"
|
2016-04-13 05:03:26 +03:00
|
|
|
|
|
|
|
"github.com/paked/messenger"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-18 22:54:43 +03:00
|
|
|
verifyToken = flag.String("verify-token", "mad-skrilla", "The token used to verify facebook (required)")
|
|
|
|
verify = flag.Bool("should-verify", false, "Whether or not the app should verify itself")
|
|
|
|
pageToken = flag.String("page-token", "not skrilla", "The token that is used to verify the page on facebook")
|
|
|
|
appSecret = flag.String("app-secret", "", "The app secret from the facebook developer portal (required)")
|
|
|
|
host = flag.String("host", "localhost", "The host used to serve the messenger bot")
|
|
|
|
port = flag.Int("port", 8080, "The port used to serve the messenger bot")
|
2016-04-13 05:03:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2018-03-18 22:54:43 +03:00
|
|
|
flag.Parse()
|
2016-04-13 05:03:26 +03:00
|
|
|
|
2018-03-18 22:54:43 +03:00
|
|
|
if *verifyToken == "" || *appSecret == "" || *pageToken == "" {
|
|
|
|
fmt.Println("missing arguments")
|
|
|
|
fmt.Println()
|
|
|
|
flag.Usage()
|
|
|
|
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2016-04-13 05:03:26 +03:00
|
|
|
|
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{
|
2016-04-13 05:03:26 +03:00
|
|
|
Verify: *verify,
|
2017-06-28 05:10:34 +03:00
|
|
|
AppSecret: *appSecret,
|
2016-04-13 05:03:26 +03:00
|
|
|
VerifyToken: *verifyToken,
|
2016-04-13 10:01:42 +03:00
|
|
|
Token: *pageToken,
|
2016-04-13 05:03:26 +03:00
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-03-10 09:02:16 +03:00
|
|
|
r.Text(fmt.Sprintf("Hello, %v!", p.FirstName), messenger.ResponseType)
|
2016-04-13 09:14:23 +03:00
|
|
|
})
|
|
|
|
|
2016-08-18 02:41:25 +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) {
|
2016-08-18 02:41:25 +03:00
|
|
|
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) {
|
2016-08-18 02:41:25 +03:00
|
|
|
fmt.Println("Read at:", m.Watermark().Format(time.UnixDate))
|
2016-04-13 12:12:23 +03:00
|
|
|
})
|
|
|
|
|
2018-03-18 22:54:43 +03:00
|
|
|
addr := fmt.Sprintf("%s:%d", *host, *port)
|
|
|
|
log.Println("Serving messenger bot on", addr)
|
|
|
|
log.Fatal(http.ListenAndServe(addr, client.Handler()))
|
2016-04-13 05:03:26 +03:00
|
|
|
}
|