-
Notifications
You must be signed in to change notification settings - Fork 0
/
notificare.py
65 lines (54 loc) · 1.99 KB
/
notificare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
import requests
import weechat
SCRIPT_NAME = "notificare"
weechat.register(
SCRIPT_NAME,
"ClifHouck",
"0.1",
"MIT",
SCRIPT_NAME + ": Send notifications on mentions and private messages.",
"",
""
)
config = {
'user_token': '',
'api_token': '',
'api_url': 'https://api.pushover.net/1/messages.json',
'sound': 'magic'
}
for option, default_value in config.items():
weechat_config_value = weechat.config_get_plugin(option)
if weechat_config_value == '' and config[option] == '':
weechat.prnt("",
weechat.prefix("error") + SCRIPT_NAME +
": Option '%s' not set and no default is provided." %
option)
if weechat_config_value != '':
config[option] = weechat_config_value
weechat.hook_print("",
"irc_privmsg", "", 1,
"handle_mention_or_private_message", "")
def handle_mention_or_private_message(data, buffer, date, tags, displayed,
highlight, prefix, message):
buffer_type = weechat.buffer_get_string(buffer, "localvar_type")
if buffer_type == "private":
send_push_notification(prefix, message)
elif int(highlight):
send_push_notification(weechat.buffer_get_string(buffer, "name"),
prefix + " | " + message)
return weechat.WEECHAT_RC_OK
def send_push_notification(origin, message):
post_data = {
'token': config['api_token'],
'user': config['user_token'],
'message': origin + ": " + message,
'sound': config['sound']
}
response = requests.post(config['api_url'], data=post_data)
if response.status_code != 200:
weechat.prnt("",
weechat.prefix("error") + SCRIPT_NAME +
": Push notification request failed: %s, %s, %s" %
(response.text, post_data['message'], post_data))
else:
weechat.prnt("", SCRIPT_NAME + ": Push notification sent successfully.")