feat: implement substitution map

This commit is contained in:
2024-11-03 14:11:12 +01:00
parent 85f72290b8
commit cdb4a8aeb9
2 changed files with 18 additions and 2 deletions

2
.gitignore vendored
View File

@@ -5,3 +5,5 @@ instance/
__pycache__/ __pycache__/
signal_targets.txt signal_targets.txt
sqlite.db sqlite.db
substitutions.yaml
test.env

View File

@@ -27,6 +27,13 @@ db = SQLAlchemy(app)
BAD_DISPATCH_ACCESS_TOKEN = "Invalid or missing dispatch-access-token parameter in URL" BAD_DISPATCH_ACCESS_TOKEN = "Invalid or missing dispatch-access-token parameter in URL"
def _apply_substitution(string):
for replace, match in app.config["SUBSTITUTIONS"].items():
string = string.replace(match, replace)
return string
class WebHookPaths(db.Model): class WebHookPaths(db.Model):
__tablename__ = "webhook_paths" __tablename__ = "webhook_paths"
@@ -85,8 +92,8 @@ class DispatchObject(db.Model):
"timestamp" : self.timestamp, "timestamp" : self.timestamp,
"phone" : self.phone, "phone" : self.phone,
"email" : self.email, "email" : self.email,
"title" : self.title, "title" : _apply_substitution(self.title),
"message" : self.message, "message" : _apply_substitution(self.message),
"uuid" : self.dispatch_secret, "uuid" : self.dispatch_secret,
"method" : self.method, "method" : self.method,
"error" : self.dispatch_error, "error" : self.dispatch_error,
@@ -414,6 +421,13 @@ def create_app():
app.config["SETTINGS_ACCESS_TOKEN"] = os.environ["SETTINGS_ACCESS_TOKEN"] app.config["SETTINGS_ACCESS_TOKEN"] = os.environ["SETTINGS_ACCESS_TOKEN"]
app.config["DISPATCH_ACCESS_TOKEN"] = os.environ["DISPATCH_ACCESS_TOKEN"] app.config["DISPATCH_ACCESS_TOKEN"] = os.environ["DISPATCH_ACCESS_TOKEN"]
substitution_config_file = app.config.get("SUBSTITUTION_MAP") or "substitutions.yaml"
app.config["SUBSTITUTIONS"] = {}
if os.path.isfile(substitution_config_file):
with open(substitution_config_file) as f:
app.config["SUBSTITUTIONS"] = yaml.safe_load(f)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Simple Telegram Notification Interface', parser = argparse.ArgumentParser(description='Simple Telegram Notification Interface',