Search in sources :

Example 1 with MessageToElizaEvent

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

the class AnswerWithElizaAction method doRun.

@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
    if (event instanceof MessageToElizaEvent) {
        MessageToElizaEvent messageToElizaEvent = (MessageToElizaEvent) event;
        String elizaResponse;
        synchronized (eliza) {
            elizaResponse = eliza.processInput(((MessageToElizaEvent) event).getMessage());
        }
        Model messageModel = WonRdfUtils.MessageUtils.textMessage(elizaResponse);
        getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(messageToElizaEvent.getCon(), messageModel));
    }
}
Also used : 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 2 with MessageToElizaEvent

use of won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent 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)

Aggregations

Model (org.apache.jena.rdf.model.Model)2 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)2 MessageToElizaEvent (won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent)2 Matcher (java.util.regex.Matcher)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 BaseNeedAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent)1 MessageEvent (won.bot.framework.eventbot.event.MessageEvent)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 SendNDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.SendNDebugCommandEvent)1 SetCacheEagernessCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent)1 SetChattinessDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.SetChattinessDebugCommandEvent)1 UsageDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.UsageDebugCommandEvent)1 WonMessage (won.protocol.message.WonMessage)1 Connection (won.protocol.model.Connection)1