11package com .github .stickerifier .stickerify .bot ;
22
3+ import static com .github .stickerifier .stickerify .logger .StructuredLogger .REQUEST_DETAILS ;
34import static com .github .stickerifier .stickerify .telegram .Answer .CORRUPTED ;
45import static com .github .stickerifier .stickerify .telegram .Answer .ERROR ;
56import static com .github .stickerifier .stickerify .telegram .Answer .FILE_ALREADY_VALID ;
910import static java .util .HashSet .newHashSet ;
1011import static java .util .concurrent .Executors .newThreadPerTaskExecutor ;
1112
12- import com .github .stickerifier .stickerify .exception .BaseException ;
1313import com .github .stickerifier .stickerify .exception .CorruptedFileException ;
1414import com .github .stickerifier .stickerify .exception .FileOperationException ;
15- import com .github .stickerifier .stickerify .exception .MediaException ;
1615import com .github .stickerifier .stickerify .exception .TelegramApiException ;
16+ import com .github .stickerifier .stickerify .logger .StructuredLogger ;
1717import com .github .stickerifier .stickerify .media .MediaHelper ;
1818import com .github .stickerifier .stickerify .telegram .Answer ;
1919import com .github .stickerifier .stickerify .telegram .model .TelegramFile ;
3131import com .pengrad .telegrambot .request .SendDocument ;
3232import com .pengrad .telegrambot .request .SendMessage ;
3333import com .pengrad .telegrambot .response .BaseResponse ;
34- import org .slf4j .Logger ;
35- import org .slf4j .LoggerFactory ;
34+ import org .slf4j .event .Level ;
3635
3736import java .io .File ;
3837import java .io .IOException ;
5150 */
5251public record Stickerify (TelegramBot bot , Executor executor ) implements UpdatesListener , ExceptionHandler , AutoCloseable {
5352
54- private static final Logger LOGGER = LoggerFactory . getLogger (Stickerify .class );
53+ private static final StructuredLogger LOGGER = new StructuredLogger (Stickerify .class );
5554 private static final String BOT_TOKEN = System .getenv ("STICKERIFY_TOKEN" );
5655 private static final ThreadFactory VIRTUAL_THREAD_FACTORY = Thread .ofVirtual ().name ("Virtual-" , 0 ).factory ();
5756
@@ -78,9 +77,7 @@ public int process(List<Update> updates) {
7877 updates .forEach (update -> executor .execute (() -> {
7978 if (update .message () != null ) {
8079 var request = new TelegramRequest (update .message ());
81- LOGGER .atInfo ().log ("Received {}" , request .getDescription ());
82-
83- answer (request );
80+ ScopedValue .where (REQUEST_DETAILS , request .toRequestDetails ()).run (() -> answer (request ));
8481 }
8582 }));
8683
@@ -89,7 +86,7 @@ public int process(List<Update> updates) {
8986
9087 @ Override
9188 public void onException (TelegramException e ) {
92- LOGGER .atError (). log ( "There was an unexpected failure: {} " , e .getMessage ());
89+ LOGGER .at ( Level . ERROR ). setCause ( e ). addKeyValue ( "exception_message " , e .getMessage ()). log ( "An unexpected failure occurred" );
9390 }
9491
9592 @ Override
@@ -104,12 +101,14 @@ public void close() {
104101 }
105102
106103 private void answer (TelegramRequest request ) {
104+ LOGGER .at (Level .INFO ).log ("Received request" );
105+
107106 var file = request .getFile ();
108107
109- if (file != null ) {
110- answerFile (request , file );
111- } else {
108+ if (file == null ) {
112109 answerText (request );
110+ } else {
111+ answerFile (request , file );
113112 }
114113 }
115114
@@ -119,7 +118,7 @@ private void answerFile(TelegramRequest request, TelegramFile file) {
119118 } else if (file .canBeDownloaded ()) {
120119 answerFile (request , file .id ());
121120 } else {
122- LOGGER .atInfo ( ).log ("Passed-in file is too large" );
121+ LOGGER .at ( Level . INFO ).log ("Passed-in file is too large" );
123122
124123 answerText (FILE_TOO_LARGE , request );
125124 }
@@ -147,10 +146,10 @@ private void answerFile(TelegramRequest request, String fileId) {
147146
148147 execute (answerWithFile );
149148 }
150- } catch (TelegramApiException | MediaException e ) {
151- processFailure (request , e , fileId );
152149 } catch (InterruptedException e ) {
153150 Thread .currentThread ().interrupt ();
151+ } catch (Exception e ) {
152+ processFailure (request , e , fileId );
154153 } finally {
155154 deleteTempFiles (pathsToDelete );
156155 }
@@ -170,36 +169,44 @@ private File retrieveFile(String fileId) throws TelegramApiException, FileOperat
170169 }
171170 }
172171
173- private void processFailure (TelegramRequest request , BaseException e , String fileId ) {
172+ private void processFailure (TelegramRequest request , Exception e , String fileId ) {
174173 if (e instanceof TelegramApiException telegramException ) {
175- processTelegramFailure (request .getDescription (), telegramException , false );
174+ boolean replyToUser = processTelegramFailure (telegramException , false );
175+ if (!replyToUser ) {
176+ return ;
177+ }
176178 }
177179
178180 if (e instanceof CorruptedFileException ) {
179- LOGGER .atInfo ( ).log ("Unable to reply to the {} : the file is corrupted" , request . getDescription () );
181+ LOGGER .at ( Level . INFO ).log ("Unable to reply to the request : the file is corrupted" );
180182 answerText (CORRUPTED , request );
181183 } else {
182- LOGGER .atWarn ( ).setCause (e ).log ("Unable to process the file {}" , fileId );
184+ LOGGER .at ( Level . WARN ).setCause (e ).addKeyValue ( "file_id" , fileId ). log ("Unable to process file" );
183185 answerText (ERROR , request );
184186 }
185187 }
186188
187- private void processTelegramFailure (String requestDescription , TelegramApiException e , boolean logUnmatchedFailure ) {
189+ private boolean processTelegramFailure (TelegramApiException e , boolean logUnmatchedFailure ) {
190+ boolean replyToUser = false ;
191+
188192 switch (e .getDescription ()) {
189- case "Bad Request: message to be replied not found" -> LOGGER .atInfo ( ).log ("Unable to reply to the {} : the message sent has been deleted" , requestDescription );
190- case "Forbidden: bot was blocked by the user" -> LOGGER .atInfo ( ).log ("Unable to reply to the {} : the user blocked the bot" , requestDescription );
193+ case "Bad Request: message to be replied not found" -> LOGGER .at ( Level . INFO ).log ("Unable to reply to the request : the message sent has been deleted" );
194+ case "Forbidden: bot was blocked by the user" -> LOGGER .at ( Level . INFO ).log ("Unable to reply to the request : the user blocked the bot" );
191195 default -> {
192196 if (logUnmatchedFailure ) {
193- LOGGER .atError ( ).setCause (e ).log ("Unable to reply to the {}" , requestDescription );
197+ LOGGER .at ( Level . ERROR ).setCause (e ).log ("Unable to reply to the request" );
194198 }
199+ replyToUser = true ;
195200 }
196201 }
202+
203+ return replyToUser ;
197204 }
198205
199206 private void answerText (TelegramRequest request ) {
200207 var message = request .message ();
201208 if (message .text () == null ) {
202- LOGGER .atInfo ( ).log ("An unhandled message type has been received: {}" , message );
209+ LOGGER .at ( Level . INFO ).log ("An unhandled message type has been received" );
203210 }
204211
205212 answerText (request .getAnswerMessage (), request );
@@ -216,7 +223,7 @@ private void answerText(Answer answer, TelegramRequest request) {
216223 try {
217224 execute (answerWithText );
218225 } catch (TelegramApiException e ) {
219- processTelegramFailure (request . getDescription (), e , true );
226+ processTelegramFailure (e , true );
220227 }
221228 }
222229
@@ -234,10 +241,10 @@ private static void deleteTempFiles(Set<Path> pathsToDelete) {
234241 for (var path : pathsToDelete ) {
235242 try {
236243 if (!Files .deleteIfExists (path )) {
237- LOGGER .atInfo (). log ("Unable to delete temp file {}" , path );
244+ LOGGER .at ( Level . INFO ). addKeyValue ( "file_path" , path ). log ("Unable to delete temp file" );
238245 }
239246 } catch (IOException e ) {
240- LOGGER .atError ( ).setCause (e ).log ("An error occurred trying to delete temp file {}" , path );
247+ LOGGER .at ( Level . ERROR ).setCause (e ).addKeyValue ( "file_path" , path ). log ("An error occurred trying to delete temp file" );
241248 }
242249 }
243250 }
0 commit comments