diff --git a/cmd/msteams.go b/cmd/msteams.go new file mode 100644 index 0000000..eaec84c --- /dev/null +++ b/cmd/msteams.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/nikoksr/notify" + msteams2 "github.com/nikoksr/notify/service/msteams" + "github.com/urfave/cli/v2" +) + +type msTeams struct { + Webhook string + Message string + Title string +} + +func SendToTeams() *cli.Command { + var msTeamOpt msTeams + return &cli.Command{ + Name: "teams", + Usage: "Send message to microsoft teams", + Description: `Teams uses webhooks to send messages, you can add multiple webhooks separated by comma ',' or +you can add permissions for multiple channels to single webhook.`, + UsageText: "pingme teams --webhook 'https://example.webhook.office.com/xx' --message 'some message'", + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &msTeamOpt.Webhook, + Name: "webhook", + Aliases: []string{"w"}, + Required: true, + Usage: "Webhook associated with teams channel, if multiple separate with ','.", + EnvVars: []string{"TEAMS_WEBHOOK"}, + }, + &cli.StringFlag{ + Destination: &msTeamOpt.Message, + Name: "msg", + Usage: "Message content.", + EnvVars: []string{"TEAMS_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &msTeamOpt.Title, + Name: "title", + Value: TimeValue, + Usage: "Title of the message.", + EnvVars: []string{"TEAMS_MSG_TITLE"}, + }, + }, + Action: func(ctx *cli.Context) error { + notifier := notify.New() + teamsSvc := msteams2.New() + + chn := strings.Split(msTeamOpt.Webhook, ",") + for _, v := range chn { + if len(v) <= 0 { + return fmt.Errorf(EmptyChannel) + } + teamsSvc.AddReceivers(v) + + } + + notifier.UseServices(teamsSvc) + + if err := notifier.Send( + context.Background(), + msTeamOpt.Title, + msTeamOpt.Message, + ); err != nil { + return err + } + log.Println("Successfully sent!") + return nil + }, + } +}