Search in sources :

Example 31 with EventListenerContext

use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.

the class CreateDebugNeedWithFacetsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    String replyText = "";
    URI reactingToNeedUriTmp = null;
    Dataset needDataset = null;
    if (event instanceof NeedSpecificEvent) {
        reactingToNeedUriTmp = ((NeedSpecificEvent) event).getNeedURI();
    } else {
        logger.warn("could not process non-need specific event {}", event);
        return;
    }
    if (event instanceof NeedCreatedEventForMatcher) {
        needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
    } else if (event instanceof HintDebugCommandEvent) {
        reactingToNeedUriTmp = ((HintDebugCommandEvent) event).getRemoteNeedURI();
    } else if (event instanceof ConnectDebugCommandEvent) {
        reactingToNeedUriTmp = ((ConnectDebugCommandEvent) event).getRemoteNeedURI();
    } else {
        logger.error("CreateEchoNeedWithFacetsAction cannot handle " + event.getClass().getName());
        return;
    }
    final URI reactingToNeedUri = reactingToNeedUriTmp;
    String titleString = null;
    boolean createNeed = true;
    if (needDataset != null) {
        DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
        titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
        createNeed = needModelWrapper.hasFlag(WON.USED_FOR_TESTING) && !needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME);
    }
    // if create need is false do not continue the debug need creation
    if (!createNeed)
        return;
    if (titleString != null) {
        if (isInitialForConnect) {
            replyText = "Debugging with initial connect: " + titleString;
        } else if (isInitialForHint) {
            replyText = "Debugging with initial hint: " + titleString;
        } else {
            replyText = "Debugging: " + titleString;
        }
    } else {
        replyText = "Debug Need No. " + counter.increment();
    }
    EventListenerContext ctx = getEventListenerContext();
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    EventBus bus = ctx.getEventBus();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
    needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, replyText);
    needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the DebugBot.");
    for (URI facet : facets) {
        needModelWrapper.addFacetUri(facet.toString());
    }
    final Dataset debugNeedDataset = needModelWrapper.copyDataset();
    final Event origEvent = event;
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(debugNeedDataset), 150));
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, debugNeedDataset);
    // remember the need URI so we can react to success/failure responses
    EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
    EventListener successCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            logger.debug("need creation successful, new need URI is {}", needURI);
            // save the mapping between the original and the reaction in to the context.
            getEventListenerContext().getBotContextWrapper().addUriAssociation(reactingToNeedUri, needURI);
            if ((origEvent instanceof HintDebugCommandEvent) || isInitialForHint) {
                bus.publish(new NeedCreatedEventForDebugHint(needURI, wonNodeUri, debugNeedDataset, null));
            } else if ((origEvent instanceof ConnectDebugCommandEvent) || isInitialForConnect) {
                bus.publish(new NeedCreatedEventForDebugConnect(needURI, wonNodeUri, debugNeedDataset, null));
            } else {
                bus.publish(new NeedCreatedEvent(needURI, wonNodeUri, debugNeedDataset, null));
            }
        }
    };
    EventListener failureCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
            logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
            EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
            bus.publish(new NeedCreationFailedEvent(wonNodeUri));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
    ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
    logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) NeedCreatedEventForDebugConnect(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugConnect) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) NeedCreatedEventForDebugHint(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugHint) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) WonMessage(won.protocol.message.WonMessage) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Example 32 with EventListenerContext

use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.

the class ExecuteDeactivateNeedCommandAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof DeactivateNeedCommandEvent))
        return;
    DeactivateNeedCommandEvent deactivateNeedCommandEvent = (DeactivateNeedCommandEvent) event;
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = ctx.getEventBus();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    WonMessage deactivateNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri);
    EventListener successCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            logger.debug("need creation successful, new need URI is {}", needURI);
            bus.publish(new DeactivateNeedCommandSuccessEvent(needURI, deactivateNeedCommandEvent));
        }
    };
    EventListener failureCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
            logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
            bus.publish(new DeactivateNeedCommandFailureEvent(needURI, deactivateNeedCommandEvent, textMessage));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(deactivateNeedMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", deactivateNeedMessage.getMessageURI());
    ctx.getWonMessageSender().sendWonMessage(deactivateNeedMessage);
    logger.debug("need creation message sent with message URI {}", deactivateNeedMessage.getMessageURI());
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DeactivateNeedCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandFailureEvent) DeactivateNeedCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandSuccessEvent) WonMessage(won.protocol.message.WonMessage) WonNodeInformationService(won.protocol.service.WonNodeInformationService) DeactivateNeedCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandFailureEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) DeactivateNeedCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandSuccessEvent) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) EventListener(won.bot.framework.eventbot.listener.EventListener) URI(java.net.URI)

Example 33 with EventListenerContext

use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.

the class TelegramMessageReceivedAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventBus bus = getEventListenerContext().getEventBus();
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof TelegramMessageReceivedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        Update update = ((TelegramMessageReceivedEvent) event).getUpdate();
        Message message = update.getMessage();
        CallbackQuery callbackQuery = update.getCallbackQuery();
        if (message != null && message.isCommand()) {
            wonTelegramBotHandler.getCommandRegistry().executeCommand(wonTelegramBotHandler, message);
        } else if (callbackQuery != null && update.hasCallbackQuery()) {
            message = callbackQuery.getMessage();
            String data = callbackQuery.getData();
            WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getMessageId());
            AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery();
            answerCallbackQuery.setCallbackQueryId(callbackQuery.getId());
            switch(correspondingURI.getType()) {
                case NEED:
                    break;
                case CONNECTION:
                    if ("0".equals(data)) {
                        // CLOSE CONNECTION
                        Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
                        Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
                        bus.publish(new CloseCommandEvent(con));
                        answerCallbackQuery.setText("Closed Connection");
                    } else if ("1".equals(data)) {
                        // ACCEPT CONNECTION
                        Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
                        URI remoteNeed = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
                        URI localNeed = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
                        bus.publish(new ConnectCommandEvent(localNeed, remoteNeed));
                        answerCallbackQuery.setText("Opened Connection");
                    }
                    break;
            }
            wonTelegramBotHandler.answerCallbackQuery(answerCallbackQuery);
            EditMessageReplyMarkup editMessageReplyMarkup = new EditMessageReplyMarkup();
            editMessageReplyMarkup.setMessageId(message.getMessageId());
            editMessageReplyMarkup.setChatId(message.getChatId());
            editMessageReplyMarkup.setReplyMarkup(null);
            wonTelegramBotHandler.editMessageReplyMarkup(editMessageReplyMarkup);
        } else if (message != null && message.isReply() && message.hasText()) {
            WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getReplyToMessage().getMessageId());
            Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
            Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
            Model messageModel = WonRdfUtils.MessageUtils.textMessage(message.getText());
            bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
        }
    }
}
Also used : AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) Message(org.telegram.telegrambots.api.objects.Message) CallbackQuery(org.telegram.telegrambots.api.objects.CallbackQuery) Connection(won.protocol.model.Connection) Update(org.telegram.telegrambots.api.objects.Update) EventBus(won.bot.framework.eventbot.bus.EventBus) Model(org.apache.jena.rdf.model.Model) WonTelegramBotHandler(won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler) TelegramMessageReceivedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramMessageReceivedEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) TelegramContentExtractor(won.bot.framework.eventbot.action.impl.telegram.util.TelegramContentExtractor) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) WonRdfUtils(won.protocol.util.WonRdfUtils) Event(won.bot.framework.eventbot.event.Event) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) RdfUtils(won.protocol.util.RdfUtils) EventListener(won.bot.framework.eventbot.listener.EventListener) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) EditMessageReplyMarkup(org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) CallbackQuery(org.telegram.telegrambots.api.objects.CallbackQuery) AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) Message(org.telegram.telegrambots.api.objects.Message) Dataset(org.apache.jena.query.Dataset) Connection(won.protocol.model.Connection) EventBus(won.bot.framework.eventbot.bus.EventBus) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) Update(org.telegram.telegrambots.api.objects.Update) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) EditMessageReplyMarkup(org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup) Model(org.apache.jena.rdf.model.Model) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) TelegramMessageReceivedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramMessageReceivedEvent) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Example 34 with EventListenerContext

use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.

the class Hint2TelegramAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof HintFromMatcherEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        Match match = ((HintFromMatcherEvent) event).getMatch();
        WonMessage wonMessage = ((HintFromMatcherEvent) event).getWonMessage();
        URI yourNeedUri = match.getFromNeed();
        URI remoteNeedUri = match.getToNeed();
        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().getHintMessage(chatId, remoteNeedUri, yourNeedUri));
            botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(wonMessage.getReceiverURI(), UriType.CONNECTION));
        } catch (TelegramApiException te) {
            logger.error(te.getMessage());
        }
    }
}
Also used : TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) 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) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) Message(org.telegram.telegrambots.api.objects.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) Match(won.protocol.model.Match)

Example 35 with EventListenerContext

use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.

the class Message2TelegramAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof MessageFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((MessageFromOtherNeedEvent) event).getCon();
        WonMessage wonMessage = ((MessageFromOtherNeedEvent) event).getWonMessage();
        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().getConnectionTextMessage(chatId, remoteNeedUri, yourNeedUri, wonMessage));
            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) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) Message(org.telegram.telegrambots.api.objects.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)44 EventBus (won.bot.framework.eventbot.bus.EventBus)26 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)23 EventListener (won.bot.framework.eventbot.listener.EventListener)22 Event (won.bot.framework.eventbot.event.Event)21 URI (java.net.URI)20 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)16 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)15 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)14 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)14 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)12 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)11 BaseEventListener (won.bot.framework.eventbot.listener.BaseEventListener)11 WonMessage (won.protocol.message.WonMessage)11 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)10 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)10 Dataset (org.apache.jena.query.Dataset)9 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)8 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)7 Connection (won.protocol.model.Connection)7