后台小程序开发的全方位指南
2429
2022-10-23
Ntfy:一个实用程序来发送系统通知,在后台进程当命令完成时
About ntfy
ntfy brings notification to your shell. It can automatically provide desktop notifications when long running commands finish or it can send push notifications to your phone when a specific command finishes. Confused? This video demonstrates some of this functionality:
Quickstart
$ sudo pip install ntfy$ ntfy send test# send a notification when the command `sleep 10` finishes# this sends the message '"sleep 10" succeeded in 0:10 minutes'$ ntfy done sleep 10$ ntfy -b pushover -o user_key t0k3n send 'Pushover test!'$ ntfy -t 'ntfy' send "Here's a custom notification title!"$ echo -e 'backends: ["pushover"]\npushover: {"user_key": "t0k3n"}' > ~/.ntfy.yml$ ntfy send "Pushover via config file!"$ ntfy done --pid 6379 # pid extra$ ntfy send ":tada: ntfy supports emoji! :100:" # emoji extra# Enable shell integration$ echo 'eval "$(ntfy shell-integration)"' >> ~/.bashrc
Install
The install technique in the quickstart is the suggested method of installation. It can be installed in a virtualenv, but with some caveats: Linux notifications require --system-site-packages for the virtualenv and OS X notifications don't work at all.
Shell integration
ntfy has support for automatically sending notifications when long running commands finish in bash and zsh. In bash it emulates zsh's preexec and precmd functionality with rcaloras/bash-preexec. To enable it add the following to your .bashrc or .zshrc:
eval "$(ntfy shell-integration)"
By default it will only send notifications for commands lasting longer than 10 seconds and if the terminal is focused. Terminal focus works on X11(Linux) and with Terminal.app and iTerm2 on MacOS. Both options can be configured via the --longer-than and --foreground-too options.
To avoid unnecessary notifications when running interactive programs, programs listed in AUTO_NTFY_DONE_IGNORE don't generate notifications. For example:
export AUTO_NTFY_DONE_IGNORE="vim screen meld"
Extras
ntfy has a few features that require extra dependencies. ntfy done -p $PID requires installing as ntfy[pid]emjoi support requires installing as ntfy[emoji]XMPP support requires installing as ntfy[xmpp]Telegram support requires installing as ntfy[telegram]Instapush support requires installing as ntfy[instapush]Slack support requires installing as ntfy[slack]
To install multiple extras, separate with commas: e.g., ntfy[pid,emjoi].
Configuring ntfy
ntfy is configured with a YAML file stored at ~/.config/ntfy/ntfy.yml
Backends
The backends key specifies what backends to use by default. Each backend has its own configuration, stored in a key of its own name. For example:
---backends: - pushover - simplepush - slack - linux - xmpppushover: user_key: hunter2pushbullet: access_token: hunter2simplepush: key: hunter2slack: token: slacktoken recipient: "#slackchannel"xmpp: jid: "user@gmail.com" password: "xxxx" mtype: "chat" recipient: "me@jit.si"
If you want mulitple configs for the same backend type, you can specify any name and then specify the backend with a backend key. For example:
---pushover: user_key: hunter2cellphone: backend: pushover user_key: hunter2
See the backends below for available backends and options.
Pushover - pushover
Required parameters: user_key Optional parameters: soundpriorityexpireretrycallbackapi_token - use your own application tokendevice - target a device, if omitted, notification is sent to all devicesurlurl_titlehtml
Pushbullet - pushbullet
Required parameter: access_token - Your Pushbullet access token, created at https://pushbullet.com/#settings/account Optional parameters: device_iden - a device identifier, if omited, notification is sent to all devicesemail - send notification to pushbullet user with the specified email or send an email if they aren't a pushullet user
Simplepush - simplepush
Required parameter: key - Your Simplepush key, created by installing the Android App (no registration required) at https://simplepush.io
XMPP - xmpp
Requires parameters: jidpasswordrecipient Optional parameters hostname (if not from jid)portpath_to_certsmtype ('chat' required for Google Hangouts)
Requires extras, install like this: pip install ntfy[xmpp].
To verify the SSL certificates offered by a server: path_to_certs = "path/to/ca/cert"
Without dnspython library installed, you will need to specify the server hostname if it doesn't match the jid.
For example, to use Google Talk you would need to use: hostname = 'talk.google.com'
Specify port if other than 5222. NOTE: Ignored without specified hostname
Telegram - telegram
Requires extras, install like this: pip install ntfy[telegram].
Requires ntfy to be installed as ntfy[telegram]. This backend is configured the first time you will try to use it: ntfy -b telegram send "Telegram configured for ntfy".
Pushjet - pushjet
Required parameter: secret - The Pushjet service secret token, created with http://docs.pushjet.io/docs/creating-a-new-service Optional parameters: endpoint - custom Pushjet API endpoint (defaults to https://api.pushjet.io) level - The importance level from 1(low) to 5(high) link
Notifico - notifico
Required parameter: webhook - The webhook link, created at https://n.tkte.ch/(choose Plain Text service when creating the webhook)
Slack - Slack
Requires extras, install like this: pip install ntfy[slack].
Required parameter: token - The Slack service secret token, created in https://api.slack.com/web#authenticationrecipient - The Slack channel or user to send notifications to. If you use the # symbol the message is send to a Slack channel and if you use the @ symbol the message is send to a Slack user.
Instapush - insta
Requires extras, install like this pip install ntfy[instapush].
Instapush does not support notification title. It sends template-driven notifications, so you have to setup you events on the dashboard first. The backend is called insta due to homonymy with the instapush python wrapper
Required parameters: appid - The application idsecret - The application secretevent_name - The instapush event to be usedtrackers - The array of trakers to use
Note on trackers: Trackers are placeholders for events (a sort of notification template). If you defined more than one tracker in your event you'll have to provide more messages. At the moment, the only way to do so is to separate each message with a colon (:) character. You can also escape the separator character: Example:
ntfy -b insta send "message1:message2"ntfy -b insta send "message1:message2\:with\:colons"
Prowl - prowl
Optional parameters: api_keyprovider_keypriorityurl
Linux Desktop Notifications - linux
Works via dbus, works with most DEs like Gnome, KDE, XFCE and with libnotify.
The following dependecies should be installed.
$ sudo apt-get install libdbus-glib-1-dev libdbus-1-dev$ pip install --user dbus-python
You will need to install some font that supports emojis (in Debian fonts-symbola or Gentoo media-fonts/symbola).
Windows Desktop Notifications - win32
Uses pywin32.
Mac OS X Notification Center - darwin
Requires ntfy to be installed globally (not in a virtualenv).
System log - systemlog
Uses the syslog core Python module, which is not available on Windows platforms.
Optional parameters: prio - Syslog priority level. Default is ALERT. Possible values are:EMERGALERTCRITERRWARNINGNOTICEINFODEBUG facility - Syslog facility. Default is LOCAL5. Possible values are:KERNUSERMAILDAEMONAUTHLPRNEWSUUCPCRONSYSLOGLOCAL0LOCAL1LOCAL2LOCAL3LOCAL4LOCAL5LOCAL6LOCAL7 fmt - Format of the message to be sent to the system logger. The title and the message are specified using the following placeholders:{title}{message} Default is [{title}] {message}.
Pushalot - pushalot
Required parameter: auth_token - Your private Pushalot auth token, found here https://pushalot.com/manager/authorizations Optional parameters: source - source of the notificationttl - message expire time in minutes (time to live)url - URL to include in the notificationsurl_title - visible URL title (ignored if no url specified)image - URL of image included in the notificationsimportant - mark notifications as importantsilent - mark notifications as silent
Other options
Title is configurable with the title key in the config. Example:
---title: Customized Title
Backends ToDo
AirgramBoxcar
Testing
python setup.py test
Contributors
dschep - Maintainer & Lead Developerdanryder - XMPP Backend & emoji supportoz123 - Linux desktop notification improvementsschwert - PushJet supportrahiel - Telegram supporttymm - Simplepush supportjungle-boogie - Documentation updatestjbenator - Advanced Pushover optionsmobiusklein - Win32 Bugfixrcaloras - Creator of bash-prexec, without which there woudn't be bash shell integration for ntfyeightnoteight - Notifico supportjuanpabloaj - Slack supportgiuseongit - Instapush supportjlesage - Systemlog supportsambrightman - Prowl supportmlesniew - Pushalot support
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~