use of won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent 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.impl.command.close.CloseCommandEvent in project webofneeds by researchstudio-sat.
the class MailCommandAction method processReferenceMailCommands.
private void processReferenceMailCommands(MimeMessage message, WonURI wonUri) {
MailBotContextWrapper botContextWrapper = ((MailBotContextWrapper) getEventListenerContext().getBotContextWrapper());
EventBus bus = getEventListenerContext().getEventBus();
try {
if (wonUri == null) {
throw new NullPointerException("No corresponding wonUri found");
}
URI needUri;
URI remoteNeedUri = null;
Dataset connectionRDF = null;
switch(wonUri.getType()) {
case CONNECTION:
connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(wonUri.getUri());
needUri = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, wonUri.getUri());
remoteNeedUri = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, wonUri.getUri());
break;
case NEED:
default:
needUri = wonUri.getUri();
break;
}
MimeMessage originalMessage = botContextWrapper.getMimeMessageForURI(needUri);
if (originalMessage == null) {
throw new NullPointerException("no originalmessage found");
}
logger.debug("Validate mailorigin with originalmail:");
logger.debug("Command Message Sender: " + message.getFrom());
logger.debug("Original Message Sender: " + originalMessage.getFrom());
String senderNew = ((InternetAddress) message.getFrom()[0]).getAddress();
String senderOriginal = ((InternetAddress) originalMessage.getFrom()[0]).getAddress();
if (!senderNew.equals(senderOriginal)) {
throw new AccessControlException("Sender of original and command mail are not equal");
} else {
logger.debug("Sender of original and command mail are not equal, continue with command processing");
}
ActionType actionType = determineAction(getEventListenerContext(), message, wonUri);
logger.debug("Executing " + actionType + " on uri: " + wonUri.getUri() + " of type " + wonUri.getType());
Connection con;
switch(actionType) {
case CLOSE_CONNECTION:
con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
bus.publish(new CloseCommandEvent(con));
break;
case OPEN_CONNECTION:
bus.publish(new ConnectCommandEvent(needUri, remoteNeedUri));
break;
case IMPLICIT_OPEN_CONNECTION:
bus.publish(new ConnectCommandEvent(needUri, remoteNeedUri, mailContentExtractor.getTextMessage(message)));
break;
case SENDMESSAGE:
con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
Model messageModel = WonRdfUtils.MessageUtils.textMessage(mailContentExtractor.getTextMessage(message));
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
break;
case CLOSE_NEED:
bus.publish(new DeactivateNeedCommandEvent(needUri));
break;
case NO_ACTION:
default:
// INVALID COMMAND
logger.error("No command was given or assumed");
break;
}
} catch (AccessControlException ace) {
logger.error("ACCESS RESTRICTION: sender of original and command mail are not equal, command will be blocked");
} catch (Exception e) {
logger.error("no reply mail was set or found: " + e.getMessage());
}
}
use of won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent 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));
}
}
}
Aggregations