Search in sources :

Example 1 with StartsWithTextMessageCommand

use of won.bot.framework.extensions.textmessagecommand.command.StartsWithTextMessageCommand 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

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 BaseAtomAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseAtomAndConnectionSpecificEvent)1 ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)1 MessageEvent (won.bot.framework.eventbot.event.MessageEvent)1 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)1 EqualsTextMessageCommand (won.bot.framework.extensions.textmessagecommand.command.EqualsTextMessageCommand)1 PatternMatcherTextMessageCommand (won.bot.framework.extensions.textmessagecommand.command.PatternMatcherTextMessageCommand)1 StartsWithTextMessageCommand (won.bot.framework.extensions.textmessagecommand.command.StartsWithTextMessageCommand)1 TextMessageCommand (won.bot.framework.extensions.textmessagecommand.command.TextMessageCommand)1 WonMessage (won.protocol.message.WonMessage)1 Connection (won.protocol.model.Connection)1