-
Notifications
You must be signed in to change notification settings - Fork 2
/
Webhook.go
251 lines (228 loc) · 7.77 KB
/
Webhook.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"unicode"
mb "github.com/JeremyJalpha/MenuBotLib"
wa "github.com/febriliankr/whatsapp-cloud-api"
)
type StatusesWebhookRequest struct {
Object string `json:"object"`
Entry []struct {
ID string `json:"id"`
Changes []struct {
Value struct {
MessagingProduct string `json:"messaging_product"`
Metadata struct {
DisplayPhoneNumber string `json:"display_phone_number"`
PhoneNumberID string `json:"phone_number_id"`
} `json:"metadata"`
Statuses []struct {
ID string `json:"id"`
Status string `json:"status"`
Timestamp string `json:"timestamp"`
RecipientID string `json:"recipient_id"`
Conversation struct {
ID string `json:"id"`
ExpirationTimestamp string `json:"expiration_timestamp"`
Origin struct {
Type string `json:"type"`
} `json:"origin"`
} `json:"conversation"`
Pricing struct {
Billable bool `json:"billable"`
PricingModel string `json:"pricing_model"`
Category string `json:"category"`
} `json:"pricing"`
} `json:"statuses"`
} `json:"value"`
Field string `json:"field"`
} `json:"changes"`
} `json:"entry"`
}
type ContactsWebhookRequest struct {
Object string `json:"object"`
Entry []struct {
ID string `json:"id"`
Changes []struct {
Value struct {
MessagingProduct string `json:"messaging_product"`
Metadata struct {
DisplayPhoneNumber string `json:"display_phone_number"`
PhoneNumberID string `json:"phone_number_id"`
} `json:"metadata"`
Contacts []struct {
Profile struct {
Name string `json:"name"`
} `json:"profile"`
WaID string `json:"wa_id"`
} `json:"contacts"`
Messages []struct {
From string `json:"from"`
ID string `json:"id"`
Timestamp string `json:"timestamp"`
Text struct {
Body string `json:"body"`
} `json:"text"`
Type string `json:"type"`
} `json:"messages"`
} `json:"value"`
Field string `json:"field"`
} `json:"changes"`
} `json:"entry"`
}
// VerificationHandler handles the GET /webhook route for verification
func VerificationHandler(verifyToken string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
profferedToken := r.URL.Query().Get("hub.verify_token")
challenge := r.URL.Query().Get("hub.challenge")
if profferedToken == verifyToken {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(challenge))
if err != nil {
http.Error(w, "Internal Server Error.", http.StatusInternalServerError)
log.Println(err)
return
}
log.Println("Webhook verified.")
} else {
err := "Error, wrong validation token."
w.WriteHeader(http.StatusForbidden)
_, sendErr := w.Write([]byte(err))
if sendErr != nil {
http.Error(w, "Internal Server Error.", http.StatusInternalServerError)
log.Println(sendErr)
return
}
log.Println(err)
}
}
}
// CalculateSignature calculates the signature for the Facebook webhook payload.
func CalculateSignatureSha256(payload, secret []byte) string {
mac := hmac.New(sha256.New, secret)
mac.Write(payload)
rawHmac := mac.Sum(nil)
return hex.EncodeToString(rawHmac)
}
// Taken from: https://stackoverflow.com/questions/38353831/facebook-webhook-signature-calculation-c
func EscapeNonASCIICharacters(s string) string {
var escaped string
for _, c := range s {
if c > 127 {
escaped += fmt.Sprintf("\\u%04X", unicode.ToUpper(c))
} else {
escaped += string(c)
}
}
return escaped
}
// Checks whether the message is older than the parmater staleMsg in minutes
func IsMessageStale(timestamp string, staleMsg int) bool {
// Try to parse the string into an int64
timeInt, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return true
}
// Convert the Unix timestamp to a time.Time value
now := time.Now() // Add this line to define 'now'
return now.Sub(time.Unix(timeInt, 0)) >= time.Duration(staleMsg)*time.Minute
}
// IsMessageValid returns the body of the last message
// returns lastMesasgeBody, lastMsgTimeStamp, error
func IsMessageValid(req ContactsWebhookRequest, staleMsg int) (string, string, error) {
lastEntry := req.Entry[len(req.Entry)-1]
lastChange := lastEntry.Changes[len(lastEntry.Changes)-1]
lastMessage := lastChange.Value.Messages[len(lastChange.Value.Messages)-1]
lastMsgTimeStamp := lastMessage.Timestamp
MessageBody := strings.ToLower(lastMessage.Text.Body)
recipientNum := lastMessage.From
if lastMsgTimeStamp == "" || lastMsgTimeStamp == "-1" {
return "Err:FailedToGetLastMsgTimeStamp", "-1", fmt.Errorf("error failed to get last message timestamp")
}
if IsMessageStale(lastMsgTimeStamp, staleMsg) {
return "Err:StaleMessage", "-1", fmt.Errorf("error message was stale")
}
return MessageBody, recipientNum, nil
}
func RespondToUser(wa *wa.Whatsapp, destinationNum, chatMessage string) error {
_, err := wa.SendText(destinationNum, chatMessage)
if err != nil {
log.Println("ReturnToUser Failed with: " + err.Error())
return fmt.Errorf("ReturnToUser Failed with: " + err.Error())
}
return nil
}
// WebhookHandler handles the POST /webhook route
func WebhookHandler(appSecret, hostNumber string, staleMsg int, wa *wa.Whatsapp, db *sql.DB, prclst mb.Pricelist, checkoutUrls mb.CheckoutInfo) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
// Verify signature
signature256 := strings.TrimPrefix(r.Header.Get("X-Hub-Signature-256"), "sha256=")
if signature256 == "" {
err := "error, signature is missing"
http.Error(w, err, http.StatusForbidden)
log.Println(err)
return
}
// Read the request body
byteBody, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "error reading request body.", http.StatusInternalServerError)
log.Println("error reading request body: " + err.Error())
return
}
calculatedSignature256 := CalculateSignatureSha256([]byte(EscapeNonASCIICharacters(string(byteBody))), []byte(appSecret))
if subtle.ConstantTimeCompare([]byte(calculatedSignature256), []byte(signature256)) != 1 {
err := "error signatures do not match"
http.Error(w, err, http.StatusForbidden)
log.Println(err + "\nExpected Sha256: " + signature256 + "\nbut got Sha256: " + calculatedSignature256)
return
}
// Respond to the webhook request
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("Success"))
if err != nil {
log.Println("error writing response: ", err)
}
bodyStr := string(byteBody)
if strings.Contains(bodyStr, "\"statuses\":[{\"id\":\"") {
log.Println("Status updates unhandled at this time.")
return
}
// Parse the request body from the JSON string
var req ContactsWebhookRequest
err = json.Unmarshal(byteBody, &req)
if err != nil {
log.Println("error parsing JSON: ", err)
return
}
messageBody, senderNumber, err := IsMessageValid(req, staleMsg)
if err != nil {
log.Println("Message was invalid: " + err.Error())
return
}
if senderNumber != hostNumber && isTest == false {
convo := mb.NewConversationContext(db, senderNumber, messageBody, prclst, isAutoInc)
botresp := mb.GetResponseToMsg(convo, db, checkoutUrls, isAutoInc)
err := RespondToUser(wa, convo.UserInfo.CellNumber, botresp)
if err != nil {
log.Println("responding to user failed with: " + err.Error())
return
}
} else {
log.Println("You sent a message:", messageBody)
}
}
}