publish.go 7.2 KB

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