How to Use the Command 'ntfy' (with examples)

How to Use the Command 'ntfy' (with examples)

The ntfy command-line tool is designed for sending and receiving HTTP POST notifications. It’s a versatile utility that lets users send notifications to various topics, manage notification priorities and tags, trigger webhooks, and even schedule notifications to be sent at a specific time. The tool offers a straightforward way to monitor events and send alerts, making it popular in home automation, system monitoring, and other applications requiring real-time communication.

Use Case 1: Send a Message to the security Topic

Code:

ntfy pub security "Front door has been opened."

Motivation:

Imagine having a smart security system at home that monitors entrances and sends alerts when something changes. In this scenario, you would want to be notified immediately if someone opens your front door unexpectedly. By using the ntfy command, you can send a quick alert to subscribers of the security topic, ensuring everyone is informed promptly. This enhances the security posture by creating a real-time communication channel for potential intrusions.

Explanation:

  • ntfy: This is the command-line tool being used.
  • pub: This command publishes a message to a specified topic.
  • security: This is the topic name. Subscribers listening to this topic will receive the message.
  • "Front door has been opened.": This is the actual message being sent as the alert.

Example Output:

Message "Front door has been opened." sent to topic "security".

Use Case 2: Send with a Title, Priority, and Tags

Code:

ntfy publish --title="Someone bought your item" --priority=high --tags=duck ebay "Someone just bought your item: Platypus Sculpture"

Motivation:

Online sellers often want immediate updates when there’s an activity in their store, such as a sale. This use case illustrates how to send a notification with additional context like a title, priority level, and tags. Including these extra elements helps the recipient quickly understand the importance and content of the message. In this case, the notification informs the seller of a sale, prompting them to act, such as preparing the item for shipping.

Explanation:

  • ntfy: The tool to send notifications.
  • publish: An alias for pub, used interchangeably to send a notification.
  • --title="Someone bought your item": Sets a human-readable title for the notification, giving the recipient a quick idea of the alert’s context.
  • --priority=high: Sets the notification’s priority to high, emphasizing the immediacy or importance of the message.
  • --tags=duck ebay: Tags are metadata added to the notification for filtering or categorization purposes. Here, ‘duck’ and ’ebay’ might relate to the item’s category.
  • "Someone just bought your item: Platypus Sculpture": The message content.

Example Output:

Notification "Someone bought your item" with priority high and tags duck, ebay sent.

Use Case 3: Send at 8:30 AM

Code:

ntfy pub --at=8:30am delayed_topic "Time for school, sleepyhead..."

Motivation:

Imagine a scenario where you want to remind your child every weekday morning to get ready for school at the same time. Using ntfy, you can schedule a notification to be sent at a specific time, ensuring they receive the alert just when they need it. This feature supports task scheduling, thereby helping maintain routines smoothly without manual intervention every day.

Explanation:

  • ntfy: The main command-line tool.
  • pub: Stands for publishing a message to a topic.
  • --at=8:30am: Schedules the message to be sent at 8:30 in the morning.
  • delayed_topic: The designated topic for recipients who are interested in these reminders.
  • "Time for school, sleepyhead...": The friendly reminder message for the recipient.

Example Output:

Scheduled message "Time for school, sleepyhead..." for topic "delayed_topic" at 8:30am.

Use Case 4: Trigger a Webhook

Code:

ntfy trigger my_webhook

Motivation:

In situations where you integrate different software services, a webhook can act like a bridge. Suppose your home automation system needs to trigger specific tasks in reaction to an event (e.g., turning on the porch lights when it’s dark). You can use ntfy to trigger a webhook that initiates this chain of actions. This approach supports seamless integration and automation across varied systems.

Explanation:

  • ntfy: The command-line tool for sending notifications or triggering actions.
  • trigger: Initiates the action of sending a notification through a webhook.
  • my_webhook: The name of the webhook endpoint, which will perform an action when triggered.

Example Output:

Webhook "my_webhook" triggered successfully.

Use Case 5: Subscribe to a Topic

Code:

ntfy sub home_automation

Motivation:

If you want to stay updated with notifications related to your home automation system, such as motion detection alerts or system status updates, subscribing to a specific topic ensures you receive all messages related to it. By subscribing, you can continuously monitor real-time updates, contributing to better decision-making and timely intervention when necessary.

Explanation:

  • ntfy: The command-line tool used for managing notifications.
  • sub: Stands for subscribe, allowing you to listen to notifications on a specific topic.
  • home_automation: The topic you are subscribing to, related to home automation notifications.

Example Output:

Subscribed to topic "home_automation". Press Ctrl-C to stop listening.

Use Case 6: Display Help

Code:

ntfy --help

Motivation:

For new users or those needing a quick refresher on available commands and options, the help feature is invaluable. It provides a comprehensive overview of how to use ntfy, including necessary command line options, their definitions, and application. This support aids users in effectively utilizing all functionalities the tool offers without needing separate documentation.

Explanation:

  • ntfy: The command-line tool.
  • --help: A flag that displays detailed help instructions and usage details for the ntfy command.

Example Output:

Usage: ntfy [OPTIONS] COMMAND [ARGS]...
Send and receive HTTP POST notifications.
...

Conclusion

The ntfy command-line tool offers a straightforward, efficient way to send and receive notifications through HTTP POST requests. With capabilities such as message scheduling, webhook triggering, and topic subscriptions, it is a versatile solution for various notification needs, from security alerts to e-commerce notifications. The examples provided illustrate its extensive functionalities and potential applications, allowing users to effectively enhance communication in their projects.

Related Posts

How to Use the Command 'dhcpcd' (with Examples)

How to Use the Command 'dhcpcd' (with Examples)

The dhcpcd command is a DHCP client used primarily for network configuration.

Read More
How to Use the Command 'chpass' (with Examples)

How to Use the Command 'chpass' (with Examples)

The chpass command is a powerful utility in Unix-like systems that allows users to modify their user database information, including their login shell and password.

Read More
How to Use the Command 'takeout' (with examples)

How to Use the Command 'takeout' (with examples)

Takeout is a Docker-based development-only dependency manager that streamlines the process of managing development dependencies by enabling or disabling services within isolated Docker containers.

Read More