<commit_before>const http = require("http");
const xmlParser = new require("xml2js").Parser();
const config = require("../configuration/Configuration");
const logger = require("../logger/Logger");

//Regex
var imageRegex = /<img src="(.*?)"/i;
var hoverRegex = /title="(.*?)"/i;

module.exports.CommandName = "xkcd";

module.exports.HandleRequest = function(request, response, next){
  logger.LogInfoData("Received post for command 'urban'", request.params);
  if(request.params.token == config.data.xkcdSlackToken) {
    GetXKCDRss(function(error, comic) {
      if(error) {
        response.json(200, FormatSlackMessage(false, error));
      }
      else {
        response.json(200, FormatSlackAttachmentMessage(true, comic));
      }
    });
  }
  return next();
};

function GetXKCDRss(callback) {
  //get the latest xkcd comic
  http.get({
    host: "xkcd.com",
    path: "/rss.xml"
  }, function(response) {
    let data = "";

    response.on("data", function(chunk){
      data += chunk;
    });

    response.on("end", function(){
      let obj = xmlParser.parseString(data, function(error, parsed) {
        if(error) {
          callback(error, undefined);
        }
        else {
          try {
            let latestComic = parsed.rss.channel[0].item[0];
            callback(undefined, {
              title: latestComic.title[0],
              hoverTitle: hoverRegex.exec(latestComic.description[0])[1],
              link: latestComic.link[0],
              image: imageRegex.exec(latestComic.description[0])[1]
            });
          }
          catch(error) {
            callback(error, undefined);
          }
        }
      });
    });

    response.on("error", function(error) {
      callback(error, undefined);
    });
  });
}

function FormatSlackAttachmentMessage(sendToChannel, jsonData) {
  return {
    response_type: sendToChannel ? "in_channel" : "ephemeral",
    text: undefined,
    attachments: [{
      title: jsonData.title,
      title_link: jsonData.link,
      text: jsonData.image
    },
    {
      text: jsonData.hoverTitle
    }]
  }
}
<commit_msg>-image is now using the "image_url" parameter when responding to slack.<commit_after>const http = require("http");
const xmlParser = new require("xml2js").Parser();
const config = require("../configuration/Configuration");
const logger = require("../logger/Logger");

//Regex
var imageRegex = /<img src="(.*?)"/i;
var hoverRegex = /title="(.*?)"/i;

module.exports.CommandName = "xkcd";

module.exports.HandleRequest = function(request, response, next){
  logger.LogInfoData("Received post for command 'urban'", request.params);
  if(request.params.token == config.data.xkcdSlackToken) {
    GetXKCDRss(function(error, comic) {
      if(error) {
        response.json(200, FormatSlackMessage(false, error));
      }
      else {
        response.json(200, FormatSlackAttachmentMessage(true, comic));
      }
    });
  }
  return next();
};

function GetXKCDRss(callback) {
  //get the latest xkcd comic
  http.get({
    host: "xkcd.com",
    path: "/rss.xml"
  }, function(response) {
    let data = "";

    response.on("data", function(chunk){
      data += chunk;
    });

    response.on("end", function(){
      let obj = xmlParser.parseString(data, function(error, parsed) {
        if(error) {
          callback(error, undefined);
        }
        else {
          try {
            let latestComic = parsed.rss.channel[0].item[0];
            callback(undefined, {
              title: latestComic.title[0],
              hoverTitle: `Hover text: ${hoverRegex.exec(latestComic.description[0])[1]}`,
              link: latestComic.link[0],
              image: imageRegex.exec(latestComic.description[0])[1]
            });
          }
          catch(error) {
            callback(error, undefined);
          }
        }
      });
    });

    response.on("error", function(error) {
      callback(error, undefined);
    });
  });
}

function FormatSlackAttachmentMessage(sendToChannel, jsonData) {
  return {
    response_type: sendToChannel ? "in_channel" : "ephemeral",
    text: undefined,
    attachments: [{
      title: jsonData.title,
      title_link: jsonData.link,
      image_url: jsonData.image
    },
    {
      text: jsonData.hoverTitle
    }]
  }
}

{'fraction_non_alphanumeric': 0.14967259120673526, 'fraction_numerical': 0.007015902712815715, 'mean_word_length': 2.567139282735613, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6510974', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1307, 'n_words': 244}