publish.go 5.3 KB

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