Skip to content

Commit

Permalink
Merge branch 'master' into calls-api
Browse files Browse the repository at this point in the history
  • Loading branch information
winston-stripe authored Sep 23, 2024
2 parents b0f1f5f + 447b7cd commit c198962
Show file tree
Hide file tree
Showing 82 changed files with 5,345 additions and 643 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/stale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@1160a2240286f5da8ec72b1c0816ce2481aabf84 # v8.0.0
- uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e # v9.0.0
with:
any-of-labels: 'feedback given'
days-before-stale: 45
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ jobs:
- '1.18'
- '1.19'
- '1.20'
- '1.21'
- '1.22'
- '1.23'
name: test go-${{ matrix.go }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: run test
Expand All @@ -29,14 +32,14 @@ jobs:
runs-on: ubuntu-22.04
name: lint
steps:
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.20'
cache: false
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # v3.4.0
uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0
with:
version: v1.52.2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.test
*~
.idea/
/vendor/
10 changes: 9 additions & 1 deletion apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ type EventAuthorization struct {
IsEnterpriseInstall bool `json:"is_enterprise_install"`
}

// ListEventAuthorizations lists authed users and teams for the given event_context.
// You must provide an app-level token to the client using OptionAppLevelToken.
// For more details, see ListEventAuthorizationsContext documentation.
func (api *Client) ListEventAuthorizations(eventContext string) ([]EventAuthorization, error) {
return api.ListEventAuthorizationsContext(context.Background(), eventContext)
}

// ListEventAuthorizationsContext lists authed users and teams for the given event_context. You must provide an app-level token to the client using OptionAppLevelToken. More info: https://api.slack.com/methods/apps.event.authorizations.list
// ListEventAuthorizationsContext lists authed users and teams for the given event_context with a custom context.
// Slack API docs: https://api.slack.com/methods/apps.event.authorizations.list
func (api *Client) ListEventAuthorizationsContext(ctx context.Context, eventContext string) ([]EventAuthorization, error) {
resp := &listEventAuthorizationsResponse{}

Expand All @@ -43,10 +47,14 @@ func (api *Client) ListEventAuthorizationsContext(ctx context.Context, eventCont
return resp.Authorizations, nil
}

// UninstallApp uninstalls your app from a workspace.
// For more details, see UninstallAppContext documentation.
func (api *Client) UninstallApp(clientID, clientSecret string) error {
return api.UninstallAppContext(context.Background(), clientID, clientSecret)
}

// UninstallAppContext uninstalls your app from a workspace with a custom context.
// Slack API docs: https://api.slack.com/methods/apps.uninstall
func (api *Client) UninstallAppContext(ctx context.Context, clientID, clientSecret string) error {
values := url.Values{
"client_id": {clientID},
Expand Down
11 changes: 7 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func (api *Client) authRequest(ctx context.Context, path string, values url.Valu
return response, response.Err()
}

// SendAuthRevoke will send a revocation for our token
// SendAuthRevoke will send a revocation for our token.
// For more details, see SendAuthRevokeContext documentation.
func (api *Client) SendAuthRevoke(token string) (*AuthRevokeResponse, error) {
return api.SendAuthRevokeContext(context.Background(), token)
}

// SendAuthRevokeContext will send a revocation request for our token to api.revoke with context
// SendAuthRevokeContext will send a revocation request for our token to api.revoke with a custom context.
// Slack API docs: https://api.slack.com/methods/auth.revoke
func (api *Client) SendAuthRevokeContext(ctx context.Context, token string) (*AuthRevokeResponse, error) {
if token == "" {
token = api.token
Expand All @@ -50,12 +52,13 @@ type ListTeamsParameters struct {
}

// ListTeams returns all workspaces a token can access.
// More info: https://api.slack.com/methods/admin.teams.list
// For more details, see ListTeamsContext documentation.
func (api *Client) ListTeams(params ListTeamsParameters) ([]Team, string, error) {
return api.ListTeamsContext(context.Background(), params)
}

// ListTeams returns all workspaces a token can access with a custom context.
// ListTeamsContext returns all workspaces a token can access with a custom context.
// Slack API docs: https://api.slack.com/methods/auth.teams.list
func (api *Client) ListTeamsContext(ctx context.Context, params ListTeamsParameters) ([]Team, string, error) {
values := url.Values{
"token": {api.token},
Expand Down
2 changes: 2 additions & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
MBTHeader MessageBlockType = "header"
MBTRichText MessageBlockType = "rich_text"
MBTCall MessageBlockType = "call"
MBTVideo MessageBlockType = "video"
)

// Block defines an interface all block types should implement
Expand All @@ -40,6 +41,7 @@ type BlockAction struct {
Type ActionType `json:"type"`
Text TextBlockObject `json:"text"`
Value string `json:"value"`
Files []File `json:"files"`
ActionTs string `json:"action_ts"`
SelectedOption OptionBlockObject `json:"selected_option"`
SelectedOptions []OptionBlockObject `json:"selected_options"`
Expand Down
21 changes: 18 additions & 3 deletions block_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package slack

import (
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -66,10 +65,14 @@ func (b *Blocks) UnmarshalJSON(data []byte) error {
block = &InputBlock{}
case "rich_text":
block = &RichTextBlock{}
case "rich_text_input":
block = &RichTextBlock{}
case "section":
block = &SectionBlock{}
case "call":
block = &CallBlock{}
case "video":
block = &VideoBlock{}
default:
block = &UnknownBlock{}
}
Expand Down Expand Up @@ -116,6 +119,8 @@ func (b *InputBlock) UnmarshalJSON(data []byte) error {
e = &DateTimePickerBlockElement{}
case "plain_text_input":
e = &PlainTextInputBlockElement{}
case "rich_text_input":
e = &RichTextInputBlockElement{}
case "email_text_input":
e = &EmailTextInputBlockElement{}
case "url_text_input":
Expand All @@ -132,8 +137,10 @@ func (b *InputBlock) UnmarshalJSON(data []byte) error {
e = &RadioButtonsBlockElement{}
case "number_input":
e = &NumberInputBlockElement{}
case "file_input":
e = &FileInputBlockElement{}
default:
return errors.New("unsupported block element type")
return fmt.Errorf("unsupported block element type %v", s.TypeVal)
}

if err := json.Unmarshal(a.Element, e); err != nil {
Expand Down Expand Up @@ -198,6 +205,8 @@ func (b *BlockElements) UnmarshalJSON(data []byte) error {
blockElement = &DateTimePickerBlockElement{}
case "plain_text_input":
blockElement = &PlainTextInputBlockElement{}
case "rich_text_input":
blockElement = &RichTextInputBlockElement{}
case "email_text_input":
blockElement = &EmailTextInputBlockElement{}
case "url_text_input":
Expand Down Expand Up @@ -300,6 +309,12 @@ func (a *Accessory) UnmarshalJSON(data []byte) error {
return err
}
a.PlainTextInputElement = element.(*PlainTextInputBlockElement)
case "rich_text_input":
element, err := unmarshalBlockElement(r, &RichTextInputBlockElement{})
if err != nil {
return err
}
a.RichTextInputElement = element.(*RichTextInputBlockElement)
case "radio_buttons":
element, err := unmarshalBlockElement(r, &RadioButtonsBlockElement{})
if err != nil {
Expand Down Expand Up @@ -431,7 +446,7 @@ func (e *ContextElements) UnmarshalJSON(data []byte) error {

e.Elements = append(e.Elements, elem.(*ImageBlockElement))
default:
return errors.New("unsupported context element type")
return fmt.Errorf("unsupported context element type %v", contextElementType)
}
}

Expand Down
74 changes: 74 additions & 0 deletions block_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const (
METDatetimepicker MessageElementType = "datetimepicker"
METPlainTextInput MessageElementType = "plain_text_input"
METRadioButtons MessageElementType = "radio_buttons"
METRichTextInput MessageElementType = "rich_text_input"
METEmailTextInput MessageElementType = "email_text_input"
METURLTextInput MessageElementType = "url_text_input"
METNumber MessageElementType = "number_input"
METFileInput MessageElementType = "file_input"

MixedElementImage MixedElementType = "mixed_image"
MixedElementText MixedElementType = "mixed_text"
Expand Down Expand Up @@ -51,6 +53,7 @@ type Accessory struct {
DatePickerElement *DatePickerBlockElement
TimePickerElement *TimePickerBlockElement
PlainTextInputElement *PlainTextInputBlockElement
RichTextInputElement *RichTextInputBlockElement
RadioButtonsElement *RadioButtonsBlockElement
SelectElement *SelectBlockElement
MultiSelectElement *MultiSelectBlockElement
Expand All @@ -73,6 +76,8 @@ func NewAccessory(element BlockElement) *Accessory {
return &Accessory{TimePickerElement: element.(*TimePickerBlockElement)}
case *PlainTextInputBlockElement:
return &Accessory{PlainTextInputElement: element.(*PlainTextInputBlockElement)}
case *RichTextInputBlockElement:
return &Accessory{RichTextInputElement: element.(*RichTextInputBlockElement)}
case *RadioButtonsBlockElement:
return &Accessory{RadioButtonsElement: element.(*RadioButtonsBlockElement)}
case *SelectBlockElement:
Expand Down Expand Up @@ -177,6 +182,12 @@ func (s *ButtonBlockElement) WithConfirm(confirm *ConfirmationBlockObject) *Butt
return s
}

// WithURL adds a URL for the button to link to and returns the modified ButtonBlockElement
func (s *ButtonBlockElement) WithURL(url string) *ButtonBlockElement {
s.URL = url
return s
}

// NewButtonBlockElement returns an instance of a new button element to be used within a block
func NewButtonBlockElement(actionID, value string, text *TextBlockObject) *ButtonBlockElement {
return &ButtonBlockElement{
Expand Down Expand Up @@ -509,6 +520,32 @@ func NewPlainTextInputBlockElement(placeholder *TextBlockObject, actionID string
}
}

// RichTextInputBlockElement creates a field where allows users to enter formatted text
// in a WYSIWYG composer, offering the same messaging writing experience as in Slack
// More Information: https://api.slack.com/reference/block-kit/block-elements#rich_text_input
type RichTextInputBlockElement struct {
Type MessageElementType `json:"type"`
ActionID string `json:"action_id,omitempty"`
Placeholder *TextBlockObject `json:"placeholder,omitempty"`
InitialValue *RichTextBlock `json:"initial_value,omitempty"`
DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"`
FocusOnLoad bool `json:"focus_on_load,omitempty"`
}

// ElementType returns the type of the Element
func (s RichTextInputBlockElement) ElementType() MessageElementType {
return s.Type
}

// NewRichTextInputBlockElement returns an instance of a rich-text input element
func NewRichTextInputBlockElement(placeholder *TextBlockObject, actionID string) *RichTextInputBlockElement {
return &RichTextInputBlockElement{
Type: METRichTextInput,
ActionID: actionID,
Placeholder: placeholder,
}
}

// CheckboxGroupsBlockElement defines an element which allows users to choose
// one or more items from a list of possible options.
//
Expand Down Expand Up @@ -591,3 +628,40 @@ func NewNumberInputBlockElement(placeholder *TextBlockObject, actionID string, i
IsDecimalAllowed: isDecimalAllowed,
}
}

// FileInputBlockElement creates a field where a user can upload a file.
//
// File input elements are currently only available in modals.
//
// More Information: https://api.slack.com/reference/block-kit/block-elements#file_input
type FileInputBlockElement struct {
Type MessageElementType `json:"type"`
ActionID string `json:"action_id,omitempty"`
FileTypes []string `json:"filetypes,omitempty"`
MaxFiles int `json:"max_files,omitempty"`
}

// ElementType returns the type of the Element
func (s FileInputBlockElement) ElementType() MessageElementType {
return s.Type
}

// NewFileInputBlockElement returns an instance of a file input element
func NewFileInputBlockElement(actionID string) *FileInputBlockElement {
return &FileInputBlockElement{
Type: METFileInput,
ActionID: actionID,
}
}

// WithFileTypes sets the file types that can be uploaded
func (s *FileInputBlockElement) WithFileTypes(fileTypes ...string) *FileInputBlockElement {
s.FileTypes = fileTypes
return s
}

// WithMaxFiles sets the maximum number of files that can be uploaded
func (s *FileInputBlockElement) WithMaxFiles(maxFiles int) *FileInputBlockElement {
s.MaxFiles = maxFiles
return s
}
32 changes: 32 additions & 0 deletions block_element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func TestWithStyleForButtonElement(t *testing.T) {

}

func TestWithURLForButtonElement(t *testing.T) {

btnTxt := NewTextBlockObject("plain_text", "Next 2 Results", false, false)
btnElement := NewButtonBlockElement("test", "click_me_123", btnTxt)

btnElement.WithURL("https://foo.bar")
assert.Equal(t, btnElement.URL, "https://foo.bar")

}

func TestNewOptionsSelectBlockElement(t *testing.T) {

testOptionText := NewTextBlockObject("plain_text", "Option One", false, false)
Expand Down Expand Up @@ -148,6 +158,12 @@ func TestNewPlainTextInputBlockElement(t *testing.T) {

}

func TestNewRichTextInputBlockElement(t *testing.T) {
richTextInputElement := NewRichTextInputBlockElement(nil, "test")
assert.Equal(t, string(richTextInputElement.Type), "rich_text_input")
assert.Equal(t, richTextInputElement.ActionID, "test")
}

func TestNewEmailTextInputBlockElement(t *testing.T) {
emailTextInputElement := NewEmailTextInputBlockElement(nil, "[email protected]")

Expand Down Expand Up @@ -215,3 +231,19 @@ func TestNewNumberInputBlockElement(t *testing.T) {
assert.Equal(t, numberInputElement.IsDecimalAllowed, true)

}

func TestNewFileInputBlockElement(t *testing.T) {

fileInputElement := NewFileInputBlockElement("test")

assert.Equal(t, string(fileInputElement.Type), "file_input")
assert.Equal(t, fileInputElement.ActionID, "test")

fileInputElement.WithFileTypes("jpg", "png")
assert.Equal(t, len(fileInputElement.FileTypes), 2)
assert.Contains(t, fileInputElement.FileTypes, "jpg")
assert.Contains(t, fileInputElement.FileTypes, "png")

fileInputElement.WithMaxFiles(10)
assert.Equal(t, fileInputElement.MaxFiles, 10)
}
20 changes: 15 additions & 5 deletions block_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ package slack
//
// More Information: https://api.slack.com/reference/messaging/blocks#image
type ImageBlock struct {
Type MessageBlockType `json:"type"`
ImageURL string `json:"image_url"`
AltText string `json:"alt_text"`
BlockID string `json:"block_id,omitempty"`
Title *TextBlockObject `json:"title,omitempty"`
Type MessageBlockType `json:"type"`
ImageURL string `json:"image_url,omitempty"`
AltText string `json:"alt_text"`
BlockID string `json:"block_id,omitempty"`
Title *TextBlockObject `json:"title,omitempty"`
SlackFile *SlackFileObject `json:"slack_file,omitempty"`
}

// SlackFileObject Defines an object containing Slack file information to be used in an
// image block or image element.
//
// More Information: https://api.slack.com/reference/block-kit/composition-objects#slack_file
type SlackFileObject struct {
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
}

// BlockType returns the type of the block
Expand Down
Loading

0 comments on commit c198962

Please sign in to comment.