publish.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/urfave/cli/v2"
  6. "heckel.io/ntfy/client"
  7. "strings"
  8. )
  9. var cmdPublish = &cli.Command{
  10. Name: "publish",
  11. Aliases: []string{"pub", "send", "trigger"},
  12. Usage: "Send message via a ntfy server",
  13. UsageText: "ntfy send [OPTIONS..] TOPIC [MESSAGE]",
  14. Action: execPublish,
  15. Flags: []cli.Flag{
  16. &cli.StringFlag{Name: "config", Aliases: []string{"c"}, Usage: "client config file"},
  17. &cli.StringFlag{Name: "title", Aliases: []string{"t"}, Usage: "message title"},
  18. &cli.StringFlag{Name: "priority", Aliases: []string{"p"}, Usage: "priority of the message (1=min, 2=low, 3=default, 4=high, 5=max)"},
  19. &cli.StringFlag{Name: "tags", Aliases: []string{"tag", "T"}, Usage: "comma separated list of tags and emojis"},
  20. &cli.StringFlag{Name: "delay", Aliases: []string{"at", "in", "D"}, Usage: "delay/schedule message"},
  21. &cli.StringFlag{Name: "click", Aliases: []string{"U"}, Usage: "URL to open when notification is clicked"},
  22. &cli.StringFlag{Name: "email", Aliases: []string{"e-mail", "mail", "e"}, Usage: "also send to e-mail address"},
  23. &cli.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, Usage: "do not cache message server-side"},
  24. &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, Usage: "do not forward message to Firebase"},
  25. &cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}, Usage: "do print message"},
  26. },
  27. Description: `Publish a message to a ntfy server.
  28. Examples:
  29. ntfy publish mytopic This is my message # Send simple message
  30. ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
  31. ntfy pub -p high backups "Backups failed" # Send high priority message
  32. ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
  33. ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
  34. ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
  35. ntfy pub -e phil@example.com alerts 'App is down!' # Also send email to phil@example.com
  36. ntfy pub --click="https://reddit.com" redd 'New msg' # Opens Reddit when notification is clicked
  37. ntfy trigger mywebhook # Sending without message, useful for webhooks
  38. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  39. it has incredibly useful information: https://ntfy.sh/docs/publish/.
  40. The default config file for all client commands is /etc/ntfy/client.yml (if root user),
  41. or ~/.config/ntfy/client.yml for all other users.`,
  42. }
  43. func execPublish(c *cli.Context) error {
  44. if c.NArg() < 1 {
  45. return errors.New("must specify topic, type 'ntfy publish --help' for help")
  46. }
  47. conf, err := loadConfig(c)
  48. if err != nil {
  49. return err
  50. }
  51. title := c.String("title")
  52. priority := c.String("priority")
  53. tags := c.String("tags")
  54. delay := c.String("delay")
  55. click := c.String("click")
  56. email := c.String("email")
  57. noCache := c.Bool("no-cache")
  58. noFirebase := c.Bool("no-firebase")
  59. quiet := c.Bool("quiet")
  60. topic := c.Args().Get(0)
  61. message := ""
  62. if c.NArg() > 1 {
  63. message = strings.Join(c.Args().Slice()[1:], " ")
  64. }
  65. var options []client.PublishOption
  66. if title != "" {
  67. options = append(options, client.WithTitle(title))
  68. }
  69. if priority != "" {
  70. options = append(options, client.WithPriority(priority))
  71. }
  72. if tags != "" {
  73. options = append(options, client.WithTagsList(tags))
  74. }
  75. if delay != "" {
  76. options = append(options, client.WithDelay(delay))
  77. }
  78. if click != "" {
  79. options = append(options, client.WithClick(email))
  80. }
  81. if email != "" {
  82. options = append(options, client.WithEmail(email))
  83. }
  84. if noCache {
  85. options = append(options, client.WithNoCache())
  86. }
  87. if noFirebase {
  88. options = append(options, client.WithNoFirebase())
  89. }
  90. cl := client.New(conf)
  91. m, err := cl.Publish(topic, message, options...)
  92. if err != nil {
  93. return err
  94. }
  95. if !quiet {
  96. fmt.Fprintln(c.App.Writer, strings.TrimSpace(m.Raw))
  97. }
  98. return nil
  99. }