use of won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener 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);
}
use of won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener in project webofneeds by researchstudio-sat.
the class DuplicateMessageSendingConversationBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getDuplicateMessageSenderDecorator(getEventListenerContext());
final EventBus bus = getEventBus();
// we're not expecting any failure messages in this test:
bus.subscribe(FailureResponseEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
FailureResponseEvent failureResponseEvent = (FailureResponseEvent) event;
bus.publish(new TestFailedEvent(DuplicateMessageSendingConversationBot.this, "Message failed: " + failureResponseEvent.getOriginalMessageURI() + ": " + WonRdfUtils.MessageUtils.getTextMessage(failureResponseEvent.getFailureMessage())));
}
}));
// create needs every trigger execution until 2 needs are created
bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS));
// connect needs
bus.subscribe(NeedCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_NEEDS * 2, new ConnectFromListToListAction(ctx, getBotContextWrapper().getNeedCreateListName(), getBotContextWrapper().getNeedCreateListName(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!")));
// 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)
bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi!")));
// 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
BaseEventListener autoResponder = new AutomaticMessageResponderListener(ctx, NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
bus.subscribe(OpenFromOtherNeedEvent.class, autoResponder);
bus.subscribe(MessageFromOtherNeedEvent.class, autoResponder);
// add a listener that closes the connection after it has seen 10 messages
bus.subscribe(MessageFromOtherNeedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_MESSAGES, new CloseConnectionAction(ctx, "Bye!")));
// add a listener that closes the connection when a failureEvent occurs
EventListener onFailureConnectionCloser = new ActionOnEventListener(ctx, new CloseConnectionAction(ctx, "Bye!"));
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
bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, new DeactivateAllNeedsAction(ctx), new PublishEventAction(ctx, new TestPassedEvent(this))), 1));
// add a listener that counts two NeedDeactivatedEvents and then tells the
// framework that the bot's work is done
bus.subscribe(NeedDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new SignalWorkDoneAction(ctx)));
}
use of won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener in project webofneeds by researchstudio-sat.
the class ConversationBot 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, "Hi, I am the ConversationBot."));
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 ConversationBot, too!"));
bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
// 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 AutomaticMessageResponderListener(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, "Bye!"));
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, "Bye!"));
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 work is done
this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new SignalWorkDoneAction(ctx));
bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
Aggregations