Search in sources :

Example 1 with MessengerCallbackEntry

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

Aggregations

MessageEnvelope (co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)1 MessengerCallback (co.aurasphere.botmill.fb.model.incoming.MessengerCallback)1 MessengerCallbackEntry (co.aurasphere.botmill.fb.model.incoming.MessengerCallbackEntry)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1