Files
houston-be/cmd/app/handler/slack_handler.go
Shubham Kirve b974cb6bf3 TP-0000 | Initialize houston repo (#1)
* TP-0000 | intialize houston repo

* TP-0000 | intialize houston repo
2023-03-29 00:01:17 +05:30

96 lines
3.3 KiB
Go

package handler
import (
"houston/pkg/slack/houston"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
"gorm.io/gorm"
)
type slackHandler struct {
logger *zap.Logger
socketModeClient *socketmode.Client
slackClient *slack.Client
db *gorm.DB
houstonCommandHandler *houston.HoustonCommandHandler
}
func NewSlackHandler(logger *zap.Logger, socketModeClient *socketmode.Client,
db *gorm.DB,
slackClient *slack.Client) *slackHandler {
return &slackHandler{
logger: logger,
socketModeClient: socketModeClient,
db: db,
slackClient: slackClient,
houstonCommandHandler: houston.NewHoustonCommandHandler(socketModeClient, logger, db),
}
}
func (sh *slackHandler) HoustonConnect() {
go func() {
for evt := range sh.socketModeClient.Events {
switch evt.Type {
case socketmode.EventTypeConnecting:
sh.logger.Info("houston connecting to slack with socket mode ...")
case socketmode.EventTypeConnectionError:
sh.logger.Error("Blazelss connection failed. retrying later ...")
case socketmode.EventTypeConnected:
sh.logger.Info("houston connected to slack with socket mode.")
case socketmode.EventTypeEventsAPI:
ev, _ := evt.Data.(slackevents.EventsAPIEvent)
sh.logger.Info("event api", zap.Any("ev", ev))
switch ev.Type {
case slackevents.CallbackEvent:
iev := ev.InnerEvent
switch ev := iev.Data.(type) {
case *slackevents.AppMentionEvent:
case *slackevents.MemberJoinedChannelEvent:
sh.houstonCommandHandler.ProcessMemberJoinEvent(ev, evt.Request)
}
case slackevents.URLVerification:
case string(slackevents.MemberJoinedChannel):
}
case socketmode.EventTypeInteractive:
callback, _ := evt.Data.(slack.InteractionCallback)
switch callback.Type {
case slack.InteractionTypeBlockActions:
sh.logger.Info("received interaction type block action",
zap.String("action_id", callback.ActionID), zap.String("block_id", callback.BlockID))
sh.houstonCommandHandler.ProcessButtonHandler(callback, evt.Request)
case slack.InteractionTypeShortcut:
case slack.InteractionTypeViewSubmission:
sh.logger.Info("received interaction type view submission",
zap.String("action_id", callback.ActionID), zap.String("block_id", callback.BlockID))
sh.logger.Info("payload data", zap.Any("callback", callback), zap.Any("request", evt.Request))
sh.houstonCommandHandler.ProcessModalCallbackEvent(callback, evt.Request)
case slack.InteractionTypeDialogSubmission:
default:
}
// command.ProcessStartIncidentCommand(client, evt.Request, callback.TriggerID)
case socketmode.EventTypeSlashCommand:
cmd, _ := evt.Data.(slack.SlashCommand)
sh.logger.Info("houston processing slash command",
zap.String("command", cmd.Text), zap.String("channel_name", cmd.ChannelName), zap.String("user_name", cmd.UserName))
sh.houstonCommandHandler.ProcessSlashCommand(evt)
// command.ProcessMainCommand(client, evt.Request)
// client.Ack(*evt.Request, payload)
default:
sh.logger.Error("houston unexpected event type received", zap.Any("event_type", evt.Type))
}
}
}()
go sh.socketModeClient.Run()
}