use of won.bot.framework.eventbot.event.MessageEvent in project webofneeds by researchstudio-sat.
the class ServiceAtomCreatedAtomRelationFilter method accept.
@Override
public boolean accept(Event event) {
BotContextWrapper botContextWrapper = getContext().getBotContextWrapper();
if (botContextWrapper instanceof ServiceAtomContext && event instanceof MessageEvent) {
ServiceAtomContext serviceAtomContext = (ServiceAtomContext) botContextWrapper;
URI serviceAtomUri = serviceAtomContext.getServiceAtomUri();
if (Objects.nonNull(serviceAtomUri)) {
MessageEvent messageEvent = (MessageEvent) event;
WonMessage wonMessage = messageEvent.getWonMessage();
if (Objects.equals(wonMessage.getRecipientAtomURI(), serviceAtomUri)) {
URI senderAtomUri = wonMessage.getSenderAtomURI();
return getContext().getBotContextWrapper().isAtomKnown(senderAtomUri);
} else if (Objects.equals(wonMessage.getSenderAtomURI(), serviceAtomUri)) {
URI recipientAtomUri = wonMessage.getRecipientAtomURI();
return getContext().getBotContextWrapper().isAtomKnown(recipientAtomUri);
}
return false;
}
}
return false;
}
use of won.bot.framework.eventbot.event.MessageEvent in project webofneeds by researchstudio-sat.
the class DebugBotIncomingMessageToEventMappingAction method handleTextMessageEvent.
private void handleTextMessageEvent(final ConnectionSpecificEvent messageEvent) {
if (messageEvent instanceof MessageEvent) {
EventListenerContext ctx = getEventListenerContext();
EventBus bus = ctx.getEventBus();
Connection con = ((BaseNeedAndConnectionSpecificEvent) messageEvent).getCon();
WonMessage msg = ((MessageEvent) messageEvent).getWonMessage();
String message = extractTextMessageFromWonMessage(msg);
try {
if (message == null) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Whatever you sent me there, it was not a normal text message. I'm expecting a <message> won:hasTextMessage \"Some text\" triple in that message.");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
} else if (PATTERN_USAGE.matcher(message).matches()) {
bus.publish(new UsageDebugCommandEvent(con));
} else if (PATTERN_HINT.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll create a new need and make it send a hint to you.");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
bus.publish(new HintDebugCommandEvent(con));
} else if (PATTERN_CONNECT.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll create a new need and make it send a connect to you.");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
bus.publish(new ConnectDebugCommandEvent(con));
} else if (PATTERN_CLOSE.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll close this connection");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
bus.publish(new CloseCommandEvent(con));
} else if (PATTERN_DEACTIVATE.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll deactivate this need. This will close the connection we are currently talking on.");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
bus.publish(new DeactivateNeedCommandEvent(con.getNeedURI()));
} else if (PATTERN_CHATTY_ON.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll send you messages spontaneously from time to time.");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
bus.publish(new SetChattinessDebugCommandEvent(con, true));
} else if (PATTERN_CHATTY_OFF.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, from now on I will be quiet and only respond to your messages.");
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
bus.publish(new SetChattinessDebugCommandEvent(con, false));
} else if (PATTERN_CACHE_EAGER.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll put any message I receive or send into the RDF cache. This slows down message processing in general, but operations that require crawling connection data will be faster.");
bus.publish(new SetCacheEagernessCommandEvent(true));
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
} else if (PATTERN_CACHE_LAZY.matcher(message).matches()) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I won't put messages I receive or send into the RDF cache. This speeds up message processing in general, but operations that require crawling connection data will be slowed down.");
bus.publish(new SetCacheEagernessCommandEvent(false));
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
} else if (PATTERN_SEND_N.matcher(message).matches()) {
Matcher m = PATTERN_SEND_N.matcher(message);
m.find();
String nStr = m.group(1);
int n = Integer.parseInt(nStr);
bus.publish(new SendNDebugCommandEvent(con, n));
} else if (PATTERN_VALIDATE.matcher(message).matches()) {
validate(ctx, bus, con);
} else if (PATTERN_RETRACT.matcher(message).matches()) {
Matcher m = PATTERN_RETRACT.matcher(message);
m.matches();
boolean useWrongSender = m.group(3) != null;
boolean retractProposes = m.group(4) != null;
retract(ctx, bus, con, useWrongSender, retractProposes);
} else if (PATTERN_REJECT.matcher(message).matches()) {
Matcher m = PATTERN_REJECT.matcher(message);
m.matches();
boolean useWrongSender = m.group(2) != null;
reject(ctx, bus, con, useWrongSender);
} else if (PATTERN_PROPOSE.matcher(message).matches()) {
Matcher m = PATTERN_PROPOSE.matcher(message);
m.matches();
boolean my = m.group(3) != null;
boolean any = m.group(4) != null;
int count = m.group(5) == null ? 1 : Integer.parseInt(m.group(5));
propose(ctx, bus, con, any || !my, any || my, count);
} else if (PATTERN_ACCEPT.matcher(message).matches()) {
accept(ctx, bus, con);
} else if (PATTERN_CANCEL.matcher(message).matches()) {
cancel(ctx, bus, con);
} else {
// default: answer with eliza.
bus.publish(new MessageToElizaEvent(con, message));
}
} catch (Exception e) {
// error: send an error message
Model messageModel = WonRdfUtils.MessageUtils.textMessage("Did not understand your command '" + message + "': " + e.getClass().getSimpleName() + ":" + e.getMessage());
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
}
}
}
use of won.bot.framework.eventbot.event.MessageEvent in project webofneeds by researchstudio-sat.
the class TextMessageCommandFilter method accept.
@Override
public boolean accept(Event event) {
if (event instanceof MessageEvent) {
MessageEvent messageEvent = (MessageEvent) event;
String message = extractTextMessageFromWonMessage(((MessageEvent) event).getWonMessage());
return usageBehaviour.isMatchingBotCommand(message);
}
return false;
}
use of won.bot.framework.eventbot.event.MessageEvent in project webofneeds by researchstudio-sat.
the class TextMessageCommandExecutor method doRun.
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
if (event instanceof BaseAtomAndConnectionSpecificEvent) {
ConnectionSpecificEvent messageEvent = (ConnectionSpecificEvent) event;
if (messageEvent instanceof MessageEvent) {
EventListenerContext ctx = getEventListenerContext();
EventBus bus = ctx.getEventBus();
Connection con = ((BaseAtomAndConnectionSpecificEvent) messageEvent).getCon();
WonMessage msg = ((MessageEvent) messageEvent).getWonMessage();
String message = extractTextMessageFromWonMessage(msg);
try {
if (message != null) {
// Ignore anything that does not contain a textmessage
for (TextMessageCommand textMessageCommand : textMessageCommands) {
if (textMessageCommand.matchesCommand(message)) {
try {
if (textMessageCommand instanceof PatternMatcherTextMessageCommand) {
((PatternMatcherTextMessageCommand) textMessageCommand).execute(con, ((PatternMatcherTextMessageCommand) textMessageCommand).getMatcher(message));
} else if (textMessageCommand instanceof EqualsTextMessageCommand) {
((EqualsTextMessageCommand) textMessageCommand).execute(con);
} else if (textMessageCommand instanceof StartsWithTextMessageCommand) {
((StartsWithTextMessageCommand) textMessageCommand).execute(con, message);
}
break;
} catch (UnsupportedOperationException e) {
logger.warn("TextMessageCommand cant be executed due to: {}", e.getMessage());
}
}
}
}
} catch (Exception e) {
// error: send an error message
bus.publish(new ConnectionMessageCommandEvent(con, "Did not understand your command '" + message + "': " + e.getClass().getSimpleName() + ":" + e.getMessage()));
}
}
}
}
Aggregations