package models import ( "fmt" "github.com/slack-go/slack" ) const ( MsgSrcApp = "app" MsgSrcSlack = "slack" MsgTpStatus = "status" MsgTpControl = "control" MsgTpError = "error" MsgTpMessage = "message" MsgTextQuit = "quit" ) type BotMessage struct { Source string Dest string Type string Target string Text string } var QuitMessage = BotMessage{ Source: MsgSrcApp, Dest: MsgSrcApp, Type: MsgTpControl, Target: MsgSrcApp, Text: MsgTextQuit, } func NewBotMessage(src, dst, tp, tgt, msg string) BotMessage { return BotMessage{ Source: src, Dest: dst, Type: tp, Target: tgt, Text: msg, } } func NewErrorBotMessage(src, tgt, msg string) BotMessage { return BotMessage{ Source: src, Dest: MsgSrcApp, Type: MsgTpError, Target: tgt, Text: msg, } } func BotMessageFromSlack(msg slack.Message) BotMessage { tgt := msg.Channel if tgt == "" && msg.User != "" { tgt = msg.User } return BotMessage{ Source: MsgSrcSlack, Dest: MsgSrcApp, Type: msg.Type, Target: tgt, Text: msg.Text, } } func (b BotMessage) GetSource() string { return b.Source } func (b BotMessage) GetDestination() string { return b.Dest } func (b BotMessage) GetType() string { return b.Type } func (b BotMessage) GetTarget() string { return b.Target } func (b BotMessage) GetText() string { return b.Text } func (b BotMessage) IsTo(dest string) bool { return b.Dest == dest } func (b BotMessage) IsError() bool { return b.Type == MsgTpError } func (b BotMessage) Is(msg BotMessage) bool { return b.Matches(msg.Source, msg.Dest, msg.Type, msg.Target, msg.Text) } func (b BotMessage) Matches(src, dest, tp, tgt, msg string) bool { return b.Source == src && b.Dest == dest && b.Type == tp && b.Target == tgt && b.Text == msg } func (b BotMessage) String() string { return fmt.Sprintf("BotMessage: %s -> %s[%s]@%s :: %s", b.Source, b.Dest, b.Type, b.Target, b.Text) }