package workflow import ( "context" "errors" "fmt" "strings" "gopkg.in/yaml.v3" ) // MessageSendStepName is the step type that sends text to a chat on a // messaging channel. const MessageSendStepName = "message.send" // Messenger delivers text to one chat on a named messaging channel. type Messenger interface { Deliver(ctx context.Context, channel, chatID, text string) error } type messageSendSettings struct { Channel string `yaml:"channel" doc:"The messaging channel to send through."` ChatID string `yaml:"chat_id" title:"Chat ID" doc:"The channel's identifier for the chat."` Text string `yaml:"text" doc:"The message. References to earlier steps and the event are filled in."` } // MessageSendStepType contributes the message.send step type. func MessageSendStepType() StepType { return StepType{Name: MessageSendStepName, Factory: newMessageSendStage, Settings: messageSendSettings{}} } func newMessageSendStage(settings yaml.Node) (Stage, error) { s, err := decodeSettings[messageSendSettings](MessageSendStepName, settings) if err != nil { return Stage{}, err } if strings.TrimSpace(s.Channel) == "" || strings.TrimSpace(s.ChatID) == "" || strings.TrimSpace(s.Text) == "" { return Stage{}, fmt.Errorf("%s: settings.channel, settings.chat_id and settings.text are required", MessageSendStepName) } return Stage{Name: MessageSendStepName, Run: func(ctx context.Context, tc *TaskContext) error { if tc.Deliveries != nil { tc.Deliveries.Record(ctx, fmt.Sprintf("send to %s chat %s: %s", s.Channel, s.ChatID, s.Text)) return nil } if tc.Messenger == nil { return errors.New("message.send: this runner cannot reach the messaging service") } if err := tc.Messenger.Deliver(ctx, s.Channel, s.ChatID, s.Text); err != nil { return fmt.Errorf("message.send to %s chat %s: %w", s.Channel, s.ChatID, err) } return nil }}, nil }