use of org.telegram.telegrambots.exceptions.TelegramApiException in project webofneeds by researchstudio-sat.
the class Connect2TelegramAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventListenerContext ctx = getEventListenerContext();
if (event instanceof ConnectFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
Connection con = ((ConnectFromOtherNeedEvent) event).getCon();
URI yourNeedUri = con.getNeedURI();
URI remoteNeedUri = con.getRemoteNeedURI();
Long chatId = botContextWrapper.getChatIdForURI(yourNeedUri);
if (chatId == null) {
logger.error("No chatId found for the specified needUri");
return;
}
try {
Message message = wonTelegramBotHandler.sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getConnectMessage(chatId, remoteNeedUri, yourNeedUri));
botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
} catch (TelegramApiException te) {
logger.error(te.getMessage());
}
}
}
use of org.telegram.telegrambots.exceptions.TelegramApiException in project webofneeds by researchstudio-sat.
the class TelegramCreateAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventListenerContext ctx = getEventListenerContext();
if (event instanceof TelegramCreateNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
TelegramCreateNeedEvent telegramCreateNeedEvent = (TelegramCreateNeedEvent) event;
String[] parameters = telegramCreateNeedEvent.getStrings();
Long chatId = telegramCreateNeedEvent.getChat().getId();
if (chatId == null) {
logger.error("no chatid present");
return;
}
try {
NeedContentPropertyType type = telegramContentExtractor.getNeedContentType(parameters[0]);
if (type == null) {
throw new InvalidParameterException("no valid type was given");
}
String title = null;
if (parameters.length > 1) {
title = parameters[1];
}
if (title == null) {
throw new InvalidParameterException("no valid title was given");
}
// MAKE THOSE ATTRIBUTES DIFFERENT AND EDITABLE
boolean isUsedForTesting = true;
boolean isDoNotMatch = false;
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
DefaultNeedModelWrapper wrapper = new DefaultNeedModelWrapper(needURI.toString());
wrapper.setTitle(type, title);
for (URI facet : facets) {
wrapper.addFacetUri(facet.toString());
}
Dataset needDataset = wrapper.copyDataset();
logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(needDataset), 150));
WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, needDataset, isUsedForTesting, isDoNotMatch);
EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
botContextWrapper.addChatIdWonURIRelation(chatId, new WonURI(needURI, UriType.NEED));
botContextWrapper.addURIChatIdRelation(needURI, chatId);
EventListener successCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
logger.debug("need creation successful, new need URI is {}", needURI);
logger.debug("created need was from sender: " + botContextWrapper.getChatIdForURI(needURI));
try {
Message message = telegramCreateNeedEvent.getAbsSender().sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getCreatedNeedMessage(chatId, needURI));
botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(needURI, UriType.NEED));
} catch (TelegramApiException te) {
logger.error(te.getMessage());
}
}
};
EventListener failureCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
logger.error("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
EventBotActionUtils.removeFromList(getEventListenerContext(), needURI, uriListName);
}
};
EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, getEventListenerContext());
logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
getEventListenerContext().getWonMessageSender().sendWonMessage(createNeedMessage);
logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
} catch (Exception e) {
try {
logger.error(e.getMessage());
telegramCreateNeedEvent.getAbsSender().sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getErrorMessage(chatId));
} catch (TelegramApiException te) {
logger.error(te.getMessage());
}
}
}
}
use of org.telegram.telegrambots.exceptions.TelegramApiException in project telegram-notifications-plugin by jenkinsci.
the class TelegramBotThread method run.
@Override
public void run() {
try {
// Start bot session
BotSession session = TELEGRAM_BOTS_API.registerBot(bot);
LOGGER.finest("BotSession was started");
while (true) {
if (isInterrupted()) {
// If thread was interrupted bot session should be closed
session.stop();
LOGGER.finest("BotSession was closed");
break;
}
}
} catch (TelegramApiException e) {
LOGGER.log(Level.SEVERE, "Telegram API error", e);
interrupt();
}
}
use of org.telegram.telegrambots.exceptions.TelegramApiException in project telegram-notifications-plugin by jenkinsci.
the class StatusCommand method execute.
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
Subscribers subscribers = Subscribers.getInstance();
String toSend;
Long id = chat.getId();
boolean isSubscribed = subscribers.isSubscribed(id);
if (isSubscribed) {
boolean isApproved = subscribers.isApproved(id);
if (GlobalConfiguration.getInstance().getApprovalType() == UserApprover.ApprovalType.ALL) {
toSend = botStrings.get("message.status.approved");
} else {
toSend = isApproved ? botStrings.get("message.status.approved") : botStrings.get("message.status.unapproved");
}
} else {
toSend = botStrings.get("message.status.unsubscribed");
}
SendMessage answer = new SendMessage();
answer.setChatId(chat.getId().toString());
answer.setText(toSend);
try {
absSender.execute(answer);
} catch (TelegramApiException e) {
BotLogger.error(LOG_TAG, e);
}
}
use of org.telegram.telegrambots.exceptions.TelegramApiException in project telegram-notifications-plugin by jenkinsci.
the class SubCommand method execute.
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
Subscribers subscribers = Subscribers.getInstance();
String ans;
Long id = chat.getId();
String name = chat.isUserChat() ? user.getUserName() : chat.getTitle();
boolean isSubscribed = subscribers.isSubscribed(id);
if (!isSubscribed) {
subscribers.subscribe(name, id);
ans = botStrings.get("message.sub.success");
} else {
ans = botStrings.get("message.sub.alreadysub");
}
SendMessage answer = new SendMessage();
answer.setChatId(chat.getId().toString());
answer.setText(ans);
try {
absSender.execute(answer);
} catch (TelegramApiException e) {
BotLogger.error(LOG_TAG, e);
}
}
Aggregations