Skip to content

Commit

Permalink
Add stamp names to reply
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Jul 5, 2024
1 parent 5c64269 commit 92576ac
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/bot/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (c *CommandInstance) Execute(ctx domain.Context) error {
err := cmd.Run()
if err != nil {
return ctx.ReplyFailure(
fmt.Sprintf("exec failed: %v", err),
fmt.Sprintf(":%s: exec failed: %v", ctx.StampNames().Failure, err),
"```",
utils.LimitLog(utils.SafeConvertString(buf.Bytes()), logLimit),
"```",
Expand All @@ -237,6 +237,7 @@ func (c *CommandInstance) Execute(ctx domain.Context) error {

var replyMessage []string
if buf.Len() > 0 {
replyMessage = append(replyMessage, fmt.Sprintf(":%s:", ctx.StampNames().Success))
replyMessage = append(replyMessage, "```")
replyMessage = append(replyMessage, utils.LimitLog(utils.SafeConvertString(buf.Bytes()), logLimit))
replyMessage = append(replyMessage, "```")
Expand Down
10 changes: 10 additions & 0 deletions pkg/bot/slack/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func (ctx *slackContext) MessageLimit() int {
return 2500
}

func (ctx *slackContext) StampNames() *domain.StampNames {
return &domain.StampNames{
BadCommand: config.C.Stamps.BadCommand,
Forbid: config.C.Stamps.Forbid,
Success: config.C.Stamps.Success,
Failure: config.C.Stamps.Failure,
Running: config.C.Stamps.Running,
}
}

func (ctx *slackContext) sendSlackMessage(channelID string, text string) error {
api := ctx.api
return utils.WithRetry(ctx, 10, func(ctx context.Context) error {
Expand Down
46 changes: 40 additions & 6 deletions pkg/bot/traq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"
"github.com/kballard/go-shellquote"
"github.com/samber/lo"
"github.com/traPtitech/DevOpsBot/pkg/config"
"github.com/traPtitech/DevOpsBot/pkg/domain"
"github.com/traPtitech/go-traq"
traqwsbot "github.com/traPtitech/traq-ws-bot"
"github.com/traPtitech/traq-ws-bot/payload"
"go.uber.org/zap"
Expand All @@ -25,13 +27,37 @@ func NewBot(rootCmd domain.Command, logger *zap.Logger) (domain.Bot, error) {
if err != nil {
return nil, fmt.Errorf("creating bot: %w", err)
}
bot.OnMessageCreated(botMessageReceived(bot, logger, rootCmd))

stampNames, err := resolveStampNames(context.Background(), bot)
if err != nil {
return nil, fmt.Errorf("resolving stamp names: %w", err)
}
bot.OnMessageCreated(botMessageReceived(bot, logger, stampNames, rootCmd))

return &traqBot{
bot: bot,
}, nil
}

func resolveStampNames(ctx context.Context, bot *traqwsbot.Bot) (*domain.StampNames, error) {
stamps, _, err := bot.API().StampApi.GetStamps(ctx).Execute()
if err != nil {
return nil, err
}

idToName := lo.SliceToMap(stamps, func(s traq.StampWithThumbnail) (string, string) {
return s.Id, s.Name
})

return &domain.StampNames{
BadCommand: idToName[config.C.Stamps.BadCommand],
Forbid: idToName[config.C.Stamps.Forbid],
Success: idToName[config.C.Stamps.Success],
Failure: idToName[config.C.Stamps.Failure],
Running: idToName[config.C.Stamps.Running],
}, nil
}

func (b *traqBot) Start(_ context.Context) error {
err := b.bot.Start()
if err != nil {
Expand All @@ -41,7 +67,12 @@ func (b *traqBot) Start(_ context.Context) error {
}

// botMessageReceived BOTのMESSAGE_CREATEDイベントハンドラ
func botMessageReceived(bot *traqwsbot.Bot, logger *zap.Logger, rootCmd domain.Command) func(p *payload.MessageCreated) {
func botMessageReceived(
bot *traqwsbot.Bot,
logger *zap.Logger,
stampNames *domain.StampNames,
rootCmd domain.Command,
) func(p *payload.MessageCreated) {
return func(p *payload.MessageCreated) {
// Validate command execution context
if p.Message.User.Bot {
Expand All @@ -57,10 +88,13 @@ func botMessageReceived(bot *traqwsbot.Bot, logger *zap.Logger, rootCmd domain.C
// Prepare command args
ctx := &traqContext{
Context: context.Background(),
api: bot.API(),
logger: logger,
p: p,
args: nil,

api: bot.API(),
logger: logger,
stampNames: stampNames,

p: p,
args: nil,
}
prefixStripped := strings.TrimPrefix(p.Message.PlainText, config.C.Prefix)
args, err := shellquote.Split(prefixStripped)
Expand Down
10 changes: 8 additions & 2 deletions pkg/bot/traq/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
type traqContext struct {
context.Context

api *traq.APIClient
logger *zap.Logger
api *traq.APIClient
logger *zap.Logger
stampNames *domain.StampNames

// p BOTが受信したMESSAGE_CREATEDイベントの生のペイロード
p *payload.MessageCreated
args []string
Expand Down Expand Up @@ -49,6 +51,10 @@ func (ctx *traqContext) MessageLimit() int {
return 9900
}

func (ctx *traqContext) StampNames() *domain.StampNames {
return ctx.stampNames
}

// sendTRAQMessage traQにメッセージ送信
func (ctx *traqContext) sendTRAQMessage(channelID string, text string) error {
api := ctx.api
Expand Down
11 changes: 11 additions & 0 deletions pkg/domain/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ type Bot interface {
Start(ctx context.Context) error
}

type StampNames struct {
BadCommand string
Forbid string
Success string
Failure string
Running string
}

// Context コマンド実行コンテキスト
type Context interface {
context.Context
Expand All @@ -23,7 +31,10 @@ type Context interface {

// L returns logger.
L() *zap.Logger
// MessageLimit returns reply character limit.
MessageLimit() int
// StampNames returns list of stamp names which can be used in raw text.
StampNames() *StampNames

// ReplyBad コマンドメッセージにBadスタンプをつけて返信します
ReplyBad(message ...string) error
Expand Down

0 comments on commit 92576ac

Please sign in to comment.