Skip to content

Commit

Permalink
Add method and tests for creating channel canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
jarospisak-unity committed Oct 14, 2024
1 parent 93ef77a commit 859c30c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slack

import (
"context"
"encoding/json"
"errors"
"net/url"
"strconv"
Expand Down Expand Up @@ -796,3 +797,36 @@ func (api *Client) MarkConversationContext(ctx context.Context, channel, ts stri
}
return response.Err()
}

// CreateChannelCanvas creates a new canvas in a channel.
// For more details, see CreateChannelCanvasContext documentation.
func (api *Client) CreateChannelCanvas(channel string, documentContent DocumentContent) (string, error) {
return api.CreateChannelCanvasContext(context.Background(), channel, documentContent)
}

// CreateChannelCanvasContext creates a new canvas in a channel with a custom context.
// Slack API docs: https://api.slack.com/methods/conversations.canvases.create
func (api *Client) CreateChannelCanvasContext(ctx context.Context, channel string, documentContent DocumentContent) (string, error) {
values := url.Values{
"token": {api.token},
"channel_id": {channel},
}
if documentContent.Type != "" {
documentContentJSON, err := json.Marshal(documentContent)
if err != nil {
return "", err
}
values.Add("document_content", string(documentContentJSON))
}

response := struct {
SlackResponse
CanvasID string `json:"canvas_id"`
}{}
err := api.postMethod(ctx, "conversations.canvases.create", values, &response)
if err != nil {
return "", err
}

return response.CanvasID, response.Err()
}
31 changes: 31 additions & 0 deletions conversation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,34 @@ func TestMarkConversation(t *testing.T) {
return
}
}

func createChannelCanvasHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(struct {
SlackResponse
CanvasID string `json:"canvas_id"`
}{
SlackResponse: SlackResponse{Ok: true},
CanvasID: "F05RQ01LJU0",
})
rw.Write(response)
}

func TestCreateChannelCanvas(t *testing.T) {
http.HandleFunc("/conversations.canvases.create", createChannelCanvasHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

documentContent := DocumentContent{
Type: "markdown",
Markdown: "> channel canvas!",
}

canvasID, err := api.CreateChannelCanvas("C1234567890", documentContent)
if err != nil {
t.Errorf("Failed to create channel canvas: %v", err)
return
}

assert.Equal(t, "F05RQ01LJU0", canvasID)
}

0 comments on commit 859c30c

Please sign in to comment.