Skip to content

Commit 2dbbb29

Browse files
MattDavisRVgautamr95
authored andcommitted
1 parent 995aca2 commit 2dbbb29

File tree

5 files changed

+414
-77
lines changed

5 files changed

+414
-77
lines changed

assistant.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package slack
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/url"
7+
)
8+
9+
// AssistantThreadSetStatusParameters are the parameters for AssistantThreadSetStatus
10+
type AssistantThreadsSetStatusParameters struct {
11+
ChannelID string `json:"channel_id"`
12+
Status string `json:"status"`
13+
ThreadTS string `json:"thread_ts"`
14+
}
15+
16+
// AssistantThreadSetTitleParameters are the parameters for AssistantThreadSetTitle
17+
type AssistantThreadsSetTitleParameters struct {
18+
ChannelID string `json:"channel_id"`
19+
ThreadTS string `json:"thread_ts"`
20+
Title string `json:"title"`
21+
}
22+
23+
// AssistantThreadSetSuggestedPromptsParameters are the parameters for AssistantThreadSetSuggestedPrompts
24+
type AssistantThreadsSetSuggestedPromptsParameters struct {
25+
Title string `json:"title"`
26+
ChannelID string `json:"channel_id"`
27+
ThreadTS string `json:"thread_ts"`
28+
Prompts []AssistantThreadsPrompt `json:"prompts"`
29+
}
30+
31+
// AssistantThreadPrompt is a suggested prompt for a thread
32+
type AssistantThreadsPrompt struct {
33+
Title string `json:"title"`
34+
Message string `json:"message"`
35+
}
36+
37+
// AssistantThreadSetSuggestedPrompts sets the suggested prompts for a thread
38+
func (p *AssistantThreadsSetSuggestedPromptsParameters) AddPrompt(title, message string) {
39+
p.Prompts = append(p.Prompts, AssistantThreadsPrompt{
40+
Title: title,
41+
Message: message,
42+
})
43+
}
44+
45+
// SetAssistantThreadsSugesstedPrompts sets the suggested prompts for a thread
46+
// @see https://api.slack.com/methods/assistant.threads.setSuggestedPrompts
47+
func (api *Client) SetAssistantThreadsSuggestedPrompts(params AssistantThreadsSetSuggestedPromptsParameters) (err error) {
48+
return api.SetAssistantThreadsSuggestedPromptsContext(context.Background(), params)
49+
}
50+
51+
// SetAssistantThreadSuggestedPromptsContext sets the suggested prompts for a thread with a custom context
52+
// @see https://api.slack.com/methods/assistant.threads.setSuggestedPrompts
53+
func (api *Client) SetAssistantThreadsSuggestedPromptsContext(ctx context.Context, params AssistantThreadsSetSuggestedPromptsParameters) (err error) {
54+
55+
values := url.Values{
56+
"token": {api.token},
57+
}
58+
59+
if params.ThreadTS != "" {
60+
values.Add("thread_ts", params.ThreadTS)
61+
}
62+
63+
// Send Prompts as JSON
64+
prompts, err := json.Marshal(params.Prompts)
65+
if err != nil {
66+
return err
67+
}
68+
69+
values.Add("prompts", string(prompts))
70+
71+
response := struct {
72+
SlackResponse
73+
}{}
74+
75+
err = api.postMethod(ctx, "assistant.threads.setSuggestedPrompts", values, &response)
76+
if err != nil {
77+
return
78+
}
79+
80+
return response.Err()
81+
}
82+
83+
// SetAssistantThreadStatus sets the status of a thread
84+
// @see https://api.slack.com/methods/assistant.threads.setStatus
85+
func (api *Client) SetAssistantThreadsStatus(params AssistantThreadsSetStatusParameters) (err error) {
86+
return api.SetAssistantThreadsStatusContext(context.Background(), params)
87+
}
88+
89+
// SetAssistantThreadStatusContext sets the status of a thread with a custom context
90+
// @see https://api.slack.com/methods/assistant.threads.setStatus
91+
func (api *Client) SetAssistantThreadsStatusContext(ctx context.Context, params AssistantThreadsSetStatusParameters) (err error) {
92+
93+
values := url.Values{
94+
"token": {api.token},
95+
}
96+
97+
if params.ThreadTS != "" {
98+
values.Add("thread_ts", params.ThreadTS)
99+
}
100+
101+
// Always send the status parameter, if empty, it will clear any existing status
102+
values.Add("status", params.Status)
103+
104+
response := struct {
105+
SlackResponse
106+
}{}
107+
108+
err = api.postMethod(ctx, "assistant.threads.setStatus", values, &response)
109+
if err != nil {
110+
return
111+
}
112+
113+
return response.Err()
114+
}
115+
116+
// SetAssistantThreadsTitle sets the title of a thread
117+
// @see https://api.slack.com/methods/assistant.threads.setTitle
118+
func (api *Client) SetAssistantThreadsTitle(params AssistantThreadsSetTitleParameters) (err error) {
119+
return api.SetAssistantThreadsTitleContext(context.Background(), params)
120+
}
121+
122+
// SetAssistantThreadsTitleContext sets the title of a thread with a custom context
123+
// @see https://api.slack.com/methods/assistant.threads.setTitle
124+
func (api *Client) SetAssistantThreadsTitleContext(ctx context.Context, params AssistantThreadsSetTitleParameters) (err error) {
125+
126+
values := url.Values{
127+
"token": {api.token},
128+
}
129+
130+
if params.ChannelID != "" {
131+
values.Add("channel_id", params.ChannelID)
132+
}
133+
134+
if params.ThreadTS != "" {
135+
values.Add("thread_ts", params.ThreadTS)
136+
}
137+
138+
if params.Title != "" {
139+
values.Add("title", params.Title)
140+
}
141+
142+
response := struct {
143+
SlackResponse
144+
}{}
145+
146+
err = api.postMethod(ctx, "assistant.threads.setTitle", values, &response)
147+
if err != nil {
148+
return
149+
}
150+
151+
return response.Err()
152+
153+
}

assistant_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package slack
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"testing"
7+
)
8+
9+
func TestAssistantThreadsSuggestedPrompts(t *testing.T) {
10+
11+
http.HandleFunc("/assistant.threads.setSuggestedPrompts", okJSONHandler)
12+
once.Do(startServer)
13+
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
14+
15+
params := AssistantThreadsSetSuggestedPromptsParameters{
16+
ChannelID: "CXXXXXXXX",
17+
ThreadTS: "1234567890.123456",
18+
}
19+
20+
params.AddPrompt("title1", "message1")
21+
params.AddPrompt("title2", "message2")
22+
23+
err := api.SetAssistantThreadsSuggestedPrompts(params)
24+
if err != nil {
25+
t.Fatalf("Unexpected error: %s", err)
26+
}
27+
28+
}
29+
30+
func TestSetAssistantThreadsStatus(t *testing.T) {
31+
32+
http.HandleFunc("/assistant.threads.setStatus", okJSONHandler)
33+
once.Do(startServer)
34+
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
35+
36+
params := AssistantThreadsSetStatusParameters{
37+
ChannelID: "CXXXXXXXX",
38+
ThreadTS: "1234567890.123456",
39+
Status: "updated status",
40+
}
41+
42+
err := api.SetAssistantThreadsStatus(params)
43+
if err != nil {
44+
t.Fatalf("Unexpected error: %s", err)
45+
}
46+
47+
}
48+
49+
func assistantThreadsTitleHandler(rw http.ResponseWriter, r *http.Request) {
50+
51+
channelID := r.FormValue("channel_id")
52+
threadTS := r.FormValue("thread_ts")
53+
title := r.FormValue("title")
54+
55+
rw.Header().Set("Content-Type", "application/json")
56+
57+
if channelID != "" && threadTS != "" && title != "" {
58+
59+
resp, _ := json.Marshal(&addBookmarkResponse{
60+
SlackResponse: SlackResponse{Ok: true},
61+
})
62+
rw.Write(resp)
63+
} else {
64+
rw.Write([]byte(`{ "ok": false, "error": "errored" }`))
65+
}
66+
67+
}
68+
69+
func TestSetAssistantThreadsTitle(t *testing.T) {
70+
71+
http.HandleFunc("/assistant.threads.setTitle", assistantThreadsTitleHandler)
72+
once.Do(startServer)
73+
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
74+
75+
params := AssistantThreadsSetTitleParameters{
76+
ChannelID: "CXXXXXXXX",
77+
ThreadTS: "1234567890.123456",
78+
Title: "updated title",
79+
}
80+
81+
err := api.SetAssistantThreadsTitle(params)
82+
if err != nil {
83+
t.Fatalf("Unexpected error: %s", err)
84+
}
85+
86+
}

block_section.go

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type SectionBlock struct {
99
BlockID string `json:"block_id,omitempty"`
1010
Fields []*TextBlockObject `json:"fields,omitempty"`
1111
Accessory *Accessory `json:"accessory,omitempty"`
12+
Expand bool `json:"expand,omitempty"`
1213
}
1314

1415
// BlockType returns the type of the block
@@ -25,6 +26,15 @@ func SectionBlockOptionBlockID(blockID string) SectionBlockOption {
2526
}
2627
}
2728

29+
// SectionBlockOptionExpand allows long text to be auto-expanded when displaying
30+
//
31+
// @see https://api.slack.com/reference/block-kit/blocks#section
32+
func SectionBlockOptionExpand(shouldExpand bool) SectionBlockOption {
33+
return func(block *SectionBlock) {
34+
block.Expand = shouldExpand
35+
}
36+
}
37+
2838
// NewSectionBlock returns a new instance of a section block to be rendered
2939
func NewSectionBlock(textObj *TextBlockObject, fields []*TextBlockObject, accessory *Accessory, options ...SectionBlockOption) *SectionBlock {
3040
block := SectionBlock{

0 commit comments

Comments
 (0)