1818#
1919# SPDX-License-Identifier: AGPL-3.0-or-later
2020
21- from typing import Dict , Optional , Type , Union
21+ from typing import Dict , List , Optional , Type , Union
2222
2323from maubot import Plugin , PluginWebApp
2424from aiohttp import hdrs , BasicAuth
@@ -71,12 +71,6 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
7171 if message_format not in valid_message_formats :
7272 raise ValueError (f"Invalid message_format '{ message_format } ' specified! "
7373 f"Must be one of: { ', ' .join (valid_message_formats )} " )
74- # validate message_type
75- valid_message_types = {"m.text" , "m.notice" }
76- message_type = helper .base ["message_type" ]
77- if message_type not in valid_message_types :
78- raise ValueError (f"Invalid message_type '{ message_type } ' specified! "
79- f"Must be one of: { ', ' .join (valid_message_types )} " )
8074
8175 # validate auth_type and auth_token
8276 valid_auth_types = {"Basic" , "Bearer" }
@@ -101,29 +95,24 @@ class WebhookPlugin(Plugin):
10195 config : BaseProxyConfig
10296 webapp : PluginWebApp
10397
98+ TEMPLATES : List [str ] = ["room" , "message" , "message_type" ]
99+
104100 @classmethod
105101 def get_config_class (cls ) -> Type [BaseProxyConfig ]:
106102 return Config
107103
108104 def on_external_config_update (self ) -> None :
109- old_path , old_method = self .config ["path" ], self .config ["method" ]
110- old_room , old_message = self .config ["room" ], self .config ["message" ]
105+ old_config = self .config .clone ()
111106 super ().on_external_config_update ()
112- new_path , new_method = self .config ["path" ], self .config ["method" ]
113- new_room , new_message = self .config ["room" ], self .config ["message" ]
114- if old_path != new_path or old_method != new_method :
107+ if old_config ["path" ] != self .config ["path" ] or old_config ["method" ] != self .config ["method" ]:
115108 self .log .debug ("Path or method changed, restarting webhook..." )
116109 self .webapp .clear ()
117110 self .register_webhook ()
118- if old_room != new_room :
119- self .reload_template ("room" )
120- if old_message != new_message :
121- self .reload_template ("message" )
122-
123- def reload_template (self , key : str ) -> None :
124- self .log .debug (f"{ key .capitalize ()} changed, reloading template..." )
125- self .load_template (key )
126- self .log .info (f"Successfully reloaded { key } template" )
111+ for key in self .TEMPLATES :
112+ if old_config [key ] != self .config [key ]:
113+ self .log .debug (f"{ key .capitalize ()} changed, reloading template..." )
114+ self .load_template (key )
115+ self .log .info (f"Successfully reloaded { key } template" )
127116
128117 def load_template (self , key : str ) -> None :
129118 try :
@@ -157,8 +146,8 @@ async def start(self) -> None:
157146 self .jinja_env .filters ["escape_md" ] = escape_md
158147
159148 self .config .load_and_update ()
160- self .load_template ( "room" )
161- self .load_template ("message" )
149+ for key in self .TEMPLATES :
150+ self .load_template (key )
162151 self .register_webhook ()
163152
164153 async def handle_request (self , req : Request ) -> Response :
@@ -225,7 +214,11 @@ def unauthorized(text: str) -> Response:
225214 "but the template generated an empty message." )
226215 return Response ()
227216
228- msgtype = self .config ["message_type" ]
217+ message_type : Union [str , Response ] = self .render_template ("message_type" , template_variables )
218+ if isinstance (message_type , Response ):
219+ return message_type
220+ msgtype = mautrix .types .MessageType (message_type )
221+
229222 self .log .info (f"Sending message ({ msgtype } ) to room { room } : { message } " )
230223 try :
231224 if self .config ["message_format" ] == 'markdown' :
0 commit comments