publish.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, Usage: "do not cache message server-side"},
  22. &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, Usage: "do not forward message to Firebase"},
  23. &cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}, Usage: "do print message"},
  24. },
  25. Description: `Publish a message to a ntfy server.
  26. Examples:
  27. ntfy publish mytopic This is my message # Send simple message
  28. ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
  29. ntfy pub -p high backups "Backups failed" # Send high priority message
  30. ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
  31. ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
  32. ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
  33. ntfy trigger mywebhook # Sending without message, useful for webhooks
  34. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  35. it has incredibly useful information: https://ntfy.sh/docs/publish/.
  36. The default config file for all client commands is /etc/ntfy/client.yml (if root user),
  37. or ~/.config/ntfy/client.yml for all other users.`,
  38. }
  39. func execPublish(c *cli.Context) error {
  40. if c.NArg() < 1 {
  41. return errors.New("must specify topic, type 'ntfy publish --help' for help")
  42. }
  43. conf, err := loadConfig(c)
  44. if err != nil {
  45. return err
  46. }
  47. title := c.String("title")
  48. priority := c.String("priority")
  49. tags := c.String("tags")
  50. delay := c.String("delay")
  51. noCache := c.Bool("no-cache")
  52. noFirebase := c.Bool("no-firebase")
  53. quiet := c.Bool("quiet")
  54. topic := c.Args().Get(0)
  55. message := ""
  56. if c.NArg() > 1 {
  57. message = strings.Join(c.Args().Slice()[1:], " ")
  58. }
  59. var options []client.PublishOption
  60. if title != "" {
  61. options = append(options, client.WithTitle(title))
  62. }
  63. if priority != "" {
  64. options = append(options, client.WithPriority(priority))
  65. }
  66. if tags != "" {
  67. options = append(options, client.WithTagsList(tags))
  68. }
  69. if delay != "" {
  70. options = append(options, client.WithDelay(delay))
  71. }
  72. if noCache {
  73. options = append(options, client.WithNoCache())
  74. }
  75. if noFirebase {
  76. options = append(options, client.WithNoFirebase())
  77. }
  78. cl := client.New(conf)
  79. m, err := cl.Publish(topic, message, options...)
  80. if err != nil {
  81. return err
  82. }
  83. if !quiet {
  84. fmt.Fprintln(c.App.Writer, strings.TrimSpace(m.Raw))
  85. }
  86. return nil
  87. }