Search in sources :

Example 1 with WonMessageReceivedOnConnectionEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent in project webofneeds by researchstudio-sat.

the class OpenConnectionDebugAction method doRun.

@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof ConnectFromOtherNeedEvent)) {
        return;
    }
    if (event instanceof WonMessageReceivedOnConnectionEvent) {
        WonMessage msg = ((WonMessageReceivedOnConnectionEvent) event).getWonMessage();
        String message = WonRdfUtils.MessageUtils.getTextMessage(msg);
        if (message == null) {
            message = "";
        }
        Matcher ignoreMatcher = PATTERN_IGNORE.matcher(message);
        if (ignoreMatcher.find()) {
            logger.debug("not reacting to incoming message of type {} as the welcome message contained 'ignore'", msg.getMessageType());
            return;
        }
        Matcher waitMatcher = PATTERN_WAIT.matcher(message);
        final boolean wait = waitMatcher.find();
        int waitSeconds = 15;
        if (wait && waitMatcher.groupCount() == 2) {
            waitSeconds = Integer.parseInt(waitMatcher.group(2));
        }
        Matcher denyMatcher = PATTERN_DENY.matcher(message);
        final boolean deny = denyMatcher.find();
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        logger.debug("auto-replying to connect for connection {}", connectEvent.getConnectionURI());
        URI connectionUri = connectEvent.getConnectionURI();
        String finalWelcomeMessage = welcomeMessage;
        if (wait || deny) {
            finalWelcomeMessage = welcomeMessage + " " + (deny ? "Denying" : "Accepting") + " your request " + (wait ? " after a timeout of " + waitSeconds + " seconds" : "");
        } else {
            finalWelcomeMessage = welcomeMessage + " " + welcomeHelpMessage;
        }
        final WonMessage toSend = deny ? createCloseWonMessage(connectionUri, finalWelcomeMessage) : createOpenWonMessage(connectionUri, finalWelcomeMessage);
        Runnable task = () -> {
            getEventListenerContext().getWonMessageSender().sendWonMessage(toSend);
        };
        if (wait) {
            Date when = new Date(System.currentTimeMillis() + waitSeconds * 1000);
            getEventListenerContext().getTaskScheduler().schedule(task, when);
        } else {
            task.run();
        }
    }
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) WonMessageReceivedOnConnectionEvent(won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent) Matcher(java.util.regex.Matcher) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) Date(java.util.Date)

Aggregations

URI (java.net.URI)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)1 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)1 WonMessageReceivedOnConnectionEvent (won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent)1 WonMessage (won.protocol.message.WonMessage)1