Skip to content

Commit

Permalink
Add: Show HTML content button
Browse files Browse the repository at this point in the history
Now with HTML incoming mails, HTML
version button will be sent, to view content
in broser with html data.

Fix #28
  • Loading branch information
rosehgal committed Jun 5, 2020
1 parent 0de04c5 commit ce39eff
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ src/main/resources/application.yml
*.db
lib/
bin/
*.html

### STS ###
.apt_generated
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<artifactId>spring-integration-mail</artifactId>
<version>5.3.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
Expand All @@ -88,7 +89,7 @@
<version>1.18.10</version>
</dependency>
</dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
@ConfigurationProperties(prefix = "trashemail")
public class TrashemailConfig {
private Integer maxEmailsPerUser;
private String hostURI;
private String downloadPath;

@Override
public String toString() {
return Integer.toString(maxEmailsPerUser);
@Override public String toString() {
return "TrashemailConfig{" +
"maxEmailsPerUser=" + maxEmailsPerUser +
", hostURI='" + hostURI + '\'' +
", downloadPath='" + downloadPath + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
@Setter
@NoArgsConstructor
public class InlineKeyboardButton {
/*
According the docs, either of callback_data or uri, only one should be set.
*/
private String text;
private String callback_data;
private String url;

@Override
public String toString() {
@Override public String toString() {
return "InlineKeyboardButton{" +
"text='" + text + '\'' +
", callback_data='" + callback_data + '\'' +
", url='" + url + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.trashemail.Respositories.UserRepository;
import io.github.trashemail.models.User;
import io.github.trashemail.utils.MailParser;
import io.github.trashemail.utils.SaveMailToHTMLFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,6 +17,8 @@ public class ForwardMailsToTelegram {
private UserRepository userRepository;
@Autowired
private SendTelegramMessage sendTelegramMessage;
@Autowired
private SaveMailToHTMLFile saveMailToHTMLFile;

private static final Logger log = LoggerFactory.getLogger(
ForwardMailsToTelegram.class);
Expand All @@ -25,7 +28,31 @@ public void sendToTelegram(Message message) throws Exception {
User user = userRepository.findByEmailId(emailFor);
MailParser parsedMail = new MailParser(message);

sendTelegramMessage.sendMessage(parsedMail.toString(),
Long.toString(user.getChatId()));
/*
If html content is set, offer to save in file and show html link.
*/
if(parsedMail.getHtmlContentSet()){
Object filename = saveMailToHTMLFile.saveToFile(
parsedMail.getHtmlContent()
);

if (filename != null)
sendTelegramMessage.sendMessage(
parsedMail.toString(),
Long.toString(user.getChatId()),
(String) filename
);
else {
sendTelegramMessage.sendMessage(parsedMail.toString(),
Long.toString(
user.getChatId()
)
);
}
}
else {
sendTelegramMessage.sendMessage(parsedMail.toString(),
Long.toString(user.getChatId()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import io.github.trashemail.Configurations.TelegramConfig;

import java.util.ArrayList;
import java.util.List;

import io.github.trashemail.Configurations.TrashemailConfig;
import io.github.trashemail.Telegram.DTO.TelegramResponse;
import io.github.trashemail.Telegram.DTO.messageEntities.InlineKeyboardButton;
import io.github.trashemail.Telegram.DTO.messageEntities.InlineKeyboardMarkup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,6 +25,8 @@
public class SendTelegramMessage {
@Autowired
private TelegramConfig telegramConfig;
@Autowired
private TrashemailConfig trashemailConfig;

@Autowired
RestTemplate restTemplate;
Expand Down Expand Up @@ -51,8 +58,7 @@ public void sendMessage(String message, String chatId){
data.add("text", split.get(i));

HttpEntity<MultiValueMap<String, String>> request =
new HttpEntity<MultiValueMap<String, String>>(data,
headers);
new HttpEntity<MultiValueMap<String, String>>(data, headers);

ResponseEntity response = restTemplate.postForEntity(
telegramURI,
Expand All @@ -65,5 +71,80 @@ public void sendMessage(String message, String chatId){
else
log.error("Unable to send message to user: " + chatId);
}

}

@Async
public void sendMessage(String message, String chatId, String filename){
String telegramURI = telegramConfig.getUrl() +
telegramConfig.getBotToken() +
"/sendMessage";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// Checking the message size and do splitting into chunks if required
int maxMessageSize = telegramConfig.getSize();
ArrayList<String> split = new ArrayList<>();
for (int i = 0; i <= message.length() / maxMessageSize; i++) {
split.add(message.substring(i * maxMessageSize,
Math.min((i + 1) * maxMessageSize,
message.length())));
}

for (int i = 0; i < split.size(); i++) {
MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
data.add("chat_id", chatId);
data.add("text", split.get(i));

HttpEntity<MultiValueMap<String, String>> request =
new HttpEntity<MultiValueMap<String, String>>(data, headers);

ResponseEntity response = restTemplate.postForEntity(
telegramURI,
request,
String.class);

if(response.getStatusCode() == HttpStatus.OK){
log.debug("Message sent to user: " + chatId);
}
else
log.error("Unable to send message to user: " + chatId);
}

// send the response with html button
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();

InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText("HTML version");
inlineKeyboardButton.setUrl(
trashemailConfig.getHostPath() + filename
);

List<List<InlineKeyboardButton>> inlineKeyboardButtonList =
new ArrayList<>();

inlineKeyboardButtonList.add(new ArrayList<>());
inlineKeyboardButtonList.get(0).add(inlineKeyboardButton);

inlineKeyboardMarkup.setInlineKeyboardButtonList(
inlineKeyboardButtonList);

TelegramResponse telegramResponse = new TelegramResponse(
Long.parseLong(chatId),
"To view in HTML format click the link below.",
inlineKeyboardMarkup
);

ResponseEntity response = restTemplate.postForEntity(
telegramURI,
telegramResponse,
TelegramResponse.class
);
if(response.getStatusCode() == HttpStatus.OK){
log.debug("HTML link sent to user: " + chatId + filename);
}
else
log.error("Unable to HTML Link to user: " + chatId + filename);
}
}
1 change: 0 additions & 1 deletion src/main/java/io/github/trashemail/utils/MailParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public String toString() {
this.subject,
this.content
);

return mailData;
}
}
40 changes: 40 additions & 0 deletions src/main/java/io/github/trashemail/utils/SaveMailToHTMLFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.trashemail.utils;

import io.github.trashemail.Configurations.TrashemailConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import java.io.FileWriter;
import java.util.UUID;

@Component
public class SaveMailToHTMLFile {
@Autowired
TrashemailConfig trashemailConfig;

private static final Logger log = LoggerFactory.getLogger(
SaveMailToHTMLFile.class);

public Object saveToFile(String htmlContent){
try {
String filename = UUID.randomUUID().toString() + ".html";

FileWriter myWriter = new FileWriter(
trashemailConfig.getDownloadPath() + filename);

myWriter.write(htmlContent);
myWriter.close();

log.debug("File written to disk: "+ filename);
return filename;
}
catch (Exception e) {
log.error("Unable to save to HTML file. " + e.getMessage());
return null;
}
}
}

0 comments on commit ce39eff

Please sign in to comment.