Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteeb37e3

Browse files
committed
[Pushbullet]: Modernize service plugin
- Use Python 3- Remove external dependencies- Use named-parameter configuration style
1 parentb00e2d5 commiteeb37e3

File tree

5 files changed

+417
-26
lines changed

5 files changed

+417
-26
lines changed

‎CHANGES.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ in progress
88

99
- Fix sending ntfy notifications with longer messages than 76 characters.
1010
Thanks, @codebude.
11+
- Modernize Pushbullet service plugin. Thanks, @DNicholai.
1112

1213

1314
2023-05-15 0.34.1

‎docs/notifier-catalog.md‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,13 +2287,15 @@ targets = {
22872287

22882288
###`pushbullet`
22892289

2290-
This service is for[PushBullet], an app for Android along with an extension for
2290+
This service is for[Pushbullet], an app for Android along with an extension for
22912291
Chrome and Firefox, which allows notes, links, pictures, addresses and files to be
2292-
sent between devices. It is based on the[PushbulletPythonLibrary] package.
2292+
sent between devices.
2293+
2294+
You can get your API key from the[Pushbullet account page] after signing up, see
2295+
also[Pushbullet » Getting an access token].
22932296

2294-
You can get your API key from the[PushBullet account page] after signing up.
22952297
You will also need the device ID to push the notifications to. To obtain this,
2296-
youwill need to follow the instructions at[pyPushBullet], by running
2298+
youcan, for example, follow the instructions at[pyPushBullet], by running
22972299
```shell
22982300
./pushbullet_cmd.py YOUR_API_KEY_HERE getdevices
22992301
```
@@ -2302,24 +2304,38 @@ The configuration layout looks like this.
23022304
```ini
23032305
[config:pushbullet]
23042306
targets = {
2305-
# API KEY device ID recipient_type
2306-
'warnme' : ['xxxxxxxxxxxxxxxxxxxxxxx','yyyyyy','tttt' ]
2307+
"warnme" : {
2308+
"access_token":"a6FJVAA0LVJKrT8k",
2309+
"recipient":"test@example.org",
2310+
"recipient_type":"email",
2311+
},
2312+
"alertme" : {
2313+
"access_token":"a6FJVAA0LVJKrT8k",
2314+
"recipient":"ujpah72o0sjAoRtnM0jc",
2315+
},
23072316
}
23082317
```
23092318

2310-
The optional_recipient_type_ could be one of`device_iden` (default),`email`,`channel` or`client`.
2319+
The optional_recipient_type_ could be one of`device` (default),`email`,`channel`
2320+
or`client`. See also[Pushbullet target parameters].
23112321

23122322
| Topic option| M/O| Description|
23132323
| -------------| :----:|-----------------------------------------|
23142324
|`title`| O| application title (default:`mqttwarn`)|
23152325

23162326
![Pushbullet](assets/pushbullet.jpg)
23172327

2318-
[PushBullet]:https://www.pushbullet.com
2319-
[PushBullet account page]:https://www.pushbullet.com/#settings/account
2320-
[PushbulletPythonLibrary]:https://pypi.org/project/PushbulletPythonLibrary/
2328+
[Pushbullet]:https://www.pushbullet.com
2329+
[Pushbullet account page]:https://www.pushbullet.com/#settings/account
2330+
[Pushbullet target parameters]:https://docs.pushbullet.com/#target-parameters
2331+
[Pushbullet » Getting an access token]:https://docs.pushbullet.com/#getting-an-access-token
23212332
[pyPushBullet]:https://github.com/Azelphur/pyPushBullet
23222333

2334+
:::{note}
2335+
The client currently only implements sending message with`type=note`. If you have a
2336+
need to submit files or links, please let us know on the[mqttwarn issue tracker].
2337+
:::
2338+
23232339

23242340
###`pushover`
23252341

@@ -3241,3 +3257,7 @@ For another scenario using the `zabbix` plugin, please refer to the [Zabbix IoT
32413257
[Zabbix low-level discovery]:https://www.zabbix.com/documentation/current/en/manual/discovery/low_level_discovery
32423258
[Zabbix meets MQTT]:http://jpmens.net/2014/05/27/zabbix-meets-mqtt/
32433259
[Zabbix trapper]:https://www.zabbix.com/documentation/current/en/manual/config/items/itemtypes/trapper
3260+
3261+
3262+
3263+
[mqttwarn issue tracker]:https://github.com/mqtt-tools/mqttwarn/issues

‎mqttwarn/services/pushbullet.py‎

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,74 @@
55
__copyright__='Copyright 2014 Jan-Piet Mens'
66
__license__='Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)'
77

8-
frompushbullet.pushbulletimportPushBullet
8+
importrequests
99

1010

1111
defplugin(srv,item):
12-
''' expects (apikey, device_id) in adddrs '''
12+
"""
13+
mqttwarn service for Pushbullet notifications.
14+
"""
1315

1416
srv.logging.debug("*** MODULE=%s: service=%s, target=%s",__file__,item.service,item.target)
1517

16-
recipient_type="device_iden"
17-
try:
18-
apikey,device_id=item.addrs
19-
except:
18+
# Decode target address descriptor.
19+
ifisinstance(item.addrs,list):
2020
try:
21-
apikey,device_id,recipient_type=item.addrs
21+
apikey,device_id=item.addrs
22+
options= {"access_token":apikey,"recipient":device_id}
2223
except:
23-
srv.logging.warn("pushbullet target is incorrectly configured")
24-
returnFalse
24+
try:
25+
apikey,device_id,recipient_type=item.addrs
26+
options= {"access_token":apikey,"recipient":device_id,"recipient_type":recipient_type}
27+
except:
28+
raiseValueError("Pushbullet target is incorrectly configured")
29+
elifisinstance(item.addrs,dict):
30+
options=item.addrs
31+
else:
32+
raiseValueError(f"Unknown target address descriptor type:{type(item.addrs).__name__}")
2533

34+
# Assemble keyword arguments for `send_note` function.
2635
text=item.message
2736
title=item.get('title',srv.SCRIPTNAME)
37+
options.update({"title":title,"body":text})
2838

2939
try:
30-
srv.logging.debug("Sending pushbullet notification to %s..."% (item.target))
31-
pb=PushBullet(apikey)
32-
pb.pushNote(device_id,title,text,recipient_type)
33-
srv.logging.debug("Successfully sent pushbullet notification")
34-
exceptExceptionase:
35-
srv.logging.warning("Cannot notify pushbullet: %s"%e)
40+
srv.logging.debug(f"Sending Pushbullet notification to{item.target}")
41+
send_note(**options)
42+
srv.logging.debug("Successfully sent Pushbullet notification")
43+
exceptException:
44+
srv.logging.exception("Sending Pushbullet notification failed")
3645
returnFalse
3746

3847
returnTrue
48+
49+
50+
defsend_note(access_token,title,body,recipient,recipient_type="device"):
51+
"""
52+
Send a Pushbullet message with type=note.
53+
54+
https://docs.pushbullet.com/#push
55+
"""
56+
headers= {
57+
"Access-Token":access_token,
58+
"Content-Type":"application/json",
59+
"User-Agent":"mqttwarn",
60+
}
61+
data= {"type":"note","title":title,"body":body}
62+
ifrecipient_typeisNone:
63+
pass
64+
elifrecipient_type=="device":
65+
data["device_iden"]=recipient
66+
elifrecipient_type=="email":
67+
data["email"]=recipient
68+
elifrecipient_type=="channel":
69+
data["channel_tag"]=recipient
70+
elifrecipient_type=="client":
71+
data["client_iden"]=recipient
72+
else:
73+
raiseValueError(f"Unknown recipient type:{recipient_type}")
74+
75+
response=requests.post("https://api.pushbullet.com/v2/pushes",headers=headers,json=data)
76+
response.raise_for_status()
77+
78+
returnresponse.status_code==requests.codes.ok

‎setup.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
"pyprowl>=3.0.1",
7575
],
7676
"pushbullet": [
77-
"PushbulletPythonLibrary>=2.3",
77+
# TODO: Upstream `send_note` utility function.
78+
# "pushbullet-python<2",
7879
],
7980
"redispub": [
8081
"redis>=2.10.6",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp