publish.go 7.3 KB

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