This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbot.go
80 lines (69 loc) · 1.92 KB
/
fbot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package fbot can be used to communicate with a Facebook Messenger bot.
// The supported API is limited to only features we use.
// Please open an issue for features you are missing.
package fbot
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"strings"
)
const defaultAPI = "https://graph.facebook.com/v2.10"
// Client can be used to communicate with a Messenger bot.
// Use New for initialization.
type Client struct {
token string
secretProof string
api string
}
// Config is passed to New.
type Config struct {
Token string
Secret string
API string // Optional. Overwrite the Facebook API URL.
}
// New rerturns a new client with credentials set up.
func New(c Config) Client {
// Generate secret proof. See https://developers.facebook.com/docs/graph-api/securing-requests/#appsecret_proof
mac := hmac.New(sha256.New, []byte(c.Secret))
mac.Write([]byte(c.Token))
api := strings.TrimSuffix(c.API, "/")
if api == "" {
api = defaultAPI
}
return Client{
token: c.Token,
secretProof: hex.EncodeToString(mac.Sum(nil)),
api: api,
}
}
// Button describes a button that can be send with a Button Template.
// Use URLButton or PayloadButton for initialization.
type Button interface{}
// Reply describes a text quick reply.
type Reply struct {
// Text is the text on the button visible to the user
Text string
// Payload is a string to identify the quick reply event internally in your application.
Payload string
}
// Helper to check for errors in reply
func checkError(r io.Reader) error {
var qr struct {
Error *struct {
Message string `json:"message"`
Type string `json:"type"`
Code int `json:"code"`
FBTraceID string `json:"fbtrace_id"`
} `json:"error"`
Result string `json:"result"`
}
err := json.NewDecoder(r).Decode(&qr)
if qr.Error != nil {
err = fmt.Errorf("Facebook error: %s", qr.Error.Message)
}
return err
}