message: use middleware.receive.use to mark messages are delivered
diff: @@ -10,33 +10,6 @@ function Facebookbot(configuration) {
  // Create a core botkit bot
  var facebook_botkit = Botkit(configuration || {});
  - if (facebook_botkit.config.require_delivery) {
  -
  - facebook_botkit.on('message_delivered', function(bot, message) {
  -
  - // get list of mids in this message
  - for (var m = 0; m < message.delivery.mids.length; m++) {
  - var mid = message.delivery.mids[m];
  -
  - // loop through all active conversations this bot is having
  - // and mark messages in conversations as delivered = true
  - bot.findConversation(message, function(convo) {
  - if (convo) {
  - for (var s = 0; s < convo.sent.length; s++) {
  - if (convo.sent[s].sent_timestamp <= message.delivery.watermark ||
  - (convo.sent[s].api_response &&
  - (convo.sent[s].api_response.mid || convo.sent[s].api_response.message_id) == mid)) {
  - convo.sent[s].delivered = true;
  - }
  - }
  - }
  - });
  - }
  -
  - });
  -
  - }
  -
  // For backwards-compatability, support the receive_via_postback config option
  // this causes facebook_postback events to be replicated as message_received events
  // allowing them to be heard without subscribing to additional events
  @@ -459,6 +432,33 @@ function Facebookbot(configuration) {
  });
  + // handle delivery messages
  + facebook_botkit.middleware.receive.use(function handleDelivery(bot, message, next) {
  +
  + if (message.type === 'message_delivered' && facebook_botkit.config.require_delivery) {
  + // get list of mids in this message
  + for (var m = 0; m < message.delivery.mids.length; m++) {
  + var mid = message.delivery.mids[m];
  +
  + // loop through all active conversations this bot is having
  + // and mark messages in conversations as delivered = true
  + bot.findConversation(message, function(convo) {
  + if (convo) {
  + for (var s = 0; s < convo.sent.length; s++) {
  + if (convo.sent[s].sent_timestamp <= message.delivery.watermark ||
  + (convo.sent[s].api_response && convo.sent[s].api_response.message_id == mid)) {
  + convo.sent[s].delivered = true;
  + }
  + }
  + }
  + });
  + }
  + }
  +
  + next();
  + });
  +
  +
  facebook_botkit.on('webserver_up', function(webserver) {
  // Validate that requests come from facebook, and abort on validation errors
  