publish.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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"},
  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. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  31. it has incredibly useful information: https://ntfy.sh/docs/publish/.`,
  32. }
  33. func execPublish(c *cli.Context) error {
  34. if c.NArg() < 2 {
  35. return errors.New("topic/message missing")
  36. }
  37. title := c.String("title")
  38. priority := c.String("priority")
  39. tags := c.String("tags")
  40. delay := c.String("delay")
  41. noCache := c.Bool("no-cache")
  42. noFirebase := c.Bool("no-firebase")
  43. topicURL := expandTopicURL(c.Args().Get(0))
  44. message := strings.Join(c.Args().Slice()[1:], " ")
  45. var options []client.PublishOption
  46. if title != "" {
  47. options = append(options, client.WithTitle(title))
  48. }
  49. if priority != "" {
  50. options = append(options, client.WithPriority(priority))
  51. }
  52. if tags != "" {
  53. options = append(options, client.WithTags(tags))
  54. }
  55. if delay != "" {
  56. options = append(options, client.WithDelay(delay))
  57. }
  58. if noCache {
  59. options = append(options, client.WithNoCache())
  60. }
  61. if noFirebase {
  62. options = append(options, client.WithNoFirebase())
  63. }
  64. return client.DefaultClient.Publish(topicURL, message, options...)
  65. }