Search in sources :

Example 1 with MessageEvent

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;
}
Also used : MessageEvent(won.bot.framework.eventbot.event.MessageEvent) WonMessage(won.protocol.message.WonMessage) BotContextWrapper(won.bot.framework.bot.context.BotContextWrapper) URI(java.net.URI) ServiceAtomContext(won.bot.framework.extensions.serviceatom.ServiceAtomContext)

Example 2 with MessageEvent

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));
        }
    }
}
Also used : UsageDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.UsageDebugCommandEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BaseNeedAndConnectionSpecificEvent(won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent) Matcher(java.util.regex.Matcher) MessageEvent(won.bot.framework.eventbot.event.MessageEvent) Connection(won.protocol.model.Connection) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) SendNDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SendNDebugCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) SetCacheEagernessCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) SetChattinessDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetChattinessDebugCommandEvent) WonMessage(won.protocol.message.WonMessage) Model(org.apache.jena.rdf.model.Model) MessageToElizaEvent(won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Example 3 with MessageEvent

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;
}
Also used : MessageEvent(won.bot.framework.eventbot.event.MessageEvent)

Example 4 with MessageEvent

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()));
            }
        }
    }
}
Also used : StartsWithTextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.StartsWithTextMessageCommand) EqualsTextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.EqualsTextMessageCommand) PatternMatcherTextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.PatternMatcherTextMessageCommand) TextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.TextMessageCommand) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) MessageEvent(won.bot.framework.eventbot.event.MessageEvent) Connection(won.protocol.model.Connection) EventBus(won.bot.framework.eventbot.bus.EventBus) StartsWithTextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.StartsWithTextMessageCommand) ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) BaseAtomAndConnectionSpecificEvent(won.bot.framework.eventbot.event.BaseAtomAndConnectionSpecificEvent) EqualsTextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.EqualsTextMessageCommand) WonMessage(won.protocol.message.WonMessage) PatternMatcherTextMessageCommand(won.bot.framework.extensions.textmessagecommand.command.PatternMatcherTextMessageCommand) BaseAtomAndConnectionSpecificEvent(won.bot.framework.eventbot.event.BaseAtomAndConnectionSpecificEvent) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Aggregations

MessageEvent (won.bot.framework.eventbot.event.MessageEvent)4 WonMessage (won.protocol.message.WonMessage)3 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)2 Connection (won.protocol.model.Connection)2 URI (java.net.URI)1 Matcher (java.util.regex.Matcher)1 Model (org.apache.jena.rdf.model.Model)1 BotContextWrapper (won.bot.framework.bot.context.BotContextWrapper)1 BaseAtomAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseAtomAndConnectionSpecificEvent)1 BaseNeedAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent)1 ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)1 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)1 DeactivateNeedCommandEvent (won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent)1 ConnectDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent)1 HintDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent)1 MessageToElizaEvent (won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent)1 SendNDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.SendNDebugCommandEvent)1 SetCacheEagernessCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent)1