Search in sources :

Example 1 with AutomaticMonitoredMessageResponderListener

use of won.bot.framework.eventbot.listener.impl.AutomaticMonitoredMessageResponderListener in project webofneeds by researchstudio-sat.

the class ConversationBotMonitored method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // create needs every trigger execution until 2 needs are created
    this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS);
    bus.subscribe(ActEvent.class, this.needCreator);
    // count until 2 needs were created, then
    // * connect the 2 needs
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_NEEDS, new ConnectFromListToListAction(ctx, ctx.getBotContextWrapper().getNeedCreateListName(), ctx.getBotContextWrapper().getNeedCreateListName(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hello," + "I am the ConversationBot, a simple bot that will exchange " + "messages and deactivate its needs after some time."));
    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, I am the ConverssationBot."));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // add a listener that auto-responds to messages by a message and at different stages of messages processing fires
    // different monitoring events. After specified number of messages, it unsubscribes from all events.
    // subscribe it to:
    // * message events - so it responds
    // * open events - so it initiates the chain reaction of responses
    this.autoResponder = new AutomaticMonitoredMessageResponderListener(ctx, NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(MessageFromOtherNeedEvent.class, this.autoResponder);
    // add a listener that closes the connection after it has seen 10 messages
    this.connectionCloser = new ActionOnceAfterNEventsListener(ctx, NO_OF_MESSAGES, new CloseConnectionAction(ctx, "Farewell!"));
    bus.subscribe(MessageFromOtherNeedEvent.class, this.connectionCloser);
    // add a listener that closes the connection when a failureEvent occurs
    EventListener onFailureConnectionCloser = new ActionOnEventListener(ctx, new CloseConnectionAction(ctx, "Farewell!"));
    bus.subscribe(FailureResponseEvent.class, onFailureConnectionCloser);
    // add a listener that auto-responds to a close message with a deactivation of both needs.
    // subscribe it to:
    // * close events
    this.needDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(CloseFromOtherNeedEvent.class, this.needDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's messaging work is done and connection messages linked data can be crawled
    this.crawlReadySignaller = new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            bus.publish(new CrawlReadyEvent());
        }
    });
    bus.subscribe(NeedDeactivatedEvent.class, this.crawlReadySignaller);
    // add a listener that, when crawl is done, tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnEventListener(ctx, new SignalWorkDoneAction(ctx));
    bus.subscribe(CrawlDoneEvent.class, this.workDoneSignaller);
    // add a listener that reacts to monitoring events
    this.monitor = new ActionOnEventListener(ctx, "msgMonitor", new MessageLifecycleMonitoringAction(ctx));
    bus.subscribe(MessageDispatchStartedEvent.class, this.monitor);
    bus.subscribe(MessageDispatchedEvent.class, this.monitor);
    bus.subscribe(SuccessResponseEvent.class, this.monitor);
    bus.subscribe(MessageFromOtherNeedEvent.class, this.monitor);
    bus.subscribe(CrawlReadyEvent.class, this.monitor);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) CloseConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) MessageLifecycleMonitoringAction(won.bot.framework.eventbot.action.impl.monitor.MessageLifecycleMonitoringAction) CrawlReadyEvent(won.bot.framework.eventbot.event.impl.monitor.CrawlReadyEvent) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) NeedDeactivatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent) MessageDispatchedEvent(won.bot.framework.eventbot.event.impl.monitor.MessageDispatchedEvent) CrawlReadyEvent(won.bot.framework.eventbot.event.impl.monitor.CrawlReadyEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) CrawlDoneEvent(won.bot.framework.eventbot.event.impl.monitor.CrawlDoneEvent) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) MessageDispatchStartedEvent(won.bot.framework.eventbot.event.impl.monitor.MessageDispatchStartedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) AutomaticMonitoredMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMonitoredMessageResponderListener)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)1 MessageLifecycleMonitoringAction (won.bot.framework.eventbot.action.impl.monitor.MessageLifecycleMonitoringAction)1 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)1 DeactivateAllNeedsAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction)1 CloseConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction)1 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)1 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 Event (won.bot.framework.eventbot.event.Event)1 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)1 CrawlDoneEvent (won.bot.framework.eventbot.event.impl.monitor.CrawlDoneEvent)1 CrawlReadyEvent (won.bot.framework.eventbot.event.impl.monitor.CrawlReadyEvent)1 MessageDispatchStartedEvent (won.bot.framework.eventbot.event.impl.monitor.MessageDispatchStartedEvent)1 MessageDispatchedEvent (won.bot.framework.eventbot.event.impl.monitor.MessageDispatchedEvent)1 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)1 NeedDeactivatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent)1 BaseEventListener (won.bot.framework.eventbot.listener.BaseEventListener)1 EventListener (won.bot.framework.eventbot.listener.EventListener)1