Skip to content

Commit 21048f9

Browse files
committed
Support for sending image attachments with messages
1 parent 3915fd1 commit 21048f9

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

pushover.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ class Request:
9999
a :class:`RequestError` exception when the request is rejected.
100100
"""
101101

102-
def __init__(self, request_type, url, payload):
102+
def __init__(self, request_type, url, payload, files={}):
103103
if not TOKEN:
104104
raise InitError
105105

106106
payload["token"] = TOKEN
107-
request = getattr(requests, request_type)(url, params=payload)
107+
request = getattr(requests, request_type)(url, params=payload, files=files)
108108
self.answer = request.json()
109109
if 400 <= request.status_code < 500:
110110
raise RequestError(self.answer["errors"])
@@ -125,8 +125,8 @@ class MessageRequest(Request):
125125
:func:`poll` function.
126126
"""
127127

128-
def __init__(self, payload):
129-
Request.__init__(self, "post", MESSAGE_URL, payload)
128+
def __init__(self, payload, files):
129+
Request.__init__(self, "post", MESSAGE_URL, payload, files)
130130
self.receipt = None
131131
if payload.get("priority", 0) == 2:
132132
self.receipt = self.answer["receipt"]
@@ -234,21 +234,25 @@ def verify(self, device=None):
234234
self.devices = request.answer["devices"]
235235
return True
236236

237-
def send_message(self, message, **kwords):
237+
def send_message(self, message, attachment=None, **kwords):
238238
"""Send a message to the user. It is possible to specify additional
239239
properties of the message by passing keyword arguments. The list of
240240
valid keywords is ``title, priority, sound, callback, timestamp, url,
241241
url_title, device, retry, expire and html`` which are described in the
242242
Pushover API documentation. For convenience, you can simply set
243243
``timestamp=True`` to set the timestamp to the current timestamp.
244244
245+
An image can be attached to a message by passing a file-like object
246+
with the `attachment` keyword argument.
247+
245248
This method returns a :class:`MessageRequest` object.
246249
"""
247250
valid_keywords = ["title", "priority", "sound", "callback",
248251
"timestamp", "url", "url_title", "device",
249252
"retry", "expire", "html"]
250253

251254
payload = {"message": message, "user": self.user_key}
255+
files = {'attachment': attachment} if attachment else {}
252256
if self.device:
253257
payload["device"] = self.device
254258

@@ -268,7 +272,7 @@ def send_message(self, message, **kwords):
268272
elif value:
269273
payload[key] = value
270274

271-
return MessageRequest(payload)
275+
return MessageRequest(payload, files)
272276

273277
def send_glance(self, text=None, **kwords):
274278
"""Send a glance to the user. The default property is ``text``,

0 commit comments

Comments
 (0)