-
Notifications
You must be signed in to change notification settings - Fork 10
/
threadsettings.go
100 lines (86 loc) · 1.96 KB
/
threadsettings.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
package mbotapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const (
SettingsEndpoint = "https://graph.facebook.com/v2.6/me/thread_settings?access_token=%s"
)
type ThreadSetting struct {
Type string `json:"setting_type"`
State string `json:"thread_state,omitempty"`
Action interface{} `json:"call_to_actions,omitempty"`
Greeting *Greeting `json:"greeting,omitempty"`
}
type Greeting struct {
Text string `json:"text"`
}
type GStarted struct {
Payload string `json:"payload"`
}
func (bot *BotAPI) SetSettings(b *bytes.Buffer) error {
uri := fmt.Sprintf(SettingsEndpoint, 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 err
}
defer resp.Body.Close()
rsp, _ := ioutil.ReadAll(resp.Body)
log.Printf("[SETTINGS] %s", rsp)
if resp.StatusCode != 200 {
return errors.New(http.StatusText(resp.StatusCode))
}
return nil
}
func (bot *BotAPI) SetGreeting(text string) error {
g := ThreadSetting{
Type: "greeting",
Greeting: &Greeting{
Text: text,
},
}
log.Printf("[SETTINGS] %#v", g)
payl, err := json.Marshal(g)
if err != nil {
return err
}
log.Printf("[SETTINGS] %s", payl)
return bot.SetSettings(bytes.NewBuffer(payl))
}
func (bot *BotAPI) SetGStarted(text string) error {
g := ThreadSetting{
Type: "call_to_actions",
State: "new_thread",
Action: []GStarted{
{text},
},
}
log.Printf("[SETTINGS] %#v", g)
payl, err := json.Marshal(g)
if err != nil {
return err
}
log.Printf("[SETTINGS] %s", payl)
return bot.SetSettings(bytes.NewBuffer(payl))
}
func (bot *BotAPI) SetMenu(bts []Button) error {
g := ThreadSetting{
Type: "call_to_actions",
State: "existing_thread",
Action: bts,
}
log.Printf("[SETTINGS] %#v", g)
payl, err := json.Marshal(g)
if err != nil {
return err
}
log.Printf("[SETTINGS] %s", payl)
return bot.SetSettings(bytes.NewBuffer(payl))
}