Browse Source

Now able to detect replies when buttons are clicked and reply with a simple message

pull/1/head
rodley82 4 years ago
parent
commit
2c62b44482
  1. 52
      main.go

52
main.go

@ -52,13 +52,42 @@ func main() {
switch event.Type { switch event.Type {
// handle EventInteractive events // handle EventInteractive events
case socketmode.EventTypeInteractive: case socketmode.EventTypeInteractive:
fmt.Println("Recivimos un Interactive!", event) // fmt.Println("Recivimos un Interactive!", event)
eventsInteractive, ok := event.Data.(slackevents.EventsAPICallbackEvent)
callback, ok := event.Data.(slack.InteractionCallback)
if !ok { if !ok {
log.Printf("Could not type cast the event to the EventsAPICallbackEvent: %v\n", event) log.Printf("Could not type cast the event to the MessageAction: %v\n", event)
continue continue
} }
fmt.Println("Se pudo castear!!", eventsInteractive) socketClient.Ack(*event.Request)
switch callback.Type {
case slack.InteractionTypeInteractionMessage:
//fmt.Println("type:", callback.Type, "callBackId:", callback.CallbackID, "aactions[0]", *callback.ActionCallback.AttachmentActions[0])
action := *callback.ActionCallback.AttachmentActions[0]
//fmt.Println("Value:", action.Value)
switch action.Value {
case "poweron":
fmt.Println("Encender!")
case "poweroff":
fmt.Println("Apagar!")
}
replyToAction(callback.Channel.Name, "Chi chi chi chi amo!", client)
case slack.InteractionTypeMessageAction:
fmt.Println("is a message action?")
case slack.InteractionTypeBlockActions:
// See https://api.slack.com/apis/connections/socket-implement#button
fmt.Println("button clicked!")
case slack.InteractionTypeShortcut:
case slack.InteractionTypeViewSubmission:
// See https://api.slack.com/apis/connections/socket-implement#modal
case slack.InteractionTypeDialogSubmission:
default:
}
// handle EventAPI events // handle EventAPI events
case socketmode.EventTypeEventsAPI: case socketmode.EventTypeEventsAPI:
// The Event sent on the channel is not the same as the EventAPI events so we need to type cast it // The Event sent on the channel is not the same as the EventAPI events so we need to type cast it
@ -70,7 +99,7 @@ func main() {
// We need to send an Acknowledge to the slack server // We need to send an Acknowledge to the slack server
socketClient.Ack(*event.Request) socketClient.Ack(*event.Request)
// Now we have an Events API event, but this event type can in turn be many types, so we actually need another type switch // Now we have an Events API event, but this event type can in turn be many types, so we actually need another type switch
log.Println(eventsAPIEvent) // log.Println(eventsAPIEvent)
err := handleEventMessage(eventsAPIEvent, client) err := handleEventMessage(eventsAPIEvent, client)
if err != nil { if err != nil {
// Replace with actual err handeling // Replace with actual err handeling
@ -92,7 +121,7 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client)
case slackevents.CallbackEvent: case slackevents.CallbackEvent:
innerEvent := event.InnerEvent innerEvent := event.InnerEvent
fmt.Println("We received an event!", innerEvent) // fmt.Println("We received an event!", innerEvent)
// Yet Another Type switch on the actual Data to see if its an AppMentionEvent // Yet Another Type switch on the actual Data to see if its an AppMentionEvent
switch ev := innerEvent.Data.(type) { switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent: case *slackevents.AppMentionEvent:
@ -106,6 +135,17 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client)
return nil return nil
} }
func replyToAction(channelName string, message string, client *slack.Client) error {
attachment := slack.Attachment{}
attachment.Text = message
attachment.Color = "#4af030"
_, _, err := client.PostMessage(channelName, slack.MsgOptionAttachments(attachment))
if err != nil {
return fmt.Errorf("failed to post message: %w", err)
}
return nil
}
// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned // handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned
func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error { func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error {

Loading…
Cancel
Save