ntfy/cmd/publish.go

146 lines
5.2 KiB
Go
Raw Normal View History

2021-12-17 02:33:01 +01:00
package cmd
import (
"errors"
"fmt"
2021-12-17 02:33:01 +01:00
"github.com/urfave/cli/v2"
"heckel.io/ntfy/client"
2022-01-13 03:24:48 +01:00
"io"
"os"
"path/filepath"
2021-12-17 02:33:01 +01:00
"strings"
)
var cmdPublish = &cli.Command{
Name: "publish",
Aliases: []string{"pub", "send", "trigger"},
2021-12-17 02:33:01 +01:00
Usage: "Send message via a ntfy server",
UsageText: "ntfy send [OPTIONS..] TOPIC [MESSAGE]",
2021-12-17 02:33:01 +01:00
Action: execPublish,
Flags: []cli.Flag{
&cli.StringFlag{Name: "config", Aliases: []string{"c"}, Usage: "client config file"},
2021-12-17 02:33:01 +01:00
&cli.StringFlag{Name: "title", Aliases: []string{"t"}, Usage: "message title"},
&cli.StringFlag{Name: "priority", Aliases: []string{"p"}, Usage: "priority of the message (1=min, 2=low, 3=default, 4=high, 5=max)"},
&cli.StringFlag{Name: "tags", Aliases: []string{"tag", "T"}, Usage: "comma separated list of tags and emojis"},
&cli.StringFlag{Name: "delay", Aliases: []string{"at", "in", "D"}, Usage: "delay/schedule message"},
2022-01-05 00:11:36 +01:00
&cli.StringFlag{Name: "click", Aliases: []string{"U"}, Usage: "URL to open when notification is clicked"},
2022-01-13 03:24:48 +01:00
&cli.StringFlag{Name: "attach", Aliases: []string{"a"}, Usage: "URL to send as an external attachment"},
2022-01-13 21:17:30 +01:00
&cli.StringFlag{Name: "filename", Aliases: []string{"name", "n"}, Usage: "Filename for the attachment"},
2022-01-13 03:24:48 +01:00
&cli.StringFlag{Name: "file", Aliases: []string{"f"}, Usage: "File to upload as an attachment"},
2021-12-24 15:01:29 +01:00
&cli.StringFlag{Name: "email", Aliases: []string{"e-mail", "mail", "e"}, Usage: "also send to e-mail address"},
2021-12-17 02:33:01 +01:00
&cli.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, Usage: "do not cache message server-side"},
&cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, Usage: "do not forward message to Firebase"},
&cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}, Usage: "do print message"},
2021-12-17 02:33:01 +01:00
},
Description: `Publish a message to a ntfy server.
Examples:
ntfy publish mytopic This is my message # Send simple message
ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
ntfy pub -p high backups "Backups failed" # Send high priority message
ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
2021-12-24 15:01:29 +01:00
ntfy pub -e phil@example.com alerts 'App is down!' # Also send email to phil@example.com
2022-01-05 00:11:36 +01:00
ntfy pub --click="https://reddit.com" redd 'New msg' # Opens Reddit when notification is clicked
2022-01-13 03:24:48 +01:00
ntfy pub --attach="http://some.tld/file.zip" files # Send ZIP archive from URL as attachment
ntfy pub --file=flower.jpg flowers 'Nice!' # Send image.jpg as attachment
cat flower.jpg | ntfy pub --file=- flowers 'Nice!' # Same as above, send image.jpg as attachment
2021-12-18 04:38:29 +01:00
ntfy trigger mywebhook # Sending without message, useful for webhooks
2021-12-17 02:33:01 +01:00
Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
it has incredibly useful information: https://ntfy.sh/docs/publish/.
The default config file for all client commands is /etc/ntfy/client.yml (if root user),
or ~/.config/ntfy/client.yml for all other users.`,
2021-12-17 02:33:01 +01:00
}
func execPublish(c *cli.Context) error {
2021-12-18 04:38:29 +01:00
if c.NArg() < 1 {
return errors.New("must specify topic, type 'ntfy publish --help' for help")
}
conf, err := loadConfig(c)
if err != nil {
return err
2021-12-17 02:33:01 +01:00
}
title := c.String("title")
priority := c.String("priority")
tags := c.String("tags")
delay := c.String("delay")
2022-01-05 00:11:36 +01:00
click := c.String("click")
2022-01-13 03:24:48 +01:00
attach := c.String("attach")
filename := c.String("filename")
file := c.String("file")
2021-12-24 15:01:29 +01:00
email := c.String("email")
2021-12-17 02:33:01 +01:00
noCache := c.Bool("no-cache")
noFirebase := c.Bool("no-firebase")
quiet := c.Bool("quiet")
2021-12-18 20:43:27 +01:00
topic := c.Args().Get(0)
2021-12-18 04:38:29 +01:00
message := ""
if c.NArg() > 1 {
message = strings.Join(c.Args().Slice()[1:], " ")
}
2021-12-17 02:33:01 +01:00
var options []client.PublishOption
if title != "" {
options = append(options, client.WithTitle(title))
}
if priority != "" {
options = append(options, client.WithPriority(priority))
}
if tags != "" {
options = append(options, client.WithTagsList(tags))
2021-12-17 02:33:01 +01:00
}
if delay != "" {
options = append(options, client.WithDelay(delay))
}
2022-01-05 00:11:36 +01:00
if click != "" {
2022-01-13 03:24:48 +01:00
options = append(options, client.WithClick(click))
}
if attach != "" {
options = append(options, client.WithAttach(attach))
}
if filename != "" {
options = append(options, client.WithFilename(filename))
2022-01-05 00:11:36 +01:00
}
2021-12-24 15:01:29 +01:00
if email != "" {
options = append(options, client.WithEmail(email))
}
2021-12-17 02:33:01 +01:00
if noCache {
options = append(options, client.WithNoCache())
}
if noFirebase {
options = append(options, client.WithNoFirebase())
}
2022-01-13 03:24:48 +01:00
var body io.Reader
if file == "" {
body = strings.NewReader(message)
} else {
if message != "" {
options = append(options, client.WithMessage(message))
}
if file == "-" {
if filename == "" {
options = append(options, client.WithFilename("stdin"))
}
body = c.App.Reader
} else {
if filename == "" {
options = append(options, client.WithFilename(filepath.Base(file)))
}
body, err = os.Open(file)
if err != nil {
return err
}
}
}
cl := client.New(conf)
2022-01-13 03:24:48 +01:00
m, err := cl.PublishReader(topic, body, options...)
2021-12-18 20:43:27 +01:00
if err != nil {
return err
}
if !quiet {
fmt.Fprintln(c.App.Writer, strings.TrimSpace(m.Raw))
}
return nil
2021-12-17 02:33:01 +01:00
}