/
welcomemessage.go
70 lines (62 loc) · 1.75 KB
/
welcomemessage.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
// ADDED BY DROP - https://github.com/matryer/drop (v0.6)
// source: github.com/maciekmm/messenger-platform-go-sdk (ca9227b956ad50bc8b6225a464f6c0146887f7c5)
// update: drop -f github.com/maciekmm/messenger-platform-go-sdk
// license: The MIT License (MIT) (see repo for details)
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type ctaBase struct {
SettingType string `json:"setting_type"`
ThreadState string `json:"thread_state"`
}
var welcomeMessage = ctaBase{
SettingType: "call_to_actions",
ThreadState: "new_thread",
}
type ctaMessage struct {
Message *SendMessage `json:"message"`
}
type cta struct {
ctaBase
CallToActions []ctaMessage `json:"call_to_actions"`
}
type result struct {
Result string `json:"result"`
}
// SetWelcomeMessage sets the message that is sent first. If message is nil or empty the welcome message is not sent.
func (m *Messenger) SetWelcomeMessage(message *SendMessage) error {
cta := &cta{
ctaBase: welcomeMessage,
CallToActions: []ctaMessage{ctaMessage{Message: message}},
}
if m.PageID == "" {
return errors.New("PageID is empty")
}
byt, err := json.Marshal(cta)
if err != nil {
return err
}
resp, err := m.doRequest("POST", fmt.Sprintf(GraphAPI+"/v2.6/%s/thread_settings", m.PageID), bytes.NewReader(byt))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Invalid status code %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
result := &result{}
err = decoder.Decode(result)
if err != nil {
return err
}
if result.Result != "Successfully added new_thread's CTAs" {
return errors.New("Something went wrong with setting thread's welcome message, facebook error: " + result.Result)
}
return nil
}