Skip to content

Commit

Permalink
Adding structure comments & definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Birtato committed Feb 14, 2024
1 parent fd5d171 commit 171a7b4
Show file tree
Hide file tree
Showing 5 changed files with 800 additions and 68 deletions.
54 changes: 44 additions & 10 deletions examples/modal/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"

"github.com/slack-go/slack"
"time"
)

func generateModalRequest() slack.ModalViewRequest {
Expand Down Expand Up @@ -60,6 +61,30 @@ func generateModalRequest() slack.ModalViewRequest {
return modalRequest
}

func updateModal() slack.ModalViewRequest {
// Create a ModalViewRequest with a header and two inputs
titleText := slack.NewTextBlockObject("plain_text", "My App", false, false)
closeText := slack.NewTextBlockObject("plain_text", "Close", false, false)
submitText := slack.NewTextBlockObject("plain_text", "Submit", false, false)

headerText := slack.NewTextBlockObject("mrkdwn", "Modal updated!", false, false)
headerSection := slack.NewSectionBlock(headerText, nil, nil)

blocks := slack.Blocks{
BlockSet: []slack.Block{
headerSection,
},
}

var modalRequest slack.ModalViewRequest
modalRequest.Type = slack.ViewType("modal")
modalRequest.Title = titleText
modalRequest.Close = closeText
modalRequest.Submit = submitText
modalRequest.Blocks = blocks
return modalRequest
}

// This was taken from the slash example
// https://github.com/slack-go/slack/blob/master/examples/slash/slash.go
func verifySigningSecret(r *http.Request) error {
Expand Down Expand Up @@ -104,7 +129,7 @@ func handleSlash(w http.ResponseWriter, r *http.Request) {
}

switch s.Command {
case "/humboldttest":
case "/slash":
api := slack.New("YOUR_TOKEN_HERE")
modalRequest := generateModalRequest()
_, err = api.OpenView(s.TriggerID, modalRequest)
Expand Down Expand Up @@ -134,21 +159,30 @@ func handleModal(w http.ResponseWriter, r *http.Request) {
return
}

// Note there might be a better way to get this info, but I figured this structure out from looking at the json response
firstName := i.View.State.Values["First Name"]["firstName"].Value
lastName := i.View.State.Values["Last Name"]["lastName"].Value

msg := fmt.Sprintf("Hello %s %s, nice to meet you!", firstName, lastName)

api := slack.New("YOUR_TOKEN_HERE")
_, _, err = api.PostMessage(i.User.ID,
slack.MsgOptionText(msg, false),
slack.MsgOptionAttachments())
if err != nil {
fmt.Printf(err.Error())
w.WriteHeader(http.StatusUnauthorized)
return
}

// update modal sample
switch i.Type {
//update when interaction type is view_submission
case slack.InteractionTypeViewSubmission:
//you can use any modal you want to show to users just like creating modal.
updateModal := updateModal()
// You must set one of external_id or view_id and you can use hash for avoiding race condition.
// More details: https://api.slack.com/surfaces/modals/using#updating_apis
_, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID)
// Wait for a few seconds to see result this code is necesarry due to slack server modal is going to be closed after the update
time.Sleep(time.Second * 2)
if err != nil {
fmt.Printf("Error updating view: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}

func main() {
Expand Down
191 changes: 152 additions & 39 deletions slackevents/inner_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ type MessageEvent struct {
PreviousMessage *MessageEvent `json:"previous_message,omitempty"`
Edited *Edited `json:"edited,omitempty"`

// Deleted Message
DeletedTimeStamp string `json:"deleted_ts,omitempty"`

// Message Subtypes
SubType string `json:"subtype,omitempty"`

Expand Down Expand Up @@ -548,6 +551,104 @@ type UserProfileChangedEvent struct {
EventTs string `json:"event_ts"`
}

// SharedChannelInviteApprovedEvent is sent if your invitation has been approved
type SharedChannelInviteApprovedEvent struct {
Type string `json:"type"`
Invite *SharedInvite `json:"invite"`
Channel *slack.Conversation `json:"channel"`
ApprovingTeamID string `json:"approving_team_id"`
TeamsInChannel []*SlackEventTeam `json:"teams_in_channel"`
ApprovingUser *SlackEventUser `json:"approving_user"`
EventTs string `json:"event_ts"`
}

// SharedChannelInviteAcceptedEvent is sent if external org accepts a Slack Connect channel invite
type SharedChannelInviteAcceptedEvent struct {
Type string `json:"type"`
ApprovalRequired bool `json:"approval_required"`
Invite *SharedInvite `json:"invite"`
Channel *SharedChannel `json:"channel"`
TeamsInChannel []*SlackEventTeam `json:"teams_in_channel"`
AcceptingUser *SlackEventUser `json:"accepting_user"`
EventTs string `json:"event_ts"`
RequiresSponsorship bool `json:"requires_sponsorship,omitempty"`
}

// SharedChannelInviteDeclinedEvent is sent if external or internal org declines the Slack Connect invite
type SharedChannelInviteDeclinedEvent struct {
Type string `json:"type"`
Invite *SharedInvite `json:"invite"`
Channel *SharedChannel `json:"channel"`
DecliningTeamID string `json:"declining_team_id"`
TeamsInChannel []*SlackEventTeam `json:"teams_in_channel"`
DecliningUser *SlackEventUser `json:"declining_user"`
EventTs string `json:"event_ts"`
}

// SharedChannelInviteReceivedEvent is sent if a bot or app is invited to a Slack Connect channel
type SharedChannelInviteReceivedEvent struct {
Type string `json:"type"`
Invite *SharedInvite `json:"invite"`
Channel *SharedChannel `json:"channel"`
EventTs string `json:"event_ts"`
}

// SlackEventTeam is a struct for teams in ShareChannel events
type SlackEventTeam struct {
ID string `json:"id"`
Name string `json:"name"`
Icon *SlackEventIcon `json:"icon,omitempty"`
AvatarBaseURL string `json:"avatar_base_url,omitempty"`
IsVerified bool `json:"is_verified"`
Domain string `json:"domain"`
DateCreated int `json:"date_created"`
RequiresSponsorship bool `json:"requires_sponsorship,omitempty"`
// TeamID string `json:"team_id,omitempty"`
}

// SlackEventIcon is a struct for icons in ShareChannel events
type SlackEventIcon struct {
ImageDefault bool `json:"image_default,omitempty"`
Image34 string `json:"image_34,omitempty"`
Image44 string `json:"image_44,omitempty"`
Image68 string `json:"image_68,omitempty"`
Image88 string `json:"image_88,omitempty"`
Image102 string `json:"image_102,omitempty"`
Image132 string `json:"image_132,omitempty"`
Image230 string `json:"image_230,omitempty"`
}

// SlackEventUser is a struct for users in ShareChannel events
type SlackEventUser struct {
ID string `json:"id"`
TeamID string `json:"team_id"`
Name string `json:"name"`
Updated int `json:"updated,omitempty"`
Profile *slack.UserProfile `json:"profile,omitempty"`
WhoCanShareContactCard string `json:"who_can_share_contact_card,omitempty"`
}

// SharedChannel is a struct for shared channels in ShareChannel events
type SharedChannel struct {
ID string `json:"id"`
IsPrivate bool `json:"is_private"`
IsIm bool `json:"is_im"`
Name string `json:"name,omitempty"`
}

// SharedInvite is a struct for shared invites in ShareChannel events
type SharedInvite struct {
ID string `json:"id"`
DateCreated int `json:"date_created"`
DateInvalid int `json:"date_invalid"`
InvitingTeam *SlackEventTeam `json:"inviting_team,omitempty"`
InvitingUser *SlackEventUser `json:"inviting_user,omitempty"`
RecipientEmail string `json:"recipient_email,omitempty"`
RecipientUserID string `json:"recipient_user_id,omitempty"`
IsSponsored bool `json:"is_sponsored,omitempty"`
IsExternalLimited bool `json:"is_external_limited,omitempty"`
}

type EventsAPIType string

const (
Expand Down Expand Up @@ -611,6 +712,14 @@ const (
ReactionRemoved = EventsAPIType("reaction_removed")
// TeamJoin A new user joined the workspace
TeamJoin = EventsAPIType("team_join")
// Slack connect app or bot invite received
SharedChannelInviteReceived = EventsAPIType("shared_channel_invite_received")
// Slack connect channel invite approved
SharedChannelInviteApproved = EventsAPIType("shared_channel_invite_approved")
// Slack connect channel invite declined
SharedChannelInviteDeclined = EventsAPIType("shared_channel_invite_declined")
// Slack connect channel invite accepted by an end user
SharedChannelInviteAccepted = EventsAPIType("shared_channel_invite_accepted")
// TokensRevoked APP's API tokes are revoked
TokensRevoked = EventsAPIType("tokens_revoked")
// EmojiChanged A custom emoji has been added or changed
Expand All @@ -635,43 +744,47 @@ const (
// implementations. The structs should be instances of the unmarshalling
// target for the matching event type.
var EventsAPIInnerEventMapping = map[EventsAPIType]interface{}{
AppMention: AppMentionEvent{},
AppHomeOpened: AppHomeOpenedEvent{},
AppUninstalled: AppUninstalledEvent{},
ChannelCreated: ChannelCreatedEvent{},
ChannelDeleted: ChannelDeletedEvent{},
ChannelArchive: ChannelArchiveEvent{},
ChannelUnarchive: ChannelUnarchiveEvent{},
ChannelLeft: ChannelLeftEvent{},
ChannelRename: ChannelRenameEvent{},
ChannelIDChanged: ChannelIDChangedEvent{},
FileChange: FileChangeEvent{},
FileDeleted: FileDeletedEvent{},
FileShared: FileSharedEvent{},
FileUnshared: FileUnsharedEvent{},
GroupDeleted: GroupDeletedEvent{},
GroupArchive: GroupArchiveEvent{},
GroupUnarchive: GroupUnarchiveEvent{},
GroupLeft: GroupLeftEvent{},
GroupRename: GroupRenameEvent{},
GridMigrationFinished: GridMigrationFinishedEvent{},
GridMigrationStarted: GridMigrationStartedEvent{},
LinkShared: LinkSharedEvent{},
Message: MessageEvent{},
MemberJoinedChannel: MemberJoinedChannelEvent{},
MemberLeftChannel: MemberLeftChannelEvent{},
PinAdded: PinAddedEvent{},
PinRemoved: PinRemovedEvent{},
ReactionAdded: ReactionAddedEvent{},
ReactionRemoved: ReactionRemovedEvent{},
TeamJoin: TeamJoinEvent{},
TokensRevoked: TokensRevokedEvent{},
EmojiChanged: EmojiChangedEvent{},
WorkflowStepExecute: WorkflowStepExecuteEvent{},
MessageMetadataPosted: MessageMetadataPostedEvent{},
MessageMetadataUpdated: MessageMetadataUpdatedEvent{},
MessageMetadataDeleted: MessageMetadataDeletedEvent{},
TeamAccessGranted: TeamAccessGrantedEvent{},
TeamAccessRevoked: TeamAccessRevokedEvent{},
UserProfileChanged: UserProfileChangedEvent{},
AppMention: AppMentionEvent{},
AppHomeOpened: AppHomeOpenedEvent{},
AppUninstalled: AppUninstalledEvent{},
ChannelCreated: ChannelCreatedEvent{},
ChannelDeleted: ChannelDeletedEvent{},
ChannelArchive: ChannelArchiveEvent{},
ChannelUnarchive: ChannelUnarchiveEvent{},
ChannelLeft: ChannelLeftEvent{},
ChannelRename: ChannelRenameEvent{},
ChannelIDChanged: ChannelIDChangedEvent{},
FileChange: FileChangeEvent{},
FileDeleted: FileDeletedEvent{},
FileShared: FileSharedEvent{},
FileUnshared: FileUnsharedEvent{},
GroupDeleted: GroupDeletedEvent{},
GroupArchive: GroupArchiveEvent{},
GroupUnarchive: GroupUnarchiveEvent{},
GroupLeft: GroupLeftEvent{},
GroupRename: GroupRenameEvent{},
GridMigrationFinished: GridMigrationFinishedEvent{},
GridMigrationStarted: GridMigrationStartedEvent{},
LinkShared: LinkSharedEvent{},
Message: MessageEvent{},
MemberJoinedChannel: MemberJoinedChannelEvent{},
MemberLeftChannel: MemberLeftChannelEvent{},
PinAdded: PinAddedEvent{},
PinRemoved: PinRemovedEvent{},
ReactionAdded: ReactionAddedEvent{},
ReactionRemoved: ReactionRemovedEvent{},
SharedChannelInviteApproved: SharedChannelInviteApprovedEvent{},
SharedChannelInviteAccepted: SharedChannelInviteAcceptedEvent{},
SharedChannelInviteDeclined: SharedChannelInviteDeclinedEvent{},
SharedChannelInviteReceived: SharedChannelInviteReceivedEvent{},
TeamJoin: TeamJoinEvent{},
TokensRevoked: TokensRevokedEvent{},
EmojiChanged: EmojiChangedEvent{},
WorkflowStepExecute: WorkflowStepExecuteEvent{},
MessageMetadataPosted: MessageMetadataPostedEvent{},
MessageMetadataUpdated: MessageMetadataUpdatedEvent{},
MessageMetadataDeleted: MessageMetadataDeletedEvent{},
TeamAccessGranted: TeamAccessGrantedEvent{},
TeamAccessRevoked: TeamAccessRevokedEvent{},
UserProfileChanged: UserProfileChangedEvent{},
}

0 comments on commit 171a7b4

Please sign in to comment.