This commit is contained in:
Philipp Heckel 2021-12-17 22:38:29 -05:00
parent a1f513f6a5
commit 5639cf7a0f
7 changed files with 105 additions and 17 deletions

View file

@ -9,7 +9,7 @@ import (
var cmdPublish = &cli.Command{ var cmdPublish = &cli.Command{
Name: "publish", Name: "publish",
Aliases: []string{"pub", "send", "push"}, Aliases: []string{"pub", "send", "push", "trigger"},
Usage: "Send message via a ntfy server", Usage: "Send message via a ntfy server",
UsageText: "ntfy send [OPTIONS..] TOPIC MESSAGE", UsageText: "ntfy send [OPTIONS..] TOPIC MESSAGE",
Action: execPublish, Action: execPublish,
@ -30,14 +30,15 @@ Examples:
ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
ntfy trigger mywebhook # Sending without message, useful for webhooks
Please also check out the docs on publishing messages. Especially for the --tags and --delay options, Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
it has incredibly useful information: https://ntfy.sh/docs/publish/.`, it has incredibly useful information: https://ntfy.sh/docs/publish/.`,
} }
func execPublish(c *cli.Context) error { func execPublish(c *cli.Context) error {
if c.NArg() < 2 { if c.NArg() < 1 {
return errors.New("topic/message missing") return errors.New("topic missing")
} }
title := c.String("title") title := c.String("title")
priority := c.String("priority") priority := c.String("priority")
@ -46,7 +47,10 @@ func execPublish(c *cli.Context) error {
noCache := c.Bool("no-cache") noCache := c.Bool("no-cache")
noFirebase := c.Bool("no-firebase") noFirebase := c.Bool("no-firebase")
topicURL := expandTopicURL(c.Args().Get(0)) topicURL := expandTopicURL(c.Args().Get(0))
message := strings.Join(c.Args().Slice()[1:], " ") message := ""
if c.NArg() > 1 {
message = strings.Join(c.Args().Slice()[1:], " ")
}
var options []client.PublishOption var options []client.PublishOption
if title != "" { if title != "" {
options = append(options, client.WithTitle(title)) options = append(options, client.WithTitle(title))

View file

@ -56,7 +56,7 @@ func execSubscribe(c *cli.Context) error {
if c.NArg() < 1 { if c.NArg() < 1 {
return errors.New("topic missing") return errors.New("topic missing")
} }
log.Printf("\x1b[1;33mThis command is incubating. The interface may change without notice.\x1b[0m") fmt.Fprintln(c.App.ErrWriter, "\x1b[1;33mThis command is incubating. The interface may change without notice.\x1b[0m")
cl := client.DefaultClient cl := client.DefaultClient
command := c.String("exec") command := c.String("exec")
since := c.String("since") since := c.String("since")

View file

@ -4,10 +4,10 @@ see [config.yml](https://github.com/binwiederhier/ntfy/blob/main/config/config.y
or using environment variables. or using environment variables.
## Quick start ## Quick start
By default, simply running `ntfy` will start the server at port 80. No configuration needed. Batteries included 😀. By default, simply running `ntfy serve` will start the server at port 80. No configuration needed. Batteries included 😀.
If everything works as it should, you'll see something like this: If everything works as it should, you'll see something like this:
``` ```
$ ntfy $ ntfy serve
2021/11/30 19:59:08 Listening on :80 2021/11/30 19:59:08 Listening on :80
``` ```

View file

@ -22,14 +22,20 @@ For this guide, we'll just use `mytopic` as our topic name:
That's it. After you tap "Subscribe", the app is listening for new messages on that topic. That's it. After you tap "Subscribe", the app is listening for new messages on that topic.
## Step 2: Send a message ## Step 2: Send a message
Now let's [send a message](publish.md) to our topic. It's easy in every language, since we're just using HTTP PUT or POST. The message Now let's [send a message](publish.md) to our topic. It's easy in every language, since we're just using HTTP PUT/POST,
is in the request body. Here's an example showing how to publish a simple message using a POST request: or with the [ntfy CLI](install.md). The message is in the request body. Here's an example showing how to publish a
simple message using a POST request:
=== "Command line (curl)" === "Command line (curl)"
``` ```
curl -d "Backup successful 😀" ntfy.sh/mytopic curl -d "Backup successful 😀" ntfy.sh/mytopic
``` ```
=== "ntfy CLI"
```
ntfy publish mytopic "Backup successful 😀"
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /mytopic HTTP/1.1 POST /mytopic HTTP/1.1

View file

@ -1,10 +1,12 @@
# Install your own ntfy server # Installing ntfy
**Self-hosting your own ntfy server** is pretty straight forward. Just install the binary, package or Docker image, then The `ntfy` CLI allows you to [publish messages](publish.md), [subscribe to topics](subscribe/cli.md) as well as to
**self-host your own ntfy server**. It's all pretty straight forward. Just install the binary, package or Docker image,
configure it and run it. Just like any other software. No fuzz. configure it and run it. Just like any other software. No fuzz.
!!! info !!! info
The following steps are only required if you want to **self-host your own ntfy server**. If you just want to The following steps are only required if you want to **self-host your own ntfy server** or you want to use the ntfy CLI.
[send messages using ntfy.sh](publish.md), you don't need to install anything. If you just want to [send messages using ntfy.sh](publish.md), you don't need to install anything. You can just use
`curl`.
## General steps ## General steps
The ntfy server comes as a statically linked binary and is shipped as tarball, deb/rpm packages and as a Docker image. The ntfy server comes as a statically linked binary and is shipped as tarball, deb/rpm packages and as a Docker image.
@ -12,7 +14,10 @@ We support amd64, armv7 and arm64.
1. Install ntfy using one of the methods described below 1. Install ntfy using one of the methods described below
2. Then (optionally) edit `/etc/ntfy/config.yml` (see [configuration](config.md)) 2. Then (optionally) edit `/etc/ntfy/config.yml` (see [configuration](config.md))
3. Then just run it with `ntfy serve` (or `systemctl start ntfy` when using the deb/rpm).
To run the ntfy server, then just run `ntfy serve` (or `systemctl start ntfy` when using the deb/rpm).
To send messages, use `ntfy publish`. To subscribe to topics, use `ntfy subscribe` (see [subscribing via CLI][subscribe/cli.md]
for details).
## Binaries and packages ## Binaries and packages
Please check out the [releases page](https://github.com/binwiederhier/ntfy/releases) for binaries and Please check out the [releases page](https://github.com/binwiederhier/ntfy/releases) for binaries and

View file

@ -1,6 +1,7 @@
# Publishing # Publishing
Publishing messages can be done via HTTP PUT or POST. Topics are created on the fly by subscribing or publishing to them. Publishing messages can be done via HTTP PUT/POST or via the [ntfy CLI](install.md). Topics are created on the fly by
Because there is no sign-up, **the topic is essentially a password**, so pick something that's not easily guessable. subscribing or publishing to them. Because there is no sign-up, **the topic is essentially a password**, so pick
something that's not easily guessable.
Here's an example showing how to publish a simple message using a POST request: Here's an example showing how to publish a simple message using a POST request:
@ -9,6 +10,11 @@ Here's an example showing how to publish a simple message using a POST request:
curl -d "Backup successful 😀" ntfy.sh/mytopic curl -d "Backup successful 😀" ntfy.sh/mytopic
``` ```
=== "ntfy CLI"
```
ntfy publish mytopic "Backup successful 😀"
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /mytopic HTTP/1.1 POST /mytopic HTTP/1.1
@ -67,6 +73,16 @@ a [title](#message-title), and [tag messages](#tags-emojis) 🥳 🎉. Here's an
ntfy.sh/phil_alerts ntfy.sh/phil_alerts
``` ```
=== "ntfy CLI"
```
ntfy publish \
--title "Unauthorized access detected" \
--tags warning,skull \
--priority urgent \
mytopic \
"Remote access to phils-laptop detected. Act right away."
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /phil_alerts HTTP/1.1 POST /phil_alerts HTTP/1.1
@ -143,6 +159,13 @@ you can set the `X-Title` header (or any of its aliases: `Title`, `ti`, or `t`).
curl -H "t: Dogs are better than cats" -d "Oh my ..." ntfy.sh/controversial curl -H "t: Dogs are better than cats" -d "Oh my ..." ntfy.sh/controversial
``` ```
=== "ntfy CLI"
```
ntfy publish \
-t "Dogs are better than cats" \
controversial "Oh my ..."
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /controversial HTTP/1.1 POST /controversial HTTP/1.1
@ -216,6 +239,13 @@ You can set the priority with the header `X-Priority` (or any of its aliases: `P
curl -H p:4 -d "A high priority message" ntfy.sh/phil_alerts curl -H p:4 -d "A high priority message" ntfy.sh/phil_alerts
``` ```
=== "ntfy CLI"
```
ntfy publish \
-p 5 \
phil_alerts An urgent message
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /phil_alerts HTTP/1.1 POST /phil_alerts HTTP/1.1
@ -320,6 +350,13 @@ them with a comma, e.g. `tag1,tag2,tag3`.
curl -H ta:dog -d "Dogs are awesome" ntfy.sh/backups curl -H ta:dog -d "Dogs are awesome" ntfy.sh/backups
``` ```
=== "ntfy CLI"
```
ntfy publish \
--tags=warning,mailsrv13,daily-backup \
backups "Backup of mailsrv13 failed"
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /backups HTTP/1.1 POST /backups HTTP/1.1
@ -395,6 +432,13 @@ to be delivered in 3 days, it'll remain in the cache for 3 days and 12 hours. Al
curl -H "Delay: 1639194738" -d "Unix timestamps are awesome" ntfy.sh/itsaunixsystem curl -H "Delay: 1639194738" -d "Unix timestamps are awesome" ntfy.sh/itsaunixsystem
``` ```
=== "ntfy CLI"
```
ntfy publish \
--at="tomorrow, 10am" \
hello "Good morning"
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /hello HTTP/1.1 POST /hello HTTP/1.1
@ -473,6 +517,11 @@ For instance, assuming your topic is `mywebhook`, you can simply call `/mywebhoo
curl ntfy.sh/mywebhook/trigger curl ntfy.sh/mywebhook/trigger
``` ```
=== "ntfy CLI"
```
ntfy trigger mywebhook
```
=== "HTTP" === "HTTP"
``` http ``` http
GET /mywebhook/trigger HTTP/1.1 GET /mywebhook/trigger HTTP/1.1
@ -510,6 +559,13 @@ Here's an example with a custom message, tags and a priority:
curl "ntfy.sh/mywebhook/publish?message=Webhook+triggered&priority=high&tags=warning,skull" curl "ntfy.sh/mywebhook/publish?message=Webhook+triggered&priority=high&tags=warning,skull"
``` ```
=== "ntfy CLI"
```
ntfy publish \
-p 5 --tags=warning,skull \
mywebhook "Webhook triggered"
```
=== "HTTP" === "HTTP"
``` http ``` http
GET /mywebhook/publish?message=Webhook+triggered&priority=high&tags=warning,skull HTTP/1.1 GET /mywebhook/publish?message=Webhook+triggered&priority=high&tags=warning,skull HTTP/1.1
@ -559,6 +615,13 @@ are still delivered to connected subscribers, but [`since=`](subscribe/api.md#fe
curl -H "Cache: no" -d "This message won't be stored server-side" ntfy.sh/mytopic curl -H "Cache: no" -d "This message won't be stored server-side" ntfy.sh/mytopic
``` ```
=== "ntfy CLI"
```
ntfy publish \
--no-cache \
mytopic "This message won't be stored server-side"
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /mytopic HTTP/1.1 POST /mytopic HTTP/1.1
@ -624,6 +687,13 @@ to `no`. This will instruct the server not to forward messages to Firebase.
curl -H "Firebase: no" -d "This message won't be forwarded to FCM" ntfy.sh/mytopic curl -H "Firebase: no" -d "This message won't be forwarded to FCM" ntfy.sh/mytopic
``` ```
=== "ntfy CLI"
```
ntfy publish \
--no-firebase \
mytopic "This message won't be forwarded to FCM"
```
=== "HTTP" === "HTTP"
``` http ``` http
POST /mytopic HTTP/1.1 POST /mytopic HTTP/1.1

View file

@ -1,3 +1,6 @@
# Subscribe via CLI # Subscribe via CLI
XXXXXXXXXxxx !!! info
The `ntfy subscribe` command is incubating. It's very much work in progress.
(This page is a stub. I'll write something once I'm happy with what the command looks like.)