slacker icon indicating copy to clipboard operation
slacker copied to clipboard

Retrieve Trigger ID from Slash Commands

Open 0xArch3r opened this issue 2 years ago • 2 comments

I am trying to retrieve the Trigger ID from a Slash Command so that I can open a Modal. I noticed in the message handler when an event matches slash command, the Data prop is given the req object instead of the evt obj like the other Message Events. This means I can no longer pull the trigger_id from the event.

The slash command is the only type that uses request for the data prop instead of the evt

0xArch3r avatar Sep 09 '22 00:09 0xArch3r

func newMessageEvent(slacker *Slacker, evt interface{}, req *socketmode.Request) *MessageEvent {
	var me *MessageEvent

	switch ev := evt.(type) {
	case *slackevents.MessageEvent:
		me = &MessageEvent{
			Channel:         ev.Channel,
			ChannelName:     getChannelName(slacker, ev.Channel),
			User:            ev.User,
			UserName:        getUserName(slacker, ev.User),
			Text:            ev.Text,
			Data:            evt,
			Type:            ev.Type,
			TimeStamp:       ev.TimeStamp,
			ThreadTimeStamp: ev.ThreadTimeStamp,
			BotID:           ev.BotID,
		}
	case *slackevents.AppMentionEvent:
		me = &MessageEvent{
			Channel:         ev.Channel,
			ChannelName:     getChannelName(slacker, ev.Channel),
			User:            ev.User,
			UserName:        getUserName(slacker, ev.User),
			Text:            ev.Text,
			Data:            evt,
			Type:            ev.Type,
			TimeStamp:       ev.TimeStamp,
			ThreadTimeStamp: ev.ThreadTimeStamp,
			BotID:           ev.BotID,
		}
	case *slack.SlashCommand:
		me = &MessageEvent{
			Channel:     ev.ChannelID,
			ChannelName: ev.ChannelName,
			User:        ev.UserID,
			UserName:    ev.UserName,
			Text:        fmt.Sprintf("%s %s", ev.Command[1:], ev.Text),
			Data:        req,
			Type:        req.Type,
		}
	}

0xArch3r avatar Sep 09 '22 00:09 0xArch3r

I was able to work around this by changing the definition of MessageEvent to the following:

`type MessageEvent struct { // Channel ID where the message was sent Channel string

// ChannelName where the message was sent
ChannelName string

// User ID of the sender
User string

// UserName of the the sender
UserName string

// Text is the unalterted text of the message, as returned by Slack
Text string

// TimeStamp is the message timestamp. For events that do not support
// threading (eg. slash commands) this will be unset.
// will be left unset.
TimeStamp string

// ThreadTimeStamp is the message thread timestamp. For events that do not
// support threading (eg. slash commands) this will be unset.
ThreadTimeStamp string

// Data is the raw event data returned from slack. Using Type, you can assert
// this into a slackevents *Event struct.
Data interface{}

// Type is the type of the event, as returned by Slack. For instance,
// `app_mention` or `message`
Type string

// BotID of the bot that sent this message. If a bot did not send this
// message, this will be an empty string.
BotID string

Event interface{}

}`

This allows me to now access the original event through the botCtx.Event().Event

0xArch3r avatar Sep 09 '22 01:09 0xArch3r

This should now be resolved in this release https://github.com/shomali11/slacker/releases/tag/v2.0.0-alpha1

shomali11 avatar Jun 24 '23 02:06 shomali11