Search in sources :

Example 11 with MessageEnvelope

use of co.aurasphere.botmill.fb.model.incoming.MessageEnvelope in project fb-botmill by BotMill.

the class FbBotMillMockMediator method sendPayload.

/**
	 * Sends a payload to all the registered bots. Used to simulate a user
	 * interacting with buttons.
	 * 
	 * @param payload
	 *            the payload to send.
	 */
public void sendPayload(String payload) {
    MessageEnvelope envelope = new MessageEnvelope();
    Postback postback = new Postback();
    postback.setPayload(payload);
    envelope.setPostback(postback);
    envelope.setSender(new User(facebookMockId));
    System.out.println("Sending payload: [" + payload + "] as user : [" + facebookMockId + "].");
    forward(envelope);
    System.out.println("Sent!");
}
Also used : User(co.aurasphere.botmill.fb.model.base.User) Postback(co.aurasphere.botmill.fb.model.incoming.callback.Postback) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)

Example 12 with MessageEnvelope

use of co.aurasphere.botmill.fb.model.incoming.MessageEnvelope 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)

Example 13 with MessageEnvelope

use of co.aurasphere.botmill.fb.model.incoming.MessageEnvelope in project fb-botmill by BotMill.

the class FbBotMillMockMediator method sendQuickReplyPayload.

/**
	 * Sends a quickreply to all the registered bots. Used to simulate a user
	 * interacting with buttons.
	 * 
	 * @param payload
	 *            the payload to send.
	 */
public void sendQuickReplyPayload(String payload) {
    MessageEnvelope envelope = new MessageEnvelope();
    QuickReply quickReply = new QuickReply("Sample", payload);
    ReceivedMessage message = new ReceivedMessage();
    message.setQuickReply(quickReply);
    envelope.setMessage(message);
    envelope.setSender(new User(facebookMockId));
    System.out.println("Sending quick reply: [" + message + "] as user : [" + facebookMockId + "].");
    forward(envelope);
    System.out.println("Sent!");
}
Also used : User(co.aurasphere.botmill.fb.model.base.User) QuickReply(co.aurasphere.botmill.fb.model.outcoming.quickreply.QuickReply) ReceivedMessage(co.aurasphere.botmill.fb.model.incoming.callback.ReceivedMessage) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)

Example 14 with MessageEnvelope

use of co.aurasphere.botmill.fb.model.incoming.MessageEnvelope in project fb-botmill by BotMill.

the class AnnotatedTemplatedBehaviourTest method initialGreeting.

/**
	 * Initial greeting.
	 */
@FbBotMillController(eventType = FbBotMillEventType.MESSAGE_PATTERN, pattern = "(?i:hi)|(?i:hello)|(?i:hey)|(?i:good day)|(?i:home)")
public void initialGreeting(MessageEnvelope envelope) {
    addReply(new AutoReply() {

        @Override
        public FbBotMillResponse createResponse(MessageEnvelope envelope) {
            return ReplyFactory.addTypingAction(TypingAction.TYPING_ON).build(envelope);
        }
    });
    addReply(new AutoReply() {

        @Override
        public FbBotMillResponse createResponse(MessageEnvelope envelope) {
            String greetingMessage = "Hey There! ";
            return ReplyFactory.addTextMessageOnly(greetingMessage).build(envelope);
        }
    });
    addReply(new AutoReply() {

        @Override
        public FbBotMillResponse createResponse(MessageEnvelope envelope) {
            String greetingMessage = "Hey There Again! ";
            return ReplyFactory.addTextMessageOnly(greetingMessage).build(envelope);
        }
    });
    executeReplies();
}
Also used : MessageAutoReply(co.aurasphere.botmill.fb.autoreply.MessageAutoReply) AutoReply(co.aurasphere.botmill.fb.autoreply.AutoReply) FbBotMillResponse(co.aurasphere.botmill.fb.model.outcoming.FbBotMillResponse) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) FbBotMillController(co.aurasphere.botmill.fb.model.annotation.FbBotMillController)

Example 15 with MessageEnvelope

use of co.aurasphere.botmill.fb.model.incoming.MessageEnvelope in project fb-botmill by BotMill.

the class AnnotatedTemplateTest method main.

public static void main(String[] args) {
    StandardPBEStringEncryptor enc = new StandardPBEStringEncryptor();
    // can be sourced out
    enc.setPassword("password");
    ConfigurationUtils.loadEncryptedConfigurationFile(enc, "botmill.properties");
    List<BotDefinition> botDef = new ArrayList<BotDefinition>();
    botDef.add(new AnnotatedTemplatedBehaviourTest());
    ConfigurationUtils.loadBotConfig();
    ConfigurationUtils.setBotDefinitionInstance(botDef);
    for (int i = 0; i < 10; i++) {
        new Thread(new Runnable() {

            String json = "{\"sender\":{\"id\":\"1158621824216736\"},\"recipient\":{\"id\":\"1226565047419159\"},\"timestamp\":1490832021661,\"message\":{\"mid\":\"mid.$cAAUPCFn4ymdhTcignVbHH3rzpKd_\",\"seq\":844819,\"text\":\"Hi!\"}}";

            MessageEnvelope envelope = FbBotMillJsonUtils.fromJson(json, MessageEnvelope.class);

            @Override
            public void run() {
                try {
                    IncomingToOutgoingMessageHandler.getInstance().process(envelope);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
Also used : BotDefinition(co.aurasphere.botmill.core.BotDefinition) StandardPBEStringEncryptor(org.jasypt.encryption.pbe.StandardPBEStringEncryptor) ArrayList(java.util.ArrayList) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) AnnotatedTemplatedBehaviourTest(co.aurasphere.botmill.fb.test.autoreply.template.AnnotatedTemplatedBehaviourTest)

Aggregations

MessageEnvelope (co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)18 AnnotatedTemplatedBehaviourTest (co.aurasphere.botmill.fb.test.autoreply.template.AnnotatedTemplatedBehaviourTest)8 Test (org.junit.Test)8 AutoReply (co.aurasphere.botmill.fb.autoreply.AutoReply)3 FbBotMillController (co.aurasphere.botmill.fb.model.annotation.FbBotMillController)3 User (co.aurasphere.botmill.fb.model.base.User)3 FbBotMillResponse (co.aurasphere.botmill.fb.model.outcoming.FbBotMillResponse)3 BotDefinition (co.aurasphere.botmill.core.BotDefinition)2 MessageAutoReply (co.aurasphere.botmill.fb.autoreply.MessageAutoReply)2 MessengerCallback (co.aurasphere.botmill.fb.model.incoming.MessengerCallback)2 ReceivedMessage (co.aurasphere.botmill.fb.model.incoming.callback.ReceivedMessage)2 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 MessageEvent (co.aurasphere.botmill.fb.event.message.MessageEvent)1 UploadAttachmentResponse (co.aurasphere.botmill.fb.model.api.upload.UploadAttachmentResponse)1 Attachment (co.aurasphere.botmill.fb.model.base.Attachment)1 Payload (co.aurasphere.botmill.fb.model.base.Payload)1 MessengerCallbackEntry (co.aurasphere.botmill.fb.model.incoming.MessengerCallbackEntry)1 EchoMessage (co.aurasphere.botmill.fb.model.incoming.callback.EchoMessage)1