Search in sources :

Example 1 with MessageFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class Message2MailAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof MessageFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((MessageFromOtherNeedEvent) event).getCon();
        URI responseTo = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Someone sent a message for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createMessageMail(originalMail, responseTo, remoteNeedUri, con.getConnectionURI());
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    } else {
        logger.debug("event was not of type MessageFromOtherNeedEvent");
    }
}
Also used : 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) 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)

Example 2 with MessageFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class GroupingBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    GroupBotContextWrapper botContextWrapper = (GroupBotContextWrapper) getBotContextWrapper();
    EventBus bus = getEventBus();
    // for each created need (in the group), add a listener that will auto-respond to messages directed at that need
    // create a filter that only accepts events for needs in the group:
    NeedUriInNamedListFilter groupMemberFilter = new NeedUriInNamedListFilter(ctx, botContextWrapper.getGroupMembersListName());
    // remember the auto-responders in a list
    this.autoResponders = new ArrayList<BaseEventListener>();
    // remember the listeners that wait for all messages
    this.messageCounters = new ArrayList<BaseEventListener>();
    // make a composite filter, with one filter for each autoResponder that wait for the FinishedEvents the responders emit.
    // that filter will be used to shut down all needs after all the autoResponders have finished.
    final OrFilter mainAutoResponderFilter = new OrFilter();
    // listen to NeedCreatedEvents
    this.autoResponderCreator = new ActionOnEventListener(ctx, groupMemberFilter, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            // create a listener that automatically answers messages, only for that need URI. We let it send NO_OF_MESSAGES messages
            logger.debug("created auto responder");
            AutomaticMessageResponderListener listener = new AutomaticMessageResponderListener(ctx, "autoResponder", NeedUriEventFilter.forEvent(event), NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
            // create a listener that publishes a FinishedEvent after having received all messages from the group
            WaitForNEventsListener waitForMessagesListener = new WaitForNEventsListener(ctx, "messageCounter", NeedUriEventFilter.forEvent(event), NO_OF_MESSAGES * (NO_OF_GROUPMEMBERS - 1));
            messageCounters.add(waitForMessagesListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptonce filter to make extra sure we count each listener only once.
            mainAutoResponderFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(waitForMessagesListener)));
            ActionOnEventListener debugger = new ActionOnEventListener(ctx, NeedUriEventFilter.forEvent(event), new BaseEventBotAction(ctx) {

                @Override
                protected void doRun(Event event, EventListener executingListener) throws Exception {
                    if (event instanceof MessageFromOtherNeedEvent) {
                        MessageFromOtherNeedEvent msg = (MessageFromOtherNeedEvent) event;
                        logger.debug("processing event {} wonMessage {} - text message '{}', sent by {} to {}", new Object[] { event.toString(), msg.getWonMessage().getMessageURI(), WonRdfUtils.MessageUtils.getTextMessage(msg.getWonMessage()), msg.getRemoteNeedURI(), msg.getNeedURI() });
                    }
                }
            });
            getEventBus().subscribe(MessageFromOtherNeedEvent.class, debugger);
            // finally, subscribe to the message events
            getEventBus().subscribe(MessageFromOtherNeedEvent.class, waitForMessagesListener);
            getEventBus().subscribe(MessageFromOtherNeedEvent.class, listener);
        }
    });
    getEventBus().subscribe(NeedCreatedEvent.class, this.autoResponderCreator);
    // count until N needs were created, then create need with group facet (the others will connect to that facet)
    this.groupCreator = new ActionOnceAfterNEventsListener(ctx, "groupCreator", NO_OF_GROUPMEMBERS, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getGroupListName(), FacetType.GroupFacet.getURI()));
    bus.subscribe(NeedCreatedEvent.class, this.groupCreator);
    // wait for N+1 needCreatedEvents, then connect the members with the group facet of the third need
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_GROUPMEMBERS + 1, new ConnectFromListToListAction(ctx, botContextWrapper.getGroupListName(), botContextWrapper.getGroupMembersListName(), FacetType.GroupFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi from the " + "GroupingBot!"));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // add a listener that is informed of the connect/open events and that auto-opens
    // subscribe it to:
    // * connect events - so it responds with open
    // * open events - so it responds with open (if the open received was the first open, and we still need to accept the connection)
    this.autoOpener = new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi from the GroupingBot!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // now, once all connections have been opened, make 1 bot send a message to the group, the subsequent listener will cause let wild chatting to begin
    this.conversationStarter = new ActionOnceAfterNEventsListener(ctx, "conversationStarter", NO_OF_GROUPMEMBERS, new RespondToMessageAction(ctx, MILLIS_BETWEEN_MESSAGES));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.conversationStarter);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.messagesDoneListener = new ActionOnceAfterNEventsListener(ctx, "messagesDoneListener", mainAutoResponderFilter, NO_OF_GROUPMEMBERS, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getGroupMembersListName()));
    bus.subscribe(FinishedEvent.class, this.messagesDoneListener);
    // When the group facet need is deactivated, all connections are closed. wait for the close events and signal work done.
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", NO_OF_GROUPMEMBERS, new SignalWorkDoneAction(ctx));
    bus.subscribe(CloseFromOtherNeedEvent.class, this.workDoneSignaller);
    // start the whole thing:
    // create needs every trigger execution until N needs are created
    this.groupMemberCreator = new ActionOnEventListener(ctx, "groupMemberCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getGroupMembersListName(), FacetType.OwnerFacet.getURI()), NO_OF_GROUPMEMBERS);
    bus.subscribe(ActEvent.class, this.groupMemberCreator);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) RespondToMessageAction(won.bot.framework.eventbot.action.impl.wonmessage.RespondToMessageAction) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) WaitForNEventsListener(won.bot.framework.eventbot.listener.impl.WaitForNEventsListener) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) GroupBotContextWrapper(won.bot.framework.bot.context.GroupBotContextWrapper) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) FinishedEvent(won.bot.framework.eventbot.event.impl.listener.FinishedEvent) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) OpenFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) AutomaticMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener)

Example 3 with MessageFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent 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)3 MessageFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent)3 URI (java.net.URI)2 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)2 Connection (won.protocol.model.Connection)2 MimeMessage (javax.mail.internet.MimeMessage)1 Message (org.telegram.telegrambots.api.objects.Message)1 TelegramApiException (org.telegram.telegrambots.exceptions.TelegramApiException)1 GroupBotContextWrapper (won.bot.framework.bot.context.GroupBotContextWrapper)1 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)1 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)1 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)1 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)1 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)1 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)1 RespondToMessageAction (won.bot.framework.eventbot.action.impl.wonmessage.RespondToMessageAction)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 Event (won.bot.framework.eventbot.event.Event)1