init: Initial commit
This commit is contained in:
14
bot/msteams/adaptivecard/card.py
Normal file
14
bot/msteams/adaptivecard/card.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class AdaptiveCard:
|
||||
def __init__(self, body: list[dict] | None = None, actions: list[dict] | None = None) -> None:
|
||||
self.__body = body if body else []
|
||||
self.__actions = actions if actions else []
|
||||
|
||||
@property
|
||||
def data(self) -> dict:
|
||||
return {
|
||||
"type": "AdaptiveCard",
|
||||
"body": self.__body,
|
||||
"actions": self.__actions,
|
||||
"version": "1.6",
|
||||
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json"
|
||||
}
|
||||
25
bot/msteams/adaptivecard/template.py
Normal file
25
bot/msteams/adaptivecard/template.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os
|
||||
import json
|
||||
import re
|
||||
|
||||
class Template:
|
||||
def __init__(self, name: str) -> None:
|
||||
self.__name = name
|
||||
self.__path = "templates/" + name + ".json"
|
||||
self.__template = json.load(open(self.__path))
|
||||
|
||||
def __str__(self) -> str:
|
||||
return json.dumps(self.__template)
|
||||
|
||||
def replace_placeholders(self, data: dict) -> dict:
|
||||
"""Return the template as a dictionary with placeholders replaced."""
|
||||
template_str = json.dumps(self.__template)
|
||||
|
||||
for key, value in data.items():
|
||||
template_str = template_str.replace(f'{{{{{key}}}}}', str(value))
|
||||
|
||||
return json.loads(template_str)
|
||||
|
||||
def render(self, data: dict) -> str:
|
||||
"""Return the template as a JSON string with placeholders replaced."""
|
||||
return json.dumps(self.replace_placeholders(data))
|
||||
13
bot/msteams/adaptivecard/webhook_card.py
Normal file
13
bot/msteams/adaptivecard/webhook_card.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
from bot.msteams.adaptivecard.card import AdaptiveCard
|
||||
from bot.msteams.adaptivecard.template import Template
|
||||
|
||||
class WebhookCard(AdaptiveCard):
|
||||
def __init__(self, data: dict, template: Template) -> None:
|
||||
super().__init__(body=template.replace_placeholders(data)["body"], actions=template.replace_placeholders(data)["actions"])
|
||||
|
||||
@property
|
||||
def data(self) -> dict:
|
||||
return super().data
|
||||
Reference in New Issue
Block a user