Search in sources :

Example 1 with MessengerCallback

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

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

the class EchoCallbackTest method testEchoWithListTemplate.

/**
	 * Tests an {@link EchoMessage} containing a {@link ListTemplatePayload}.
	 */
@Test
public void testEchoWithListTemplate() {
    // Tests the echo with a List Template.
    String echoListTemplatejson = "{\"object\":\"page\",\"entry\":[{\"id\":\"1423903854504468\",\"time\":1483578465379,\"messaging\":[{\"sender\":{\"id\":\"****\"},\"recipient\":{\"id\":\"****\"},\"timestamp\":1483578464765,\"message\":{\"is_echo\":true,\"app_id\":****,\"mid\":\"mid.1483578464765:ed1fe23f35\",\"seq\":785209,\"attachments\":[{\"title\":\"Title1, Title2, Title3, Title4\",\"url\":\"https://www.facebook.com/commerce/update/\",\"type\":\"template\",\"payload\":{\"template_type\":\"list\",\"sharable\":false,\"elements\":[{\"title\":\"Title1\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}},{\"title\":\"Title2\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}},{\"title\":\"Title3\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}},{\"title\":\"Title4\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}}],\"buttons\":[{\"type\":\"postback\",\"title\":\"Just Sample\",\"payload\":\"Yes\"}]}}]}}]}]}";
    MessengerCallback callback = FbBotMillJsonUtils.fromJson(echoListTemplatejson, MessengerCallback.class);
    MessageEnvelope messageEnvelope = checkCallbackWellFormed(callback);
    EchoMessage echoMessage = super.assertInstanceOf(messageEnvelope.getMessage(), EchoMessage.class);
    // Checks that individual fields are correctly parsed.
    Assert.assertTrue(echoMessage.isEcho());
    Assert.assertEquals("****", echoMessage.getAppId());
    Assert.assertEquals("mid.1483578464765:ed1fe23f35", echoMessage.getMid());
    Assert.assertEquals("785209", echoMessage.getSeq());
    super.assertListOfSize(1, echoMessage.getAttachments());
    // Checks the attachment.
    Attachment attachment = echoMessage.getAttachments().get(0);
    Assert.assertEquals("Title1, Title2, Title3, Title4", attachment.getTitle());
    Assert.assertEquals("https://www.facebook.com/commerce/update/", attachment.getUrl());
    Assert.assertEquals(AttachmentType.TEMPLATE, attachment.getType());
    // Checks the payload.
    ListTemplatePayload payload = super.assertInstanceOf(attachment.getPayload(), ListTemplatePayload.class);
    Assert.assertEquals(PayloadType.LIST, payload.getTemplateType());
    super.assertListOfSize(4, payload.getElements());
    for (int i = 0; i < 4; i++) {
        checkListTemplateElements(payload.getElements().get(i), i + 1);
    }
    // Checks the buttons.
    super.assertListOfSize(1, payload.getButtons());
    checkPostbackButtonWellFormed(payload.getButtons().get(0), "Just Sample", "Yes");
}
Also used : MessengerCallback(co.aurasphere.botmill.fb.model.incoming.MessengerCallback) ListTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ListTemplatePayload) Attachment(co.aurasphere.botmill.fb.model.base.Attachment) EchoMessage(co.aurasphere.botmill.fb.model.incoming.callback.EchoMessage) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) Test(org.junit.Test)

Aggregations

MessageEnvelope (co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)2 MessengerCallback (co.aurasphere.botmill.fb.model.incoming.MessengerCallback)2 Attachment (co.aurasphere.botmill.fb.model.base.Attachment)1 MessengerCallbackEntry (co.aurasphere.botmill.fb.model.incoming.MessengerCallbackEntry)1 EchoMessage (co.aurasphere.botmill.fb.model.incoming.callback.EchoMessage)1 ListTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.ListTemplatePayload)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 Test (org.junit.Test)1