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);
}
Aggregations