API
API references
actions
Module defining the Action class as well as it subclasses:
- ViewAction
- HttpAction
Action
Superclass for action buttons.
See: ntfy button action documentation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action |
str
|
name of the action (e.g. 'view', 'http') |
required |
label |
str
|
description of the action |
required |
url |
str
|
where the action redirects |
required |
clear |
bool
|
if true, the notification is deleted upon click |
False
|
Source code in ntfy_lite/actions.py
HttpAction
Bases: Action
Class encapsulating the information of a view action. See: ntfy http action
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label |
str
|
arbitrary string |
required |
url |
str
|
url to which the request should be sent |
required |
clear |
bool
|
if the ntfy notification should be cleared after the request succeeds |
False
|
method |
HttpMethod
|
GET, POST or PUT |
GET
|
headers |
Optional[Mapping[str, str]]
|
HTTP headers to be passed in the request |
None
|
body |
Optional[str]
|
HTTP body |
None
|
Source code in ntfy_lite/actions.py
HttpMethod
Bases: Enum
List of methods supported by instances of HttpAction.
Source code in ntfy_lite/actions.py
GET = auto()
class-attribute
instance-attribute
GET http method
POST = auto()
class-attribute
instance-attribute
POST http method
PUT = auto()
class-attribute
instance-attribute
PUT http method
ViewAction
Bases: Action
Class encapsulating the information of a view action. See: ntfy view action For arguments: see documentation of the ntfy_lite.actions.Action superclass
Source code in ntfy_lite/actions.py
defaults
Module defining level2tags, i.e. a mapping between logging level to emoticons. This mapping is used for the logging handler (ntfy_lite.handler.NtfyHandler)
level2tags: typing.Dict[LoggingLevel, typing.Tuple[str, ...]] = {logging.CRITICAL: ('fire'), logging.ERROR: ('broken_heart'), logging.WARNING: ('warning'), logging.INFO: ('artificial_satellite'), logging.DEBUG: ('speech_balloon'), logging.NOTSET: tuple()}
module-attribute
Default mapping from logging level to tags, i.e. tags that will be added to notifications corresponding to the key logging level.
demo_logging
ntfy lite: ntfy logging Handler example
demo_push
ntfy lite: notification push examples
error
NtfyError
Bases: Exception
Error thrown when the push of a notification fails.
Attributes status_code: error code returned by the request reason: reason of the failure
Source code in ntfy_lite/error.py
handler
Module defining the NtfyHandler class.
The NtfyHandler is a logging handler, i.e. an handler suitable for the python logging package
# Basic usage
import logging
import ntfy_lite as ntfy
ntfy_handler = ntfy.NtfyHandler("my_topic")
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] %(asctime)s | %(name)s | %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
handlers=(ntfy_handler,),
)
NtfyHandler
Bases: Handler
Subclass of logging.Handler that pushes ntfy notifications.
The notification title will be the record name, and the notification message will be either the record message or a file attachment (depending on the level2filepath argument).
Source code in ntfy_lite/handler.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
__init__(topic, url='https://ntfy.sh', twice_in_a_row=True, error_callback=None, level2tags=level2tags, level2priority=level2priority, level2filepath={}, level2email={}, dry_run=DryRun.off)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic |
str
|
Topic on which the notifications will be pushed. |
required |
url |
str
|
https://ntfy.sh by default. |
'https://ntfy.sh'
|
twice_in_a_row |
bool
|
If False, if several similar records (similar: same name and same message) are emitted, only the first one will result in notification being pushed (to avoid the channel to reach the accepted limits of notifications). |
True
|
error_callback |
Optional[Callable[[Exception], Any]]
|
It will be called if a NtfyError is raised when pushing a notification. |
None
|
level2tags |
Dict[LoggingLevel, Tuple[str, ...]]
|
mapping between logging level and tags to be associated with the notification |
level2tags
|
level2priority |
Dict[LoggingLevel, Priority]
|
mapping between the logging level and the notification priority. |
level2priority
|
level2filepath |
Dict[LoggingLevel, Path]
|
If for the logging level of the record a corresponding filepath is set, the notification will contain no message but a correspondinf file attachment (be aware of the size limits, see https://ntfy.sh/docs/publish/#attach-local-file). |
{}
|
level2email |
Dict[LoggingLevel, str]
|
If an email address is specified for the logging level of the record, the ntfy notification will also request a mail to be sent. |
{}
|
dry_run |
DryRun
|
For testing. If 'on', no notification will be sent. If 'error', no notification will be sent, instead a NtfyError are raised. |
off
|
Source code in ntfy_lite/handler.py
emit(record)
Push the record as an ntfy message.
Source code in ntfy_lite/handler.py
ntfy
Module defining the push method, which send a message or the content of a file to an NTFY channel.
DryRun
Bases: Enum
An optional value of DryRun may be passed as an argument to the ntfy_lite.ntfy.push function.
-
If 'off' is passed (default), then the ntfy_lite.ntfy.push function will publish to ntfy.
-
If 'on' is passed, then the ntfy_lite.ntfy.push function will not publish to ntfy.
-
If 'error' is passed, then the ntfy_lite.ntfy.push function will raise an ntfy_lite.error.NtfyError.
This is meant for testing.
Source code in ntfy_lite/ntfy.py
push(topic, title, message=None, priority=Priority.DEFAULT, tags=[], click=None, email=None, filepath=None, attach=None, icon=None, actions=[], at=None, url='https://ntfy.sh', dry_run=DryRun.off)
Pushes a notification.
# basic usage
import ntfy_lite as ntfy
ntfy.push(
"my topic", priority=ntfy.Priority.DEFAULT, message="my message"
)
For more documentation of all arguments, visit: https://ntfy.sh/docs/publish/
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic |
str
|
the ntfy topic on which to publish |
required |
title |
str
|
the title of the notification |
required |
message |
Optional[str]
|
the message. It is optional and if None, then a filepath argument must be provided instead. |
None
|
priority |
Priority
|
the priority of the notification |
DEFAULT
|
tags |
emojis
|
either a string (a single tag) or a list of string (several tags). see supported emojis |
[]
|
click |
Optional[str]
|
URL link to be included in the notification |
None
|
email |
Optional[str]
|
address to which the notification should also be sent |
None
|
filepath |
Optional[Path]
|
path to the file to be sent as attachement. It is optional and if None, then a message argument must be provided instead. |
None
|
icon |
Optional[str]
|
URL to an icon to include in the notification |
None
|
actions |
Union[Action, Sequence[Action]]
|
An action is either a ntfy_lite.actions.ViewAction (i.e. a link to a website) or a ntfy_lite.actions.HttpAction (i.e. sending of a HTTP GET, POST or PUT request to a website) |
[]
|
at |
Optional[str]
|
to be used for delayed notification, see scheduled delivery |
None
|
url |
Optional[str]
|
ntfy server |
'https://ntfy.sh'
|
dry_run |
DryRun
|
for testing purposes, see ntfy_lite.ntfy.DryRun |
off
|
Source code in ntfy_lite/ntfy.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
ntfy2logging
Module defining:
- LoggingLevel: typing union of all logging levels
- Priority: enumeration over ntfy priority levels
- level2priority: default mapping between logging levels and ntfy priority levels
LoggingLevel = typing.Literal[logging.DEBUG, logging.INFO, logging.NOTSET, logging.WARNING, logging.ERROR, logging.CRITICAL]
module-attribute
Union of all logging levels (DEBUG, INFO, NOTSET, WARNING, ERROR and CRITICAL)
level2priority: typing.Dict[LoggingLevel, Priority] = {logging.CRITICAL: Priority.MAX, logging.ERROR: Priority.HIGH, logging.WARNING: Priority.HIGH, logging.INFO: Priority.DEFAULT, logging.DEBUG: Priority.LOW, logging.NOTSET: Priority.MIN}
module-attribute
Default mapping from logging level to ntfy priority level (e.g. a record of level INFO maps to a notification of piority level 3)
Priority
Bases: Enum
Enumeration of supported ntfy priority levels
Source code in ntfy_lite/ntfy2logging.py
DEFAULT = '3'
class-attribute
instance-attribute
DEFAULT
HIGH = '4'
class-attribute
instance-attribute
HIGH
LOW = '2'
class-attribute
instance-attribute
LOW
MAX = '5'
class-attribute
instance-attribute
MAX
MIN = '1'
class-attribute
instance-attribute
MIN
utils
Module defining the function 'validate_url'.
validate_url(attribute, value)
Return None if value is a valid URL or is None, raises a ValueError otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attribute |
str
|
an arbitrary string, used in the message of the raised ValueError |
required |
value |
Optional[str]
|
the string to check |
required |