publish.go 7.3 KB

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