Skip to content

Commit

Permalink
Add possibility to set custom FreeMarker templates for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Kliuchnikau committed Aug 5, 2024
1 parent 008cb94 commit a12f265
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 134 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,26 @@ Languages: 🇬🇧 🇫🇷 🇷🇺 🇺🇦 🇧🇾 🇨🇳
}
}
```
Блок `proxy` используется если нужно указать дополнительную конфигурацию proxy.
Блок `proxy` используется если нужно указать дополнительную конфигурацию proxy.\
Также в любой из конфигураций мессенджера можно дополнительно указать параметр `templatePath` чтобы установить путь к собственному `Freemarker` шаблону для сообщения.
Пример:
```
{
"base": {
...
},
"mail": {
"host": "smtp.gmail.com",
"port": "465",
"username": "username",
"password": "password",
"securityProtocol": "SSL",
"from": "test@gmail.com",
"recipient": "test1@gmail.com",
"templatePath": "templates/html_custom.ftl"
}
}
```

<a name="Base"></a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import guru.qa.allure.notifications.clients.Notifier;
import guru.qa.allure.notifications.config.discord.Discord;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.MarkdownTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import guru.qa.allure.notifications.template.data.MessageData;
import kong.unirest.ContentType;
import kong.unirest.Unirest;
Expand All @@ -23,7 +23,7 @@ public void sendText(MessageData messageData) throws MessagingException {
.routeParam("channelId", discord.getChannelId())
.header("Authorization", "Bot " + discord.getBotToken())
.header("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType())
.field("content", new MarkdownTemplate(messageData).create())
.field("content", new MessageTemplate(messageData).createMessageFromTemplate(discord.getTemplatePath()))
.asString()
.getBody();
}
Expand All @@ -34,7 +34,7 @@ public void sendPhoto(MessageData messageData, byte[] chartImage) throws Messagi
.routeParam("channelId", discord.getChannelId())
.header("Authorization", "Bot " + discord.getBotToken())
.field("file", new ByteArrayInputStream(chartImage), ContentType.IMAGE_PNG, "chart.png")
.field("content", new MarkdownTemplate(messageData).create())
.field("content", new MessageTemplate(messageData).createMessageFromTemplate(discord.getTemplatePath()))
.asString()
.getBody();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import guru.qa.allure.notifications.clients.Notifier;
import guru.qa.allure.notifications.config.loop.Loop;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.MarkdownTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import guru.qa.allure.notifications.template.data.MessageData;
import kong.unirest.ContentType;
import kong.unirest.Unirest;
Expand All @@ -21,7 +21,7 @@ public LoopClient(Loop loop) {
@Override
public void sendText(MessageData messageData) throws MessagingException {
Map<String, Object> body = new HashMap<>();
body.put("text", new MarkdownTemplate(messageData).create());
body.put("text", new MessageTemplate(messageData).createMessageFromTemplate(loop.getTemplatePath()));

Unirest.post(loop.getWebhookUrl())
.header("Content-Type", ContentType.APPLICATION_JSON.getMimeType())
Expand All @@ -35,7 +35,7 @@ public void sendPhoto(MessageData messageData, byte[] chartImage) throws Messagi
String encodedChartImage = Base64.getEncoder().encodeToString(chartImage);

Map<String, Object> body = new HashMap<>();
body.put("text", new MarkdownTemplate(messageData).create());
body.put("text", new MessageTemplate(messageData).createMessageFromTemplate(loop.getTemplatePath()));

Map<String, String> attachment = new HashMap<>();
attachment.put("image_url", "data:image/png;base64," + encodedChartImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import guru.qa.allure.notifications.clients.Notifier;
import guru.qa.allure.notifications.config.mail.Mail;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.HTMLTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import guru.qa.allure.notifications.template.data.MessageData;

public class Email implements Notifier {
Expand All @@ -20,13 +20,14 @@ public void sendText(MessageData messageData) throws MessagingException {
letter.from(mail.getFrom())
.to(mail.getRecipient())
.subject(messageData.getProject())
.text(new HTMLTemplate(messageData).create())
.text(new MessageTemplate(messageData).createMessageFromTemplate(mail.getTemplatePath()))
.send();
}

@Override
public void sendPhoto(MessageData messageData, byte[] chartImage) throws MessagingException {
String message = "<img src='cid:image'/><br/>" + new HTMLTemplate(messageData).create();
String message = "<img src='cid:image'/><br/>" + new MessageTemplate(messageData).createMessageFromTemplate(
mail.getTemplatePath());
letter.from(mail.getFrom())
.to(mail.getRecipient())
.subject(messageData.getProject())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import guru.qa.allure.notifications.config.mattermost.Mattermost;
import guru.qa.allure.notifications.exceptions.MessageBuildException;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.MarkdownTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import kong.unirest.ContentType;
import guru.qa.allure.notifications.template.data.MessageData;
import kong.unirest.Unirest;
Expand Down Expand Up @@ -48,7 +48,7 @@ public void sendPhoto(MessageData messageData, byte[] chartImage) throws Messagi

private void send(MessageData messageData, Map<String, Object> body) throws MessageBuildException {
body.put("channel_id", mattermost.getChat());
body.put("message", new MarkdownTemplate(messageData).create());
body.put("message", new MessageTemplate(messageData).createMessageFromTemplate(mattermost.getTemplatePath()));

Unirest.post("https://{uri}/api/v4/posts")
.routeParam("uri", mattermost.getUrl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import guru.qa.allure.notifications.clients.Notifier;
import guru.qa.allure.notifications.config.rocket.RocketChat;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.RocketTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import guru.qa.allure.notifications.template.data.MessageData;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
Expand All @@ -22,7 +22,7 @@ public RocketChatClient(RocketChat rocket) {
public void sendText(MessageData messageData) throws MessagingException {
Map<String, Object> body = new HashMap<>();
body.put("channel", rocketChat.getChannel());
body.put("text", new RocketTemplate(messageData).create());
body.put("text", new MessageTemplate(messageData).createMessageFromTemplate(rocketChat.getTemplatePath()));
Unirest.post(rocketChat.getUrl() + "/api/v1/chat.postMessage")
.header("X-Auth-Token", rocketChat.getToken())
.header("X-User-Id", rocketChat.getUserId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import guru.qa.allure.notifications.config.skype.Skype;
import guru.qa.allure.notifications.exceptions.MessageBuildException;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.MarkdownTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import kong.unirest.ContentType;
import guru.qa.allure.notifications.template.data.MessageData;
import kong.unirest.Unirest;
Expand Down Expand Up @@ -68,7 +68,7 @@ private SkypeMessage createSimpleMessage(MessageData messageData) throws Message
return SkypeMessage.builder()
.type("message")
.from(from)
.text(new MarkdownTemplate(messageData).create())
.text(new MessageTemplate(messageData).createMessageFromTemplate(skype.getTemplatePath()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import guru.qa.allure.notifications.clients.Notifier;
import guru.qa.allure.notifications.config.slack.Slack;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.MarkdownTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import guru.qa.allure.notifications.template.data.MessageData;
import kong.unirest.ContentType;
import kong.unirest.Unirest;
Expand All @@ -20,7 +20,7 @@ public SlackClient(Slack slack) {
@Override
public void sendText(MessageData messageData) throws MessagingException {
String body = String.format("channel=%s&text=%s",
slack.getChat(), new MarkdownTemplate(messageData).create());
slack.getChat(), new MessageTemplate(messageData).createMessageFromTemplate(slack.getTemplatePath()));

Unirest.post("https://slack.com/api/chat.postMessage")
.header("Authorization", "Bearer " + slack.getToken())
Expand All @@ -37,7 +37,8 @@ public void sendPhoto(MessageData messageData, byte[] chartImage) throws Messagi
.field("file", new ByteArrayInputStream(chartImage), ContentType.IMAGE_PNG, "chart.png")
.field("channels", slack.getChat())
.field("filename", " ")
.field("initial_comment", new MarkdownTemplate(messageData).create())
.field("initial_comment", new MessageTemplate(messageData).createMessageFromTemplate(
slack.getTemplatePath()))
.asString()
.getBody();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import guru.qa.allure.notifications.clients.Notifier;
import guru.qa.allure.notifications.config.telegram.Telegram;
import guru.qa.allure.notifications.exceptions.MessagingException;
import guru.qa.allure.notifications.template.TelegramTemplate;
import guru.qa.allure.notifications.template.MessageTemplate;
import guru.qa.allure.notifications.template.data.MessageData;
import kong.unirest.ContentType;
import kong.unirest.Unirest;
Expand All @@ -20,11 +20,11 @@ public TelegramClient(Telegram telegram) {
@Override
public void sendText(MessageData messageData) throws MessagingException {
Unirest.post("https://api.telegram.org/bot{token}/sendMessage")
.routeParam("token", telegram.token())
.routeParam("token", telegram.getToken())
.header("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType())
.field("chat_id", telegram.chat())
.field("reply_to_message_id", telegram.replyTo() + "")
.field("text", new TelegramTemplate(messageData).create())
.field("chat_id", telegram.getChat())
.field("reply_to_message_id", telegram.getReplyTo() + "")
.field("text", new MessageTemplate(messageData).createMessageFromTemplate(telegram.getTemplatePath()))
.field("parse_mode", "HTML")
.asString()
.getBody();
Expand All @@ -33,11 +33,11 @@ public void sendText(MessageData messageData) throws MessagingException {
@Override
public void sendPhoto(MessageData messageData, byte[] chartImage) throws MessagingException {
Unirest.post("https://api.telegram.org/bot{token}/sendPhoto")
.routeParam("token", telegram.token())
.routeParam("token", telegram.getToken())
.field("photo", new ByteArrayInputStream(chartImage), ContentType.IMAGE_PNG, "chart.png")
.field("chat_id", telegram.chat())
.field("reply_to_message_id", telegram.replyTo())
.field("caption", new TelegramTemplate(messageData).create())
.field("chat_id", telegram.getChat())
.field("reply_to_message_id", telegram.getReplyTo())
.field("caption", new MessageTemplate(messageData).createMessageFromTemplate(telegram.getTemplatePath()))
.field("parse_mode", "HTML")
.asString()
.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class Discord {
private String botToken;
@SerializedName("channelId")
private String channelId;
@SerializedName("templatePath")
private String templatePath = "markdown.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public class Loop {
@SerializedName("webhookUrl")
private String webhookUrl;
@SerializedName("templatePath")
private String templatePath = "markdown.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public class Mail {
private String from;
@SerializedName("recipient")
private String recipient;
@SerializedName("templatePath")
private String templatePath = "html.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class Mattermost {
private String token;
@SerializedName("chat")
private String chat;
@SerializedName("templatePath")
private String templatePath = "markdown.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class RocketChat {
private String userId;
@SerializedName("channel")
private String channel;
@SerializedName("templatePath")
private String templatePath = "rocket.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public class Skype {
private String botId;
@SerializedName("botName")
private String botName;
@SerializedName("templatePath")
private String templatePath = "markdown.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class Slack {
private String chat;
@SerializedName("replyTo")
private String replyTo;
@SerializedName("templatePath")
private String templatePath = "markdown.ftl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,21 @@

import com.google.gson.annotations.SerializedName;

import lombok.Getter;

/**
* @author kadehar
* @since 4.0
* Model class representing telegram settings.
*/
@Getter
public class Telegram {
@SerializedName("token")
private String token;
@SerializedName("chat")
private String chat;
@SerializedName("replyTo")
private String replyTo;

public String token() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String chat() {
return chat;
}

public void setChat(String chat) {
this.chat = chat;
}

public String replyTo() {
return replyTo;
}

public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}
@SerializedName("templatePath")
private String templatePath = "telegram.ftl";
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import guru.qa.allure.notifications.template.data.MessageData;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
Expand All @@ -26,12 +27,13 @@ public MessageTemplate(MessageData messageData) {
this.messageData = messageData;
}

public String of(String templateFile) throws MessageBuildException {
public String createMessageFromTemplate(String templateFile) throws MessageBuildException {
log.info("Processing template {}", templateFile);
Template template;
try {
log.info("Parsing template");
template = templateConfig.configure().getTemplate(templateFile);
File templateAsFile = new File(templateFile);
template = templateConfig.configure(templateAsFile).getTemplate(templateAsFile.getName());
} catch (IOException ex) {
throw new MessageBuildException(String.format("Unable to parse template %s!", templateFile), ex);
}
Expand Down
Loading

0 comments on commit a12f265

Please sign in to comment.