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.")));
}
Aggregations