Skip to content

Commit

Permalink
everythingにはbotで投稿する
Browse files Browse the repository at this point in the history
  • Loading branch information
walnuts1018 committed Mar 31, 2024
1 parent 4e27102 commit f7695a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SLACK_USER_TOKEN = xoxp-1234567890-1234567890123-1234567890123-1234567890abcdefghijklmnopqrstuv
SLACK_BOT_TOKEN = xoxb-123456789012-1234567890123-1234567890123-1234567890abcdefghijklmnopqrstuv
SLACK_APP_TOKEN = xapp-1-ABCDEFGHIJK-1234567890123-1234567890123-1234567890abcdefghijklmnopqrstuvwxyz1234567890abcd
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type Config struct {
SlackUserToken string `env:"SLACK_USER_TOKEN,required"`
SlackAppToken string `env:"SLACK_APP_TOKEN,required"`
SlackBotToken string `env:"SLACK_BOT_TOKEN,required"`
SlackCacheDuration time.Duration `env:"SLACK_CACHE_DURATION" envDefault:"5m"`
SlackDetermineYearCacheDuration time.Duration `env:"SLACK_DETERMINE_YEAR_CACHE_DURATION" envDefault:"72h"`

Expand Down
38 changes: 21 additions & 17 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

type Client struct {
slackClient *slack.Client
socketClient *socketmode.Client
slackUserClient *slack.Client
socketUserClient *socketmode.Client
slackBotClient *slack.Client

cacheDuration time.Duration

Expand Down Expand Up @@ -49,17 +50,20 @@ func NewSlackClient(cfg config.Config) Client {
useDebug := cfg.LogLevel == slog.LevelDebug
logger := slog.Default()

slackClient := slack.New(cfg.SlackUserToken, slack.OptionDebug(useDebug), slack.OptionLog(SlackLogger{logger}), slack.OptionAppLevelToken(cfg.SlackAppToken))
slackUserClient := slack.New(cfg.SlackUserToken, slack.OptionDebug(useDebug), slack.OptionLog(SlackLogger{logger}), slack.OptionAppLevelToken(cfg.SlackAppToken))
slackBotClient := slack.New(cfg.SlackBotToken, slack.OptionDebug(useDebug), slack.OptionLog(SlackLogger{logger}), slack.OptionAppLevelToken(cfg.SlackAppToken))

socketClient := socketmode.New(
slackClient,
socketUserClient := socketmode.New(
slackUserClient,
socketmode.OptionDebug(useDebug),
socketmode.OptionLog(SlackLogger{logger}),
)

return Client{
slackClient: slackClient,
socketClient: socketClient,
slackUserClient: slackUserClient,
socketUserClient: socketUserClient,
slackBotClient: slackBotClient,

cacheDuration: cfg.SlackCacheDuration,

prefixedChannelCache: make(map[string]map[string]string, 2),
Expand All @@ -75,7 +79,7 @@ func NewSlackClient(cfg config.Config) Client {
func (c *Client) InviteUsersToChannels(ctx context.Context, channelIDs []string, userIDs []string) error {
for _, channelID := range channelIDs {
//joinしないと招待できない
_, warn, _, err := c.slackClient.JoinConversationContext(ctx, channelID)
_, warn, _, err := c.slackUserClient.JoinConversationContext(ctx, channelID)
if err != nil {
slog.Error("Error joining channel", "channelID", channelID, "error", err)
continue
Expand All @@ -85,7 +89,7 @@ func (c *Client) InviteUsersToChannels(ctx context.Context, channelIDs []string,
slog.Warn("Warning joining channel", "channelID", channelID, "warning", warn)
}

if _, err := c.slackClient.InviteUsersToConversationContext(ctx, channelID, userIDs...); err != nil && err.Error() != "already_in_channel" {
if _, err := c.slackUserClient.InviteUsersToConversationContext(ctx, channelID, userIDs...); err != nil && err.Error() != "already_in_channel" {
slog.Error("Error inviting user to channel", "channelID", channelID, "userIDs", userIDs, "error", err)
continue
}
Expand Down Expand Up @@ -133,7 +137,7 @@ func (c *Client) GetPublicChannels(ctx context.Context) ([]slack.Channel, error)
cursor := "dGVhbTpDMDRRV0ZGTkREMQ==" // 2023-general

for {
cs, nextCursor, err := c.slackClient.GetConversationsContext(ctx, &slack.GetConversationsParameters{
cs, nextCursor, err := c.slackUserClient.GetConversationsContext(ctx, &slack.GetConversationsParameters{
Types: []string{"public_channel"},
ExcludeArchived: true,
Cursor: cursor,
Expand Down Expand Up @@ -169,7 +173,7 @@ func (c *Client) GetAllMCGMembers(ctx context.Context, mustIncludeUsers ...strin
return c.mcgMemberCache, nil
}

users, err := c.slackClient.GetUsersContext(ctx)
users, err := c.slackUserClient.GetUsersContext(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -203,7 +207,7 @@ func (c *Client) ForwardMessage(ctx context.Context, everythingChannelID string,
return nil
}

profile, err := c.slackClient.GetUserProfileContext(ctx, &slack.GetUserProfileParameters{
profile, err := c.slackUserClient.GetUserProfileContext(ctx, &slack.GetUserProfileParameters{
UserID: message.User,
IncludeLabels: false,
})
Expand All @@ -220,7 +224,7 @@ func (c *Client) ForwardMessage(ctx context.Context, everythingChannelID string,
displayName = profile.DisplayName
}

permalink, err := c.slackClient.GetPermalinkContext(ctx, &slack.PermalinkParameters{
permalink, err := c.slackUserClient.GetPermalinkContext(ctx, &slack.PermalinkParameters{
Channel: message.Channel,
Ts: message.TimeStamp,
})
Expand Down Expand Up @@ -252,7 +256,7 @@ func (c *Client) ForwardMessage(ctx context.Context, everythingChannelID string,
}
}

_, _, err = c.slackClient.PostMessageContext(
_, _, err = c.slackBotClient.PostMessageContext(
ctx,
everythingChannelID,
slack.MsgOptionBlocks(blocks...),
Expand All @@ -264,7 +268,7 @@ func (c *Client) ForwardMessage(ctx context.Context, everythingChannelID string,
}

func (c *Client) HandleSlackEvents(ctx context.Context) {
for event := range c.socketClient.Events {
for event := range c.socketUserClient.Events {
slog.Debug("Event", "event", event)
switch event.Type {
case socketmode.EventTypeConnecting:
Expand All @@ -279,7 +283,7 @@ func (c *Client) HandleSlackEvents(ctx context.Context) {
slog.Debug("Ignored event", "event", event)
continue
}
c.socketClient.Ack(*event.Request)
c.socketUserClient.Ack(*event.Request)

slog.Debug("EventsAPIEvent", "eventsAPIEvent", eventsAPIEvent)
if eventsAPIEvent.Type == slackevents.CallbackEvent {
Expand Down Expand Up @@ -418,7 +422,7 @@ func (c *Client) HandleSlackEvents(ctx context.Context) {
}

func (c *Client) Listen(ctx context.Context) error {
return c.socketClient.Run()
return c.socketUserClient.Run()
}

type ObservTarget struct {
Expand Down

0 comments on commit f7695a9

Please sign in to comment.