TelegramBots/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/SilentSender.java

73 lines
2.3 KiB
Java
Raw Normal View History

package org.telegram.abilitybots.api.sender;
2019-09-09 19:10:26 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ForceReplyKeyboard;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;
import java.io.Serializable;
import java.util.Optional;
/**
* A silent sender that returns {@link Optional} objects upon execution. Mainly used to decrease verboseness of exception handling.
*
* @author Abbas Abou Daya
*/
public class SilentSender {
2019-09-09 19:10:26 +02:00
private static final Logger log = LoggerFactory.getLogger(SilentSender.class);
private final MessageSender sender;
public SilentSender(MessageSender sender) {
this.sender = sender;
}
public Optional<Message> send(String message, long id) {
return doSendMessage(message, id, false);
}
public Optional<Message> sendMd(String message, long id) {
return doSendMessage(message, id, true);
}
public Optional<Message> forceReply(String message, long id) {
SendMessage msg = new SendMessage();
msg.setText(message);
2020-11-01 23:46:36 +01:00
msg.setChatId(Long.toString(id));
msg.setReplyMarkup(new ForceReplyKeyboard());
return execute(msg);
}
public <T extends Serializable, Method extends BotApiMethod<T>> Optional<T> execute(Method method) {
try {
return Optional.ofNullable(sender.execute(method));
} catch (TelegramApiException e) {
2019-07-27 14:19:54 +02:00
log.error("Could not execute bot API method", e);
return Optional.empty();
}
}
public <T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void
executeAsync(Method method, Callback callable) {
try {
sender.executeAsync(method, callable);
} catch (TelegramApiException e) {
2019-07-27 14:19:54 +02:00
log.error("Could not execute bot API method", e);
}
}
private Optional<Message> doSendMessage(String txt, long groupId, boolean format) {
SendMessage smsg = new SendMessage();
2020-11-01 23:46:36 +01:00
smsg.setChatId(Long.toString(groupId));
smsg.setText(txt);
smsg.enableMarkdown(format);
return execute(smsg);
}
}