Search in sources :

Example 1 with MessageEnvelope

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

the class FbBotMillServlet method doPost.

/**
	 * Specifies how to handle a POST request. It parses the request as a
	 * {@link MessengerCallback} object. If the request is not a
	 * MessengerCallback, then the FbBotMillServlet logs an error and does
	 * nothing, otherwise it will forward the request to all registered bots in
	 * order to let them process the callbacks.
	 *
	 * @param req
	 *            the req
	 * @param resp
	 *            the resp
	 * @throws ServletException
	 *             the servlet exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    logger.trace("POST received!");
    MessengerCallback callback = new MessengerCallback();
    // Extrapolates and logs the JSON for debugging.
    String json = readerToString(req.getReader());
    logger.debug("JSON input: " + json);
    // Parses the request as a MessengerCallback.
    try {
        callback = FbBotMillJsonUtils.fromJson(json, MessengerCallback.class);
    } catch (Exception e) {
        logger.error("Error during MessengerCallback parsing: ", e);
        return;
    }
    // envelope of all the callbacks received to the registered bots.
    if (callback != null) {
        List<MessengerCallbackEntry> callbackEntries = callback.getEntry();
        if (callbackEntries != null) {
            for (MessengerCallbackEntry entry : callbackEntries) {
                List<MessageEnvelope> envelopes = entry.getMessaging();
                if (envelopes != null) {
                    MessageEnvelope lastEnvelope = envelopes.get(envelopes.size() - 1);
                    IncomingToOutgoingMessageHandler.getInstance().process(lastEnvelope);
                }
            }
        }
    }
    // Always set to ok.
    resp.setStatus(HttpServletResponse.SC_OK);
}
Also used : MessengerCallback(co.aurasphere.botmill.fb.model.incoming.MessengerCallback) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) MessengerCallbackEntry(co.aurasphere.botmill.fb.model.incoming.MessengerCallbackEntry)

Example 2 with MessageEnvelope

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

the class AnnotatedTemplatedBehaviourTest method catchTextAndReplyWithImage.

@FbBotMillController(eventType = FbBotMillEventType.MESSAGE, text = "Hi with Image!", caseSensitive = true)
public void catchTextAndReplyWithImage(MessageEnvelope envelope) {
    //		
    UploadAttachmentResponse response = UploadApi.uploadAttachment(AttachmentType.IMAGE, "http://vignette2.wikia.nocookie.net/nickelodeon/images/2/27/Spongebob_PNG.png/revision/latest?cb=20120702055752");
    String attachmentId = response.getAttachmentId();
    reply(new AutoReply() {

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

Example 3 with MessageEnvelope

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

the class BuyButtonTest method defineBehaviour.

/*
	 * (non-Javadoc)
	 * 
	 * @see co.aurasphere.botmill.fb.FbBotDefinition#defineBehavior()
	 */
public void defineBehaviour() {
    // loads the annotated encryption class.
    ConfigurationUtils.loadEncryptedConfigurationProperties();
    // loads the annotated bot.
    ConfigurationUtils.loadBotDefinitions();
    addActionFrame(new MessageEvent(MESSAGE_TO_SEND), new AutoReply() {

        @Override
        public FbBotMillResponse createResponse(MessageEnvelope envelope) {
            return ReplyFactory.addGenericTemplate().addElement("A simple Button Template with a Buy Button").addButton(ButtonFactory.createBuyButton("buy_button_payload").setPaymentSummary("USD", PaymentType.FIXED_AMOUNT, "BotMill.io").addPriceLabel("A price label", "2").setTestPayment(true).addRequestedUserInfo(RequestedUserInfo.CONTACT_PHONE).build()).endElement().build(envelope);
        }
    });
}
Also used : AutoReply(co.aurasphere.botmill.fb.autoreply.AutoReply) FbBotMillResponse(co.aurasphere.botmill.fb.model.outcoming.FbBotMillResponse) MessageEvent(co.aurasphere.botmill.fb.event.message.MessageEvent) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)

Example 4 with MessageEnvelope

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

the class TestDeserialization method main.

/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
public static void main(String[] args) {
    String json = "{\"sender\":{\"id\":\"USER_ID\"},\"recipient\":{\"id\":\"PAGE_ID\"},\"timestamp\":1458692752478,\"message\":{\"mid\":\"mid.1458696618141:b4ef9d19ec21086067\",\"seq\":51,\"attachments\":[{\"type\":\"image\",\"payload\":{\"url\":\"IMAGE_URL\"}}]}}";
    String json2 = "{\"sender\":{\"id\":\"1152902104831488\"},\"recipient\":{\"id\":\"1209810882431614\"},\"timestamp\":1482953812997,\"message\":{\"mid\":\"mid.1482953812997:6c6e910961\",\"seq\":778909,\"attachments\":[{\"title\":\"Alvin's Location\",\"url\":\"https:\\/\\/www.facebook.com\\/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3D43.899746%252C%2B-79.269825%26FORM%3DFBKPL1%26mkt%3Den-US&h=ATNzUb8qaIa5x61TRftUfE7RxWJE_E-5XeshoVeDOeGprLWnutTFxlENgC7vFhYuD3pB02N6PpzsEgcOiofJShlqT7a-lesSHScIzrLQVGPg-_22e6CETCbNhXeo57KTKsXLQU8&s=1&enc=AZNukLEVHCKFse_g9I7aBKSSgHbB3odYCw_9Do4rhrUXbJvjz_okfOP0gGwjSJ9Cd5PRpkxhT9kiCBrGBbXQCqPejz98NXzD6V6WVIEB4ZaDqA\",\"type\":\"location\",\"payload\":{\"coordinates\":{\"lat\":43.899746,\"long\":-79.269825}}}]}}";
    MessageEnvelope envelope = FbBotMillJsonUtils.fromJson(json, MessageEnvelope.class);
    Payload payload = envelope.getMessage().getAttachments().get(0).getPayload();
    UrlPayload actualPayload = (UrlPayload) payload;
    System.out.println(actualPayload.getUrl());
}
Also used : UrlPayload(co.aurasphere.botmill.fb.model.outcoming.payload.UrlPayload) Payload(co.aurasphere.botmill.fb.model.base.Payload) UrlPayload(co.aurasphere.botmill.fb.model.outcoming.payload.UrlPayload) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)

Example 5 with MessageEnvelope

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

the class AnnotatedTemplateTest method testAnnotatedQuickReplyTemplateBot.

@Test
public void testAnnotatedQuickReplyTemplateBot() {
    String json = "{\"sender\":{\"id\":\"1158621824216736\"},\"recipient\":{\"id\":\"1226565047419159\"},\"timestamp\":1490832021661,\"message\":{\"mid\":\"mid.$cAAUPCFn4ymdhTcignVbHH3rzpKd_\",\"seq\":844819,\"text\":\"quick replies\"}}";
    MessageEnvelope envelope = FbBotMillJsonUtils.fromJson(json, MessageEnvelope.class);
    IncomingToOutgoingMessageHandler.getInstance().process(envelope);
    assertNotNull(envelope);
}
Also used : MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) Test(org.junit.Test) 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