publish.go 7.0 KB

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