Search in sources :

Example 1 with RespondWithEchoToMessageAction

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

the class EchoBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // register with WoN nodes, be notified when new needs are created
    RegisterMatcherAction registerMatcherAction = new RegisterMatcherAction(ctx);
    this.matcherRegistrator = new ActionOnEventListener(ctx, registerMatcherAction, 1);
    bus.subscribe(ActEvent.class, this.matcherRegistrator);
    RandomDelayedAction delayedRegistration = new RandomDelayedAction(ctx, registrationMatcherRetryInterval, registrationMatcherRetryInterval, 0, registerMatcherAction);
    ActionOnEventListener matcherRetryRegistrator = new ActionOnEventListener(ctx, delayedRegistration);
    bus.subscribe(MatcherRegisterFailedEvent.class, matcherRetryRegistrator);
    // create the echo need - if we're not reacting to the creation of our own echo need.
    this.needCreator = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), prepareCreateNeedAction(ctx));
    bus.subscribe(NeedCreatedEventForMatcher.class, this.needCreator);
    // as soon as the echo need is created, connect to original
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new RandomDelayedAction(ctx, 5000, 5000, 1, new ConnectWithAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), "Greetings! I am the EchoBot! I will repeat everything you say, which you might " + "find useful for testing purposes.")));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // add a listener that auto-responds to messages by a message
    // after 10 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 ActionOnEventListener(ctx, new RespondWithEchoToMessageAction(ctx));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(MessageFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new LogAction(ctx, "received close message from remote need.")));
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) RegisterMatcherAction(won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction) NotFilter(won.bot.framework.eventbot.filter.impl.NotFilter) NeedUriInNamedListFilter(won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter) RespondWithEchoToMessageAction(won.bot.framework.eventbot.action.impl.wonmessage.RespondWithEchoToMessageAction) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ConnectWithAssociatedNeedAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectWithAssociatedNeedAction)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 RegisterMatcherAction (won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction)1 ConnectWithAssociatedNeedAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectWithAssociatedNeedAction)1 RespondWithEchoToMessageAction (won.bot.framework.eventbot.action.impl.wonmessage.RespondWithEchoToMessageAction)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 NeedUriInNamedListFilter (won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter)1 NotFilter (won.bot.framework.eventbot.filter.impl.NotFilter)1 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)1