use of won.bot.framework.eventbot.bus.EventBus in project webofneeds by researchstudio-sat.
the class CreateDebugNeedWithFacetsAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
String replyText = "";
URI reactingToNeedUriTmp = null;
Dataset needDataset = null;
if (event instanceof NeedSpecificEvent) {
reactingToNeedUriTmp = ((NeedSpecificEvent) event).getNeedURI();
} else {
logger.warn("could not process non-need specific event {}", event);
return;
}
if (event instanceof NeedCreatedEventForMatcher) {
needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
} else if (event instanceof HintDebugCommandEvent) {
reactingToNeedUriTmp = ((HintDebugCommandEvent) event).getRemoteNeedURI();
} else if (event instanceof ConnectDebugCommandEvent) {
reactingToNeedUriTmp = ((ConnectDebugCommandEvent) event).getRemoteNeedURI();
} else {
logger.error("CreateEchoNeedWithFacetsAction cannot handle " + event.getClass().getName());
return;
}
final URI reactingToNeedUri = reactingToNeedUriTmp;
String titleString = null;
boolean createNeed = true;
if (needDataset != null) {
DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
createNeed = needModelWrapper.hasFlag(WON.USED_FOR_TESTING) && !needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME);
}
// if create need is false do not continue the debug need creation
if (!createNeed)
return;
if (titleString != null) {
if (isInitialForConnect) {
replyText = "Debugging with initial connect: " + titleString;
} else if (isInitialForHint) {
replyText = "Debugging with initial hint: " + titleString;
} else {
replyText = "Debugging: " + titleString;
}
} else {
replyText = "Debug Need No. " + counter.increment();
}
EventListenerContext ctx = getEventListenerContext();
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, replyText);
needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the DebugBot.");
for (URI facet : facets) {
needModelWrapper.addFacetUri(facet.toString());
}
final Dataset debugNeedDataset = needModelWrapper.copyDataset();
final Event origEvent = event;
logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(debugNeedDataset), 150));
WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, debugNeedDataset);
// remember the need URI so we can react to success/failure responses
EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
EventListener successCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
logger.debug("need creation successful, new need URI is {}", needURI);
// save the mapping between the original and the reaction in to the context.
getEventListenerContext().getBotContextWrapper().addUriAssociation(reactingToNeedUri, needURI);
if ((origEvent instanceof HintDebugCommandEvent) || isInitialForHint) {
bus.publish(new NeedCreatedEventForDebugHint(needURI, wonNodeUri, debugNeedDataset, null));
} else if ((origEvent instanceof ConnectDebugCommandEvent) || isInitialForConnect) {
bus.publish(new NeedCreatedEventForDebugConnect(needURI, wonNodeUri, debugNeedDataset, null));
} else {
bus.publish(new NeedCreatedEvent(needURI, wonNodeUri, debugNeedDataset, null));
}
}
};
EventListener failureCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
bus.publish(new NeedCreationFailedEvent(wonNodeUri));
}
};
EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
use of won.bot.framework.eventbot.bus.EventBus in project webofneeds by researchstudio-sat.
the class ExecuteDeactivateNeedCommandAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (!(event instanceof DeactivateNeedCommandEvent))
return;
DeactivateNeedCommandEvent deactivateNeedCommandEvent = (DeactivateNeedCommandEvent) event;
EventListenerContext ctx = getEventListenerContext();
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
WonMessage deactivateNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri);
EventListener successCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
logger.debug("need creation successful, new need URI is {}", needURI);
bus.publish(new DeactivateNeedCommandSuccessEvent(needURI, deactivateNeedCommandEvent));
}
};
EventListener failureCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
bus.publish(new DeactivateNeedCommandFailureEvent(needURI, deactivateNeedCommandEvent, textMessage));
}
};
EventBotActionUtils.makeAndSubscribeResponseListener(deactivateNeedMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", deactivateNeedMessage.getMessageURI());
ctx.getWonMessageSender().sendWonMessage(deactivateNeedMessage);
logger.debug("need creation message sent with message URI {}", deactivateNeedMessage.getMessageURI());
}
use of won.bot.framework.eventbot.bus.EventBus in project webofneeds by researchstudio-sat.
the class TelegramMessageReceivedAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventBus bus = getEventListenerContext().getEventBus();
EventListenerContext ctx = getEventListenerContext();
if (event instanceof TelegramMessageReceivedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
Update update = ((TelegramMessageReceivedEvent) event).getUpdate();
Message message = update.getMessage();
CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null && message.isCommand()) {
wonTelegramBotHandler.getCommandRegistry().executeCommand(wonTelegramBotHandler, message);
} else if (callbackQuery != null && update.hasCallbackQuery()) {
message = callbackQuery.getMessage();
String data = callbackQuery.getData();
WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getMessageId());
AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery();
answerCallbackQuery.setCallbackQueryId(callbackQuery.getId());
switch(correspondingURI.getType()) {
case NEED:
break;
case CONNECTION:
if ("0".equals(data)) {
// CLOSE CONNECTION
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
bus.publish(new CloseCommandEvent(con));
answerCallbackQuery.setText("Closed Connection");
} else if ("1".equals(data)) {
// ACCEPT CONNECTION
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
URI remoteNeed = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
URI localNeed = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
bus.publish(new ConnectCommandEvent(localNeed, remoteNeed));
answerCallbackQuery.setText("Opened Connection");
}
break;
}
wonTelegramBotHandler.answerCallbackQuery(answerCallbackQuery);
EditMessageReplyMarkup editMessageReplyMarkup = new EditMessageReplyMarkup();
editMessageReplyMarkup.setMessageId(message.getMessageId());
editMessageReplyMarkup.setChatId(message.getChatId());
editMessageReplyMarkup.setReplyMarkup(null);
wonTelegramBotHandler.editMessageReplyMarkup(editMessageReplyMarkup);
} else if (message != null && message.isReply() && message.hasText()) {
WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getReplyToMessage().getMessageId());
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
Model messageModel = WonRdfUtils.MessageUtils.textMessage(message.getText());
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
}
}
}
use of won.bot.framework.eventbot.bus.EventBus in project webofneeds by researchstudio-sat.
the class CommentBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
final EventBus bus = getEventBus();
CommentBotContextWrapper botContextWrapper = (CommentBotContextWrapper) getBotContextWrapper();
// create needs every trigger execution until 2 needs are created
this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getNeedCreateListName()), NO_OF_NEEDS);
bus.subscribe(ActEvent.class, this.needCreator);
// count until 1 need is created, then create a comment facet
this.commentFacetCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCommentListName(), FacetType.CommentFacet.getURI()), 1);
bus.subscribe(NeedCreatedEvent.class, this.commentFacetCreator);
this.needConnector = new ActionOnceAfterNEventsListener(ctx, 2, new ConnectFromListToListAction(ctx, botContextWrapper.getNeedCreateListName(), botContextWrapper.getCommentListName(), FacetType.OwnerFacet.getURI(), FacetType.CommentFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi, I am the " + "CommentBot."));
bus.subscribe(NeedCreatedEvent.class, this.needConnector);
this.autoOpener = new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi!"));
bus.subscribe(OpenFromOtherNeedEvent.class, this.autoOpener);
bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
BaseEventListener assertionRunner = new ActionOnceAfterNEventsListener(ctx, 1, new BaseEventBotAction(ctx) {
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
executeAssertionsForEstablishedConnectionInternal(bus);
}
});
bus.subscribe(OpenFromOtherNeedEvent.class, assertionRunner);
// deactivate all needs when the assertion was executed
this.allNeedsDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
bus.subscribe(AssertionsExecutedEvent.class, this.allNeedsDeactivator);
// add a listener that counts two NeedDeactivatedEvents and then tells the
// framework that the bot's work is done
this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, 2, new SignalWorkDoneAction(ctx));
bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
use of won.bot.framework.eventbot.bus.EventBus 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