publish.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/urfave/cli/v2"
  6. "heckel.io/ntfy/client"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. var cmdPublish = &cli.Command{
  13. Name: "publish",
  14. Aliases: []string{"pub", "send", "trigger"},
  15. Usage: "Send message via a ntfy server",
  16. UsageText: "ntfy send [OPTIONS..] TOPIC [MESSAGE]",
  17. Action: execPublish,
  18. Flags: []cli.Flag{
  19. &cli.StringFlag{Name: "config", Aliases: []string{"c"}, Usage: "client config file"},
  20. &cli.StringFlag{Name: "title", Aliases: []string{"t"}, Usage: "message title"},
  21. &cli.StringFlag{Name: "priority", Aliases: []string{"p"}, Usage: "priority of the message (1=min, 2=low, 3=default, 4=high, 5=max)"},
  22. &cli.StringFlag{Name: "tags", Aliases: []string{"tag", "T"}, Usage: "comma separated list of tags and emojis"},
  23. &cli.StringFlag{Name: "delay", Aliases: []string{"at", "in", "D"}, Usage: "delay/schedule message"},
  24. &cli.StringFlag{Name: "click", Aliases: []string{"U"}, Usage: "URL to open when notification is clicked"},
  25. &cli.StringFlag{Name: "attach", Aliases: []string{"a"}, Usage: "URL to send as an external attachment"},
  26. &cli.StringFlag{Name: "filename", Aliases: []string{"name", "n"}, Usage: "Filename for the attachment"},
  27. &cli.StringFlag{Name: "file", Aliases: []string{"f"}, Usage: "File to upload as an attachment"},
  28. &cli.StringFlag{Name: "email", Aliases: []string{"e-mail", "mail", "e"}, Usage: "also send to e-mail address"},
  29. &cli.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, Usage: "do not cache message server-side"},
  30. &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, Usage: "do not forward message to Firebase"},
  31. &cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}, Usage: "do print message"},
  32. },
  33. Description: `Publish a message to a ntfy server.
  34. Examples:
  35. ntfy publish mytopic This is my message # Send simple message
  36. ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
  37. ntfy pub -p high backups "Backups failed" # Send high priority message
  38. ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
  39. ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
  40. ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
  41. ntfy pub -e phil@example.com alerts 'App is down!' # Also send email to phil@example.com
  42. ntfy pub --click="https://reddit.com" redd 'New msg' # Opens Reddit when notification is clicked
  43. ntfy pub --attach="http://some.tld/file.zip" files # Send ZIP archive from URL as attachment
  44. ntfy pub --file=flower.jpg flowers 'Nice!' # Send image.jpg as attachment
  45. cat flower.jpg | ntfy pub --file=- flowers 'Nice!' # Same as above, send image.jpg as attachment
  46. ntfy trigger mywebhook # Sending without message, useful for webhooks
  47. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  48. it has incredibly useful information: https://ntfy.sh/docs/publish/.
  49. The default config file for all client commands is /etc/ntfy/client.yml (if root user),
  50. or ~/.config/ntfy/client.yml for all other users.`,
  51. }
  52. func execPublish(c *cli.Context) error {
  53. if c.NArg() < 1 {
  54. return errors.New("must specify topic, type 'ntfy publish --help' for help")
  55. }
  56. conf, err := loadConfig(c)
  57. if err != nil {
  58. return err
  59. }
  60. title := c.String("title")
  61. priority := c.String("priority")
  62. tags := c.String("tags")
  63. delay := c.String("delay")
  64. click := c.String("click")
  65. attach := c.String("attach")
  66. filename := c.String("filename")
  67. file := c.String("file")
  68. email := c.String("email")
  69. noCache := c.Bool("no-cache")
  70. noFirebase := c.Bool("no-firebase")
  71. quiet := c.Bool("quiet")
  72. topic := c.Args().Get(0)
  73. message := ""
  74. if c.NArg() > 1 {
  75. message = strings.Join(c.Args().Slice()[1:], " ")
  76. }
  77. var options []client.PublishOption
  78. if title != "" {
  79. options = append(options, client.WithTitle(title))
  80. }
  81. if priority != "" {
  82. options = append(options, client.WithPriority(priority))
  83. }
  84. if tags != "" {
  85. options = append(options, client.WithTagsList(tags))
  86. }
  87. if delay != "" {
  88. options = append(options, client.WithDelay(delay))
  89. }
  90. if click != "" {
  91. options = append(options, client.WithClick(click))
  92. }
  93. if attach != "" {
  94. options = append(options, client.WithAttach(attach))
  95. }
  96. if filename != "" {
  97. options = append(options, client.WithFilename(filename))
  98. }
  99. if email != "" {
  100. options = append(options, client.WithEmail(email))
  101. }
  102. if noCache {
  103. options = append(options, client.WithNoCache())
  104. }
  105. if noFirebase {
  106. options = append(options, client.WithNoFirebase())
  107. }
  108. var body io.Reader
  109. if file == "" {
  110. body = strings.NewReader(message)
  111. } else {
  112. if message != "" {
  113. options = append(options, client.WithMessage(message))
  114. }
  115. if file == "-" {
  116. if filename == "" {
  117. options = append(options, client.WithFilename("stdin"))
  118. }
  119. body = c.App.Reader
  120. } else {
  121. if filename == "" {
  122. options = append(options, client.WithFilename(filepath.Base(file)))
  123. }
  124. body, err = os.Open(file)
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. }
  130. cl := client.New(conf)
  131. m, err := cl.PublishReader(topic, body, options...)
  132. if err != nil {
  133. return err
  134. }
  135. if !quiet {
  136. fmt.Fprintln(c.App.Writer, strings.TrimSpace(m.Raw))
  137. }
  138. return nil
  139. }