99import static java .util .HashSet .newHashSet ;
1010import static java .util .concurrent .Executors .newFixedThreadPool ;
1111
12+ import com .github .stickerifier .stickerify .exception .BaseException ;
13+ import com .github .stickerifier .stickerify .exception .CorruptedVideoException ;
14+ import com .github .stickerifier .stickerify .exception .FileOperationException ;
15+ import com .github .stickerifier .stickerify .exception .MediaException ;
16+ import com .github .stickerifier .stickerify .exception .TelegramApiException ;
1217import com .github .stickerifier .stickerify .media .MediaHelper ;
1318import com .github .stickerifier .stickerify .telegram .Answer ;
14- import com .github .stickerifier .stickerify .telegram .exception .TelegramApiException ;
1519import com .github .stickerifier .stickerify .telegram .model .TelegramFile ;
1620import com .github .stickerifier .stickerify .telegram .model .TelegramRequest ;
1721import com .pengrad .telegrambot .ExceptionHandler ;
1822import com .pengrad .telegrambot .TelegramBot ;
1923import com .pengrad .telegrambot .UpdatesListener ;
24+ import com .pengrad .telegrambot .model .LinkPreviewOptions ;
2025import com .pengrad .telegrambot .model .Update ;
2126import com .pengrad .telegrambot .request .BaseRequest ;
2227import com .pengrad .telegrambot .request .GetFile ;
@@ -117,6 +122,7 @@ private void answerFile(TelegramRequest request, String fileId) {
117122
118123 LOGGER .atTrace ().log ("Converting file {}" , fileId );
119124 var outputFile = MediaHelper .convert (originalFile );
125+ LOGGER .atTrace ().log ("File converted successfully" );
120126
121127 if (outputFile == null ) {
122128 answerText (FILE_ALREADY_VALID , request );
@@ -130,14 +136,14 @@ private void answerFile(TelegramRequest request, String fileId) {
130136
131137 execute (answerWithFile );
132138 }
133- } catch (TelegramApiException e ) {
139+ } catch (TelegramApiException | MediaException e ) {
134140 processFailure (request , e );
135141 } finally {
136142 deleteTempFiles (pathsToDelete );
137143 }
138144 }
139145
140- private File retrieveFile (String fileId ) throws TelegramApiException {
146+ private File retrieveFile (String fileId ) throws TelegramApiException , FileOperationException {
141147 var file = execute (new GetFile (fileId )).file ();
142148
143149 try {
@@ -147,14 +153,16 @@ private File retrieveFile(String fileId) throws TelegramApiException {
147153
148154 return downloadedFile ;
149155 } catch (IOException e ) {
150- throw new TelegramApiException (e );
156+ throw new FileOperationException (e );
151157 }
152158 }
153159
154- private void processFailure (TelegramRequest request , TelegramApiException e ) {
155- if (e .getMessage ().endsWith ("Bad Request: message to be replied not found" )) {
156- LOGGER .atInfo ().log ("Unable to reply to the {}: the message sent has been deleted" , request .getDescription ());
157- } else if ("The video could not be processed successfully" .equals (e .getMessage ())) {
160+ private void processFailure (TelegramRequest request , BaseException e ) {
161+ if (e instanceof TelegramApiException telegramException ) {
162+ processTelegramFailure (request .getDescription (), telegramException , false );
163+ }
164+
165+ if (e instanceof CorruptedVideoException ) {
158166 LOGGER .atInfo ().log ("Unable to reply to the {}: the file is corrupted" , request .getDescription ());
159167 answerText (CORRUPTED , request );
160168 } else {
@@ -163,6 +171,18 @@ private void processFailure(TelegramRequest request, TelegramApiException e) {
163171 }
164172 }
165173
174+ private void processTelegramFailure (String requestDescription , TelegramApiException e , boolean logUnmatchedFailure ) {
175+ var exceptionMessage = e .getMessage ();
176+
177+ if (exceptionMessage .endsWith ("Bad Request: message to be replied not found" )) {
178+ LOGGER .atInfo ().log ("Unable to reply to the {}: the message sent has been deleted" , requestDescription );
179+ } else if (exceptionMessage .endsWith ("Forbidden: bot was blocked by the user" )) {
180+ LOGGER .atInfo ().log ("Unable to reply to the {}: the user blocked the bot" , requestDescription );
181+ } else if (logUnmatchedFailure ) {
182+ LOGGER .atError ().setCause (e ).log ("Unable to reply to the {}" , requestDescription );
183+ }
184+ }
185+
166186 private void answerText (TelegramRequest request ) {
167187 var message = request .message ();
168188 if (message .text () == null ) {
@@ -173,19 +193,23 @@ private void answerText(TelegramRequest request) {
173193 }
174194
175195 private void answerText (Answer answer , TelegramRequest request ) {
196+ var previewOptions = new LinkPreviewOptions ().isDisabled (answer .isDisableLinkPreview ());
197+
176198 var answerWithText = new SendMessage (request .getChatId (), answer .getText ())
177199 .replyToMessageId (request .getMessageId ())
178200 .parseMode (MarkdownV2 )
179- .disableWebPagePreview ( answer . isDisableWebPreview () );
201+ .linkPreviewOptions ( previewOptions );
180202
181203 try {
182204 execute (answerWithText );
183205 } catch (TelegramApiException e ) {
184- LOGGER . atError (). setCause ( e ). log ( "Unable to reply to the {}" , request );
206+ processTelegramFailure ( request . getDescription (), e , true );
185207 }
186208 }
187209
188210 private <T extends BaseRequest <T , R >, R extends BaseResponse > R execute (BaseRequest <T , R > request ) throws TelegramApiException {
211+ LOGGER .atTrace ().log ("Sending {} request" , request .getMethod ());
212+
189213 var response = bot .execute (request );
190214
191215 if (response .isOk ()) {
0 commit comments