publish.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package cmd
  2. import (
  3. "errors"
  4. "github.com/urfave/cli/v2"
  5. "heckel.io/ntfy/client"
  6. "strings"
  7. )
  8. var cmdPublish = &cli.Command{
  9. Name: "publish",
  10. Aliases: []string{"pub", "send", "push", "trigger"},
  11. Usage: "Send message via a ntfy server",
  12. UsageText: "ntfy send [OPTIONS..] TOPIC MESSAGE",
  13. Action: execPublish,
  14. Flags: []cli.Flag{
  15. &cli.StringFlag{Name: "title", Aliases: []string{"t"}, Usage: "message title"},
  16. &cli.StringFlag{Name: "priority", Aliases: []string{"p"}, Usage: "priority of the message (1=min, 2=low, 3=default, 4=high, 5=max)"},
  17. &cli.StringFlag{Name: "tags", Aliases: []string{"ta"}, Usage: "comma separated list of tags and emojis"},
  18. &cli.StringFlag{Name: "delay", Aliases: []string{"at", "in"}, Usage: "delay/schedule message"},
  19. &cli.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, Usage: "do not cache message server-side"},
  20. &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, Usage: "do not forward message to Firebase"},
  21. },
  22. Description: `Publish a message to a ntfy server.
  23. Examples:
  24. ntfy publish mytopic This is my message # Send simple message
  25. ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
  26. ntfy pub -p high backups "Backups failed" # Send high priority message
  27. ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
  28. ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
  29. ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
  30. ntfy trigger mywebhook # Sending without message, useful for webhooks
  31. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  32. it has incredibly useful information: https://ntfy.sh/docs/publish/.`,
  33. }
  34. func execPublish(c *cli.Context) error {
  35. if c.NArg() < 1 {
  36. return errors.New("topic missing")
  37. }
  38. title := c.String("title")
  39. priority := c.String("priority")
  40. tags := c.String("tags")
  41. delay := c.String("delay")
  42. noCache := c.Bool("no-cache")
  43. noFirebase := c.Bool("no-firebase")
  44. topic := c.Args().Get(0)
  45. message := ""
  46. if c.NArg() > 1 {
  47. message = strings.Join(c.Args().Slice()[1:], " ")
  48. }
  49. var options []client.PublishOption
  50. if title != "" {
  51. options = append(options, client.WithTitle(title))
  52. }
  53. if priority != "" {
  54. options = append(options, client.WithPriority(priority))
  55. }
  56. if tags != "" {
  57. options = append(options, client.WithTagsList(tags))
  58. }
  59. if delay != "" {
  60. options = append(options, client.WithDelay(delay))
  61. }
  62. if noCache {
  63. options = append(options, client.WithNoCache())
  64. }
  65. if noFirebase {
  66. options = append(options, client.WithNoFirebase())
  67. }
  68. conf, err := loadConfig(c)
  69. if err != nil {
  70. return err
  71. }
  72. cl := client.New(conf)
  73. return cl.Publish(topic, message, options...)
  74. }