|
|
@ -8,7 +8,6 @@ import ( |
|
|
"os" |
|
|
"os" |
|
|
"os/exec" |
|
|
"os/exec" |
|
|
"rodleyserverbot/slack-bot/config" |
|
|
"rodleyserverbot/slack-bot/config" |
|
|
"strings" |
|
|
|
|
|
"github.com/slack-go/slack" |
|
|
"github.com/slack-go/slack" |
|
|
"github.com/slack-go/slack/slackevents" |
|
|
"github.com/slack-go/slack/slackevents" |
|
|
"github.com/slack-go/slack/socketmode" |
|
|
"github.com/slack-go/slack/socketmode" |
|
|
@ -19,7 +18,7 @@ func Start() { |
|
|
// Set debug to true while developing
|
|
|
// Set debug to true while developing
|
|
|
// Also add a ApplicationToken option to the client
|
|
|
// Also add a ApplicationToken option to the client
|
|
|
client := slack.New( |
|
|
client := slack.New( |
|
|
config.Config.SlackAppToken, |
|
|
config.Config.SlackAuthToken, |
|
|
slack.OptionDebug(false), |
|
|
slack.OptionDebug(false), |
|
|
slack.OptionAppLevelToken(config.Config.SlackAppToken)) |
|
|
slack.OptionAppLevelToken(config.Config.SlackAppToken)) |
|
|
// go-slack comes with a SocketMode package that we need to use that accepts a Slack client and outputs a Socket mode client instead
|
|
|
// go-slack comes with a SocketMode package that we need to use that accepts a Slack client and outputs a Socket mode client instead
|
|
|
@ -49,7 +48,7 @@ func Start() { |
|
|
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)
|
|
|
|
|
|
|
|
|
callback, ok := event.Data.(slack.InteractionCallback) |
|
|
callback, ok := event.Data.(slack.InteractionCallback) |
|
|
|
|
|
|
|
|
@ -75,7 +74,7 @@ func Start() { |
|
|
} |
|
|
} |
|
|
if err == nil { |
|
|
if err == nil { |
|
|
finalMessage := fmt.Sprintf("Chi chi chi chi amo! Output: %s", output) |
|
|
finalMessage := fmt.Sprintf("Chi chi chi chi amo! Output: %s", output) |
|
|
replyToAction(callback.Channel.Name, finalMessage, client) |
|
|
replyToAction(callback.Channel.ID, finalMessage, client) |
|
|
} |
|
|
} |
|
|
case slack.InteractionTypeMessageAction: |
|
|
case slack.InteractionTypeMessageAction: |
|
|
//fmt.Println("is a message action?")
|
|
|
//fmt.Println("is a message action?")
|
|
|
@ -89,25 +88,27 @@ func Start() { |
|
|
// See https://api.slack.com/apis/connections/socket-implement#modal
|
|
|
// See https://api.slack.com/apis/connections/socket-implement#modal
|
|
|
case slack.InteractionTypeDialogSubmission: |
|
|
case slack.InteractionTypeDialogSubmission: |
|
|
default: |
|
|
default: |
|
|
|
|
|
//fmt.Println("Don't know what type it is")
|
|
|
} |
|
|
} |
|
|
// 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
|
|
|
eventsAPIEvent, ok := event.Data.(slackevents.EventsAPIEvent) |
|
|
eventsAPIEvent, ok := event.Data.(slackevents.EventsAPIEvent) |
|
|
if !ok { |
|
|
if !ok { |
|
|
log.Printf("Could not type cast the event to the EventsAPIEvent: %v\n", event) |
|
|
// log.Printf("Could not type cast the event to the EventsAPIEvent: %v\n", event)
|
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
// 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:", 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
|
|
|
log.Fatal(err) |
|
|
// log.Fatal(err)
|
|
|
} |
|
|
} |
|
|
|
|
|
default: |
|
|
|
|
|
//fmt.Println("Event received no matching TYPE:", event.Type, "event:", event)
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
@ -129,8 +130,13 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client) |
|
|
switch ev := innerEvent.Data.(type) { |
|
|
switch ev := innerEvent.Data.(type) { |
|
|
case *slackevents.AppMentionEvent: |
|
|
case *slackevents.AppMentionEvent: |
|
|
// The application has been mentioned since this Event is a Mention event
|
|
|
// The application has been mentioned since this Event is a Mention event
|
|
|
log.Println(ev) |
|
|
//log.Println("App Mentioned!", ev)
|
|
|
handleAppMentionEvent(ev, client) |
|
|
handleAppMentionEvent(ev, client) |
|
|
|
|
|
case *slackevents.MessageEvent: |
|
|
|
|
|
//log.Println("MessageEvent!", ev)
|
|
|
|
|
|
handleAppMessagedEvent(ev, client) |
|
|
|
|
|
default: |
|
|
|
|
|
//fmt.Println("Not handled EventMessage!:", ev)
|
|
|
} |
|
|
} |
|
|
default: |
|
|
default: |
|
|
return errors.New("unsupported event type") |
|
|
return errors.New("unsupported event type") |
|
|
@ -168,49 +174,68 @@ func executeAction(actionName string) (string, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func replyToAction(channelName string, message string, client *slack.Client) error { |
|
|
func replyToAction(channelName string, message string, client *slack.Client) error { |
|
|
|
|
|
//fmt.Println("replyToAction channel:", channelName, "Message:", message)
|
|
|
attachment := slack.Attachment{} |
|
|
attachment := slack.Attachment{} |
|
|
attachment.Text = message |
|
|
attachment.Text = message |
|
|
attachment.Color = "#4af030" |
|
|
attachment.Color = "#4af030" |
|
|
_, _, err := client.PostMessage(channelName, slack.MsgOptionAttachments(attachment)) |
|
|
_, _, err := client.PostMessage(channelName, slack.MsgOptionAttachments(attachment)) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return fmt.Errorf("failed to post message: %w", err) |
|
|
//fmt.Println("failed to post message:", err)
|
|
|
|
|
|
return err |
|
|
} |
|
|
} |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned
|
|
|
func handleAppMessagedEvent(event *slackevents.MessageEvent, client *slack.Client) error { |
|
|
func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error { |
|
|
//fmt.Println("handleAppMessagedEvent event:", event, "BotID:", event.BotID)
|
|
|
|
|
|
if event.BotID != "" { |
|
|
|
|
|
// We're not interested in messages from ourselves or other bots
|
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
attachment := slack.Attachment{} |
|
|
|
|
|
var err error |
|
|
|
|
|
attachment.CallbackID = "server_action" |
|
|
|
|
|
// Send a message to the user
|
|
|
|
|
|
attachment.Text = "Por el momento esto es lo que se hacer" |
|
|
|
|
|
attachment.Pretext = fmt.Sprintf("Como te puedo ayudar?") |
|
|
|
|
|
attachment.Color = "#3d3d3d" |
|
|
|
|
|
powerOnAction := slack.AttachmentAction{} |
|
|
|
|
|
powerOnAction.Name = "poweron" |
|
|
|
|
|
powerOnAction.Text = "Encender" |
|
|
|
|
|
powerOnAction.Value = "poweron" |
|
|
|
|
|
powerOnAction.Type = "button" |
|
|
|
|
|
|
|
|
|
|
|
powerOffAction := slack.AttachmentAction{} |
|
|
|
|
|
powerOffAction.Name = "poweroff" |
|
|
|
|
|
powerOffAction.Text = "Apagar" |
|
|
|
|
|
powerOffAction.Value = "poweroff" |
|
|
|
|
|
powerOffAction.Type = "button" |
|
|
|
|
|
powerOffAction.Style = "danger" |
|
|
|
|
|
|
|
|
// Grab the user name based on the ID of the one who mentioned the bot
|
|
|
actions := []slack.AttachmentAction{powerOnAction, powerOffAction} |
|
|
user, err := client.GetUserInfo(event.User) |
|
|
attachment.Actions = actions |
|
|
|
|
|
|
|
|
|
|
|
// Send the message to the channel
|
|
|
|
|
|
// The Channel is available in the event message
|
|
|
|
|
|
_, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment)) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
return fmt.Errorf("failed to post message: %w", err) |
|
|
} |
|
|
} |
|
|
// Check if the user said Hello to the bot
|
|
|
return nil |
|
|
text := strings.ToLower(event.Text) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned
|
|
|
|
|
|
func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error { |
|
|
|
|
|
// fmt.Println("handleAppMentionEvent event:", event)
|
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
// Create the attachment and assigned based on the message
|
|
|
// Create the attachment and assigned based on the message
|
|
|
attachment := slack.Attachment{} |
|
|
attachment := slack.Attachment{} |
|
|
// Add Some default context like user who mentioned the bot
|
|
|
|
|
|
// attachment.Fields = []slack.AttachmentField{
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Title: "Date",
|
|
|
|
|
|
// Value: time.Now().String(),
|
|
|
|
|
|
// }, {
|
|
|
|
|
|
// Title: "Initializer",
|
|
|
|
|
|
// Value: user.Name,
|
|
|
|
|
|
// },
|
|
|
|
|
|
// }
|
|
|
|
|
|
attachment.CallbackID = "server_action" |
|
|
attachment.CallbackID = "server_action" |
|
|
if strings.Contains(text, "hello") { |
|
|
|
|
|
// Greet the user
|
|
|
|
|
|
attachment.Text = fmt.Sprintf("Hello %s", user.Name) |
|
|
|
|
|
attachment.Pretext = "Greetings" |
|
|
|
|
|
attachment.Color = "#4af030" |
|
|
|
|
|
} else { |
|
|
|
|
|
// Send a message to the user
|
|
|
// Send a message to the user
|
|
|
attachment.Text = "Por el momento esto es lo que se hacer" |
|
|
attachment.Text = "Por el momento esto es lo que se hacer" |
|
|
attachment.Pretext = fmt.Sprintf("Como te puedo ayudar %s?", user.Name) |
|
|
attachment.Pretext = fmt.Sprintf("Como te puedo ayudar?") |
|
|
attachment.Color = "#3d3d3d" |
|
|
attachment.Color = "#3d3d3d" |
|
|
powerOnAction := slack.AttachmentAction{} |
|
|
powerOnAction := slack.AttachmentAction{} |
|
|
powerOnAction.Name = "poweron" |
|
|
powerOnAction.Name = "poweron" |
|
|
@ -229,7 +254,6 @@ func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Cli |
|
|
//fmt.Println(actions)
|
|
|
//fmt.Println(actions)
|
|
|
attachment.Actions = actions |
|
|
attachment.Actions = actions |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
// Send the message to the channel
|
|
|
// Send the message to the channel
|
|
|
// The Channel is available in the event message
|
|
|
// The Channel is available in the event message
|
|
|
_, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment)) |
|
|
_, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment)) |
|
|
|