Search in sources :

Example 1 with WonURI

use of won.bot.framework.eventbot.action.impl.mail.model.WonURI 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());
        }
    }
}
Also used : TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) Message(org.telegram.telegrambots.api.objects.Message) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 2 with WonURI

use of won.bot.framework.eventbot.action.impl.mail.model.WonURI 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());
            }
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) Message(org.telegram.telegrambots.api.objects.Message) WonMessage(won.protocol.message.WonMessage) TelegramCreateNeedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramCreateNeedEvent) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedContentPropertyType(won.protocol.model.NeedContentPropertyType) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) InvalidParameterException(java.security.InvalidParameterException) TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) InvalidParameterException(java.security.InvalidParameterException) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) WonMessage(won.protocol.message.WonMessage) TelegramCreateNeedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramCreateNeedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) EventListener(won.bot.framework.eventbot.listener.EventListener)

Example 3 with WonURI

use of won.bot.framework.eventbot.action.impl.mail.model.WonURI in project webofneeds by researchstudio-sat.

the class MailCommandAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof MailCommandEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        MimeMessage message = ((MailCommandEvent) event).getMessage();
        String referenceId = MailContentExtractor.getMailReference(message);
        WonURI wonUri = botContextWrapper.getWonURIForMailId(referenceId);
        // determine if the mail is referring to some other mail/need/connection or not
        if (wonUri != null) {
            processReferenceMailCommands(message, wonUri);
        } else {
            processNonReferenceMailCommand(message);
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) MailCommandEvent(won.bot.framework.eventbot.event.impl.mail.MailCommandEvent)

Example 4 with WonURI

use of won.bot.framework.eventbot.action.impl.mail.model.WonURI in project webofneeds by researchstudio-sat.

the class Connect2MailParserAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof ConnectFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((ConnectFromOtherNeedEvent) event).getCon();
        URI responseTo = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Someone issued a connect for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createConnectMail(originalMail, remoteNeedUri);
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 5 with WonURI

use of won.bot.framework.eventbot.action.impl.mail.model.WonURI in project webofneeds by researchstudio-sat.

the class Hint2MailParserAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof HintFromMatcherEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Match match = ((HintFromMatcherEvent) event).getMatch();
        WonMessage message = ((HintFromMatcherEvent) event).getWonMessage();
        URI responseTo = match.getFromNeed();
        URI remoteNeedUri = match.getToNeed();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Found a hint for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createHintMail(originalMail, remoteNeedUri);
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(message.getReceiverURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) WonMessage(won.protocol.message.WonMessage) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) Match(won.protocol.model.Match)

Aggregations

WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)12 URI (java.net.URI)11 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)11 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)7 MimeMessage (javax.mail.internet.MimeMessage)6 Connection (won.protocol.model.Connection)6 Dataset (org.apache.jena.query.Dataset)5 Message (org.telegram.telegrambots.api.objects.Message)5 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)5 WonMessage (won.protocol.message.WonMessage)5 TelegramApiException (org.telegram.telegrambots.exceptions.TelegramApiException)4 Event (won.bot.framework.eventbot.event.Event)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)4 MessagingException (javax.mail.MessagingException)3 InternetAddress (javax.mail.internet.InternetAddress)2 Model (org.apache.jena.rdf.model.Model)2 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)2