Files
archie/modules/runs/message.go
T

45 lines
1.5 KiB
Go

package runs
import (
"context"
"errors"
"fmt"
"strings"
)
// Messenger delivers text to one chat on a messaging channel.
type Messenger interface {
Send(ctx context.Context, channel, chatID, text, key string) error
}
// messageSettings send an authored message.
type messageSettings struct {
Channel string `yaml:"channel" json:"channel" jsonschema:"the messaging channel to send through"`
ChatID string `yaml:"chat_id" json:"chat_id" jsonschema:"the channel's identifier for the chat"`
Text string `yaml:"text" json:"text" jsonschema:"the message; references to earlier steps and inputs are filled in"`
}
func (s messageSettings) valid() error {
if strings.TrimSpace(s.Channel) == "" || strings.TrimSpace(s.ChatID) == "" || strings.TrimSpace(s.Text) == "" {
return errors.New("channel, chat_id and text are required")
}
return nil
}
func (s messageSettings) describe() string {
return fmt.Sprintf("Sends a message to a chat on %s", s.Channel)
}
// runMessage sends the message. A dry run records it without sending.
func runMessage(d Deps, rc *Context, _ *progress, _ string, s messageSettings) (StepResult, error) {
sent := fmt.Sprintf("send to %s chat %s: %s", s.Channel, s.ChatID, s.Text)
if rc.Dry() {
_, err := rc.Step("send-skipped", func(context.Context, string) (string, error) { return sent, nil })
return StepResult{Summary: sent}, err
}
_, err := rc.Step("send", func(ctx context.Context, key string) (bool, error) {
return true, d.Messenger.Send(ctx, s.Channel, s.ChatID, s.Text, key)
})
return StepResult{Summary: sent}, err
}