1
+ import json
1
2
import logging
2
3
from typing import Optional
3
4
11
12
Notification ,
12
13
NotificationServiceEnum ,
13
14
)
15
+ from app .util import json_type
14
16
from app .util .db import open_session
15
17
16
18
logger = logging .getLogger (__name__ )
17
19
18
20
19
21
def replace_variables (
20
- title_template : str ,
21
- body_template : str ,
22
+ template : str ,
22
23
username : Optional [str ] = None ,
23
24
book_title : Optional [str ] = None ,
24
25
book_authors : Optional [str ] = None ,
25
26
book_narrators : Optional [str ] = None ,
26
27
event_type : Optional [str ] = None ,
27
28
other_replacements : dict [str , str ] = {},
28
29
):
29
- title = title_template
30
- body = body_template
31
-
32
30
if username :
33
- title = title .replace ("{eventUser}" , username )
34
- body = body .replace ("{eventUser}" , username )
31
+ template = template .replace ("{eventUser}" , username )
35
32
if book_title :
36
- title = title .replace ("{bookTitle}" , book_title )
37
- body = body .replace ("{bookTitle}" , book_title )
33
+ template = template .replace ("{bookTitle}" , book_title )
38
34
if book_authors :
39
- title = title .replace ("{bookAuthors}" , book_authors )
40
- body = body .replace ("{bookAuthors}" , book_authors )
35
+ template = template .replace ("{bookAuthors}" , book_authors )
41
36
if book_narrators :
42
- title = title .replace ("{bookNarrators}" , book_narrators )
43
- body = body .replace ("{bookNarrators}" , book_narrators )
37
+ template = template .replace ("{bookNarrators}" , book_narrators )
44
38
if event_type :
45
- title = title .replace ("{eventType}" , event_type )
46
- body = body .replace ("{eventType}" , event_type )
39
+ template = template .replace ("{eventType}" , event_type )
47
40
48
41
for key , value in other_replacements .items ():
49
- title = title .replace (f"{{{ key } }}" , value )
50
- body = body .replace (f"{{{ key } }}" , value )
42
+ template = template .replace (f"{{{ key } }}" , value )
51
43
52
- return title , body
44
+ return template
53
45
54
46
55
47
async def _send (
56
48
title : str ,
57
49
body : str ,
50
+ additional_fields : dict [str , json_type .JSON ],
58
51
notification : Notification ,
59
52
client_session : ClientSession ,
60
53
):
@@ -63,13 +56,29 @@ async def _send(
63
56
body_key = "message"
64
57
case NotificationServiceEnum .apprise :
65
58
body_key = "body"
59
+ case NotificationServiceEnum .custom :
60
+ body_key = ""
66
61
67
- async with client_session .post (
68
- notification .url ,
69
- json = {
62
+ if notification .service == NotificationServiceEnum .custom :
63
+ json_body = {}
64
+ else :
65
+ json_body : dict [str , json_type .JSON ] = {
70
66
"title" : title ,
71
67
body_key : body ,
72
- },
68
+ }
69
+
70
+ for key , value in additional_fields .items ():
71
+ if key in json_body .keys ():
72
+ logger .warning (
73
+ f"Key '{ key } ' already exists in the JSON body but is passed as additional field. Overwriting with value: { value } "
74
+ )
75
+ json_body [key ] = value
76
+
77
+ print (json_body )
78
+
79
+ async with client_session .post (
80
+ notification .url ,
81
+ json = json_body ,
73
82
headers = notification .headers ,
74
83
) as response :
75
84
response .raise_for_status ()
@@ -95,8 +104,16 @@ async def send_notification(
95
104
book_authors = "," .join (book .authors )
96
105
book_narrators = "," .join (book .narrators )
97
106
98
- title , body = replace_variables (
107
+ title = replace_variables (
99
108
notification .title_template ,
109
+ requester_username ,
110
+ book_title ,
111
+ book_authors ,
112
+ book_narrators ,
113
+ notification .event .value ,
114
+ other_replacements ,
115
+ )
116
+ body = replace_variables (
100
117
notification .body_template ,
101
118
requester_username ,
102
119
book_title ,
@@ -105,13 +122,24 @@ async def send_notification(
105
122
notification .event .value ,
106
123
other_replacements ,
107
124
)
125
+ additional_fields : dict [str , json_type .JSON ] = json .loads (
126
+ replace_variables (
127
+ json .dumps (notification .additional_fields ),
128
+ requester_username ,
129
+ book_title ,
130
+ book_authors ,
131
+ book_narrators ,
132
+ notification .event .value ,
133
+ other_replacements ,
134
+ )
135
+ )
108
136
109
137
logger .info (
110
138
f"Sending notification to { notification .url } with title: '{ title } ', event type: { notification .event .value } "
111
139
)
112
140
113
141
async with ClientSession () as client_session :
114
- return await _send (title , body , notification , client_session )
142
+ return await _send (title , body , additional_fields , notification , client_session )
115
143
116
144
117
145
async def send_all_notifications (
@@ -144,23 +172,45 @@ async def send_manual_notification(
144
172
):
145
173
"""Send a notification for manual book requests"""
146
174
try :
147
- title , body = replace_variables (
175
+ book_authors = "," .join (book .authors )
176
+ book_narrators = "," .join (book .narrators )
177
+
178
+ title = replace_variables (
148
179
notification .title_template ,
180
+ requester_username ,
181
+ book .title ,
182
+ book_authors ,
183
+ book_narrators ,
184
+ notification .event .value ,
185
+ other_replacements ,
186
+ )
187
+ body = replace_variables (
149
188
notification .body_template ,
150
189
requester_username ,
151
190
book .title ,
152
- "," . join ( book . authors ) ,
153
- "," . join ( book . narrators ) ,
191
+ book_authors ,
192
+ book_narrators ,
154
193
notification .event .value ,
155
194
other_replacements ,
156
195
)
196
+ additional_fields : dict [str , json_type .JSON ] = json .loads (
197
+ replace_variables (
198
+ json .dumps (notification .additional_fields ),
199
+ requester_username ,
200
+ book .title ,
201
+ book_authors ,
202
+ book_narrators ,
203
+ notification .event .value ,
204
+ other_replacements ,
205
+ )
206
+ )
157
207
158
208
logger .info (
159
209
f"Sending manual notification to { notification .url } with title: '{ title } ', event type: { notification .event .value } "
160
210
)
161
211
162
212
async with ClientSession () as client_session :
163
- await _send (title , body , notification , client_session )
213
+ await _send (title , body , additional_fields , notification , client_session )
164
214
165
215
except Exception as e :
166
216
logger .error ("Failed to send notification" , e )
0 commit comments