forked from abhinavdahiya/go-messenger-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger.go
256 lines (223 loc) · 5.51 KB
/
messenger.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package mbotapi
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
// This defines a bot
// Set Debug to true for debugging
type BotAPI struct {
Token string
VerifyToken string
AppSecret string
Debug bool
Client *http.Client
}
// This helps create a BotAPI instance with token, verify_token
// By default Debug is set to false
func NewBotAPI(token string, vtoken string, secret string) *BotAPI {
return &BotAPI{
Token: token,
AppSecret: secret,
VerifyToken: vtoken,
Debug: false,
Client: &http.Client{},
}
}
// This helps send request (send messages to users)
// It takes Request struct encoded into a buffer of json bytes
// The APIResponse contains the error from FB if any
// Should NOT be directly used, Use Send / SendFile
func (bot *BotAPI) MakeRequest(b *bytes.Buffer) (APIResponse, error) {
uri := fmt.Sprintf(APIEndpoint, bot.Token)
req, _ := http.NewRequest("POST", uri, b)
req.Header.Set("Content-Type", "application/json")
resp, err := bot.Client.Do(req)
if err != nil {
return APIResponse{}, err
}
defer resp.Body.Close()
var rsp APIResponse
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&rsp)
if err != nil {
return APIResponse{}, nil
}
if resp.StatusCode != 200 {
return rsp, errors.New(http.StatusText(resp.StatusCode))
}
return rsp, nil
}
// This function helps send messages to users
// It takes Message / GenericTemplate / ButtonTemplate / ReceiptTemplate and
// sends it to the user
func (bot *BotAPI) Send(u User, c interface{}, notif string) (APIResponse, error) {
var r Request
n := RegularNotif
if notif != "" {
n = notif
}
switch c.(type) {
case Request:
return APIResponse{}, errors.New("Use MakeRequest to send Request!!")
case Action:
r = Request{
Recipient: u,
Action: c.(Action),
NotifType: n,
}
case Message:
r = Request{
Recipient: u,
Message: c.(Message),
NotifType: n,
}
case GenericTemplate:
r = Request{
Recipient: u,
NotifType: n,
Message: Message{
Attachment: &Attachment{
Type: "template",
Payload: c.(GenericTemplate),
},
},
}
case ListTemplate:
r = Request{
Recipient: u,
NotifType: n,
Message: Message{
Attachment: &Attachment{
Type: "template",
Payload: c.(ListTemplate),
},
},
}
case ButtonTemplate:
r = Request{
Recipient: u,
NotifType: n,
Message: Message{
Attachment: &Attachment{
Type: "template",
Payload: c.(ButtonTemplate),
},
},
}
case ReceiptTemplate:
r = Request{
Recipient: u,
NotifType: n,
Message: Message{
Attachment: &Attachment{
Type: "template",
Payload: c.(ReceiptTemplate),
},
},
}
default:
return APIResponse{}, errors.New("Type is not supported")
}
//if r == (Request{}) {
// return APIResponse{}, errors.New("Unknown Error")
//}
payl, _ := json.Marshal(r)
if bot.Debug {
log.Printf("[INFO] Payload: %s", string(payl))
}
return bot.MakeRequest(bytes.NewBuffer(payl))
}
// This helps to send local images (currently) to users
// TODO: not tested yet!
func (bot *BotAPI) SendFile(u User, path string) (APIResponse, error) {
file, err := os.Open(path)
if err != nil {
return APIResponse{}, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("filedata", filepath.Base(path))
if err != nil {
return APIResponse{}, err
}
_, err = io.Copy(part, file)
usr, _ := json.Marshal(u)
_ = writer.WriteField("recipient", string(usr))
img := NewImageFromURL("")
im, _ := json.Marshal(img)
_ = writer.WriteField("message", string(im))
err = writer.Close()
if err != nil {
return APIResponse{}, err
}
return bot.MakeRequest(body)
}
//This function verifies the message
func verifySignature(appSecret string, bytes []byte, expectedSignature string) bool {
mac := hmac.New(sha1.New, []byte(appSecret))
mac.Write(bytes)
if fmt.Sprintf("%x", mac.Sum(nil)) != expectedSignature {
return false
}
return true
}
// This function registers the handlers for
// - webhook verification
// - all callbacks made on the webhhoks
// It loops over all entries in the callback and
// pushes to the Callback channel
// This also return a *http.ServeMux which can be used to listenAndServe
func (bot *BotAPI) SetWebhook(pattern string) (<-chan Callback, *http.ServeMux) {
callbackChan := make(chan Callback, 100)
mux := http.NewServeMux()
mux.HandleFunc(pattern, func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
if req.FormValue("hub.verify_token") == bot.VerifyToken {
w.Write([]byte(req.FormValue("hub.challenge")))
return
}
w.WriteHeader(http.StatusUnauthorized)
return
case "POST":
defer req.Body.Close()
body, _ := ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewReader(body))
if bot.Debug {
log.Printf("[INFO]%s", body)
}
if req.Header.Get("X-Hub-Signature") == "" || !verifySignature(bot.AppSecret, body, req.Header.Get("X-Hub-Signature")[5:]) {
w.WriteHeader(http.StatusBadRequest)
return
}
var rsp Response
decoder := json.NewDecoder(req.Body)
decoder.Decode(&rsp)
if rsp.Object == "page" {
for _, e := range rsp.Entries {
for _, c := range e.Messaging {
callbackChan <- c
}
}
}
w.WriteHeader(http.StatusOK)
return
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
})
return callbackChan, mux
}