Search in sources :

Example 1 with FbBotMillEvent

use of co.aurasphere.botmill.fb.event.FbBotMillEvent in project fb-botmill by BotMill.

the class IncomingToOutgoingMessageHandler method handleOutgoingMessage.

/**
	 * Handle outgoing message.
	 *
	 * @param message
	 *            the message
	 */
private void handleOutgoingMessage(MessageEnvelope message) {
    methodMapCatchAll = new ArrayList<FbBotClassMethod>();
    boolean isAuthorized = false;
    for (BotDefinition defClass : ConfigurationUtils.getBotDefinitionInstances()) {
        if (defClass.getClass().isAnnotationPresent(Bot.class)) {
            Bot botClass = defClass.getClass().getAnnotation(Bot.class);
            if (botClass.state().equals(BotBeanState.PROTOTYPE)) {
                try {
                    defClass = defClass.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            // Check authorization first.
            for (Method authorizationMethod : defClass.getClass().getMethods()) {
                if (authorizationMethod.isAnnotationPresent(BotAuthorization.class)) {
                    Boolean ob = false;
                    try {
                        defClass.getClass().getSuperclass().getDeclaredMethod(CONST_INCOMING_MSG_SETNAME, MessageEnvelope.class).invoke(defClass, message);
                        ob = (Boolean) authorizationMethod.invoke(defClass, message);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    if (ob) {
                        isAuthorized = true;
                        break;
                    }
                }
            }
            // if (isAuthorized) {
            for (Method method : defClass.getClass().getMethods()) {
                if (method.isAnnotationPresent(FbBotMillController.class)) {
                    FbBotMillController botMillController = method.getAnnotation(FbBotMillController.class);
                    if (isAuthorized || botMillController.skipAuthorization()) {
                        try {
                            if (botMillController.pattern().equals(".*.") || botMillController.eventType().equals(FbBotMillEventType.ANY)) {
                                FbBotClassMethod fbCm = new FbBotClassMethod();
                                fbCm.setCls(defClass);
                                fbCm.setMethod(method);
                                methodMapCatchAll.add(fbCm);
                            } else {
                                FbBotMillEvent event = toEventActionFrame(botMillController);
                                if (event.verifyEventCondition(message)) {
                                    defClass.getClass().getSuperclass().getDeclaredMethod(CONST_INCOMING_MSG_SETNAME, MessageEnvelope.class).invoke(defClass, message);
                                    defClass.getClass().getSuperclass().getDeclaredMethod(CONST_EVENT_SETNAME, FbBotMillEvent.class).invoke(defClass, event);
                                    FbBotApi.setFbBot(defClass);
                                    method.invoke(defClass, message);
                                    return;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            return;
                        }
                    }
                }
            }
        // }
        }
    }
    // if (isAuthorized) {
    synchronized (this) {
        for (FbBotClassMethod catchAllMethod : methodMapCatchAll) {
            if (catchAllMethod.getMethod().isAnnotationPresent(FbBotMillController.class)) {
                FbBotMillController botMillController = catchAllMethod.getMethod().getAnnotation(FbBotMillController.class);
                if (isAuthorized || botMillController.skipAuthorization()) {
                    try {
                        FbBotMillEvent event = toEventActionFrame(botMillController);
                        if (event.verifyEventCondition(message)) {
                            catchAllMethod.getCls().getClass().getSuperclass().getDeclaredMethod(CONST_INCOMING_MSG_SETNAME, MessageEnvelope.class).invoke(catchAllMethod.getCls(), message);
                            catchAllMethod.getCls().getClass().getSuperclass().getDeclaredMethod(CONST_EVENT_SETNAME, FbBotMillEvent.class).invoke(catchAllMethod.getCls(), event);
                            FbBotApi.setFbBot(catchAllMethod.getCls());
                            catchAllMethod.getMethod().invoke(catchAllMethod.getCls(), message);
                            return;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        return;
                    }
                }
            }
        }
    }
// }
}
Also used : Bot(co.aurasphere.botmill.core.annotation.Bot) Method(java.lang.reflect.Method) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BotMillEventMismatchException(co.aurasphere.botmill.core.internal.exception.BotMillEventMismatchException) BotDefinition(co.aurasphere.botmill.core.BotDefinition) FbBotMillController(co.aurasphere.botmill.fb.model.annotation.FbBotMillController) FbBotMillEvent(co.aurasphere.botmill.fb.event.FbBotMillEvent)

Aggregations

BotDefinition (co.aurasphere.botmill.core.BotDefinition)1 Bot (co.aurasphere.botmill.core.annotation.Bot)1 BotMillEventMismatchException (co.aurasphere.botmill.core.internal.exception.BotMillEventMismatchException)1 FbBotMillEvent (co.aurasphere.botmill.fb.event.FbBotMillEvent)1 FbBotMillController (co.aurasphere.botmill.fb.model.annotation.FbBotMillController)1 MessageEnvelope (co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1