<commit_before>var express = require('express'),
bodyParser  = require("body-parser"),
request = require('request'),
shim = require.once("../shim.js"),
app = null,
server = null,
platform = null,

sendMessage = function(message, thread) {
	var slackTeams = exports.config.slack_teams,
	thread_components = thread.split('~', 2),
	thread_id = thread_components[0],
	thread_team_id = thread_components[1],
	slack_token = slackTeams[thread_team_id].slack_token;

	if (slack_token != null) {
		var body = {
			"token": slack_token,
			"channel": thread_id,
			"username": exports.config.name,
			"link_names": 1,
			"text": message,
			"unfurl_links": true,
			"icon_url": exports.config.icon
		};

		request({
			"uri": 'https://slack.com/api/chat.postMessage',
			"method": 'GET',
			"qs": body
		},
		function (error, response, body) {
			if (response.statusCode != 200) {
				console.log('error: ' + response.statusCode);
			}
		});
	}
	else {
		console.log('No slack team found!!!');
	}
},

sendFile = function(type, file, description, thread) {
	var slackTeams = exports.config.slack_teams,
	thread_components = thread.split('~', 2),
	thread_id = thread_components[0],
	thread_team_id = thread_components[1],
	slack_token = slackTeams[thread_team_id].slack_token;

	if (slack_token != null) {
		var body = {
			"token": slack_token,
			"channel": thread_id,
			"file": file,
			"filetype": type,
			"title": description
		};

		request({
			"uri": 'https://slack.com/api/files.upload',
			"method": 'GET',
			"qs": body
		},
		function (error, response, body) {
			if (response.statusCode != 200) {
				console.log('error: ' + response.statusCode);
			}
		});
	}
	else {
		console.log('No slack team found!!!');
	}
},

renameChannel = function(title, thread) {
	var slackTeams = exports.config.slack_teams,
	thread_components = thread.split('~', 2),
	thread_id = thread_components[0],
	thread_team_id = thread_components[1],
	slack_token = slackTeams[thread_team_id].slack_token;

	if (slack_token != null) {
		var body = {
			"token": slack_token,
			"channel": thread_id,
			"name": title
		};

		request({
			"uri": 'https://slack.com/api/channels.rename',
			"method": 'GET',
			"qs": body
		},
		function (error, response, body) {
			if (response.statusCode != 200) {
				console.log('error: ' + response.statusCode);
			}
		});
	}
	else {
		console.log('No slack team found!!!');
	}
},

getUsers = function(slackTeam) {
	var body = {"token": slackTeam.slack_token},
	userMap = [];
	request({
		"uri": 'https://slack.com/api/users.list',
		"method": 'GET',
		"qs": body
	},
	function (error, response, body) {
		body = JSON.parse(body);
		if (response.statusCode != 200 || body.ok == false) {
			console.log('failed to get list of users');
		}
		else {
			for (var i = 0; i < body.members.length; i++) {
				userMap[body.members[i].id] = body.members[i].name
			}
			slackTeam.users = userMap;
		}
	});
},

lookUpUserAddToTeam = function(slackTeam, userId, match, message, callback) {
	var body = {"token": slackTeam.slack_token, "user": userId},
	userMap = [];
	request({
		"uri": 'https://slack.com/api/users.info',
		"method": 'GET',
		"qs": body
	},
	function (error, response, body) {
		body = JSON.parse(body);
		if (response.statusCode != 200) {
			console.log('error: ' + response.statusCode + '\n' + error);
			return message
		}
		else {
			slackTeam.users[body.user.id] = body.user.name;
			return callback(body.user.name, message, match);
		}
	});
},

replaceUserIdWithUserName = function(userName, message, match) {
	if (userName) {
		var index = message.indexOf(match);
		return message.substr(0, index) + userName + message.substr(index + match.length);
	}
};



exports.start = function (callback) {
	var slackTeams = exports.config.slack_teams,
	slack_token = null;
	app = express();
	app.use(bodyParser.json());
	app.use(bodyParser.urlencoded({ extended: true }));

	platform = shim.createPlatformModule({
		commandPrefix: exports.config.commandPrefix,
		sendMessage: sendMessage,
		sendFile: sendFile,
		setTitle: renameChannel
	});

	for (teamId in slackTeams) {
		getUsers(slackTeams[teamId]);
	}

	app.post('/', function (req, res) {
		var data = req.body,
		event = [],
		message = data.text.trim(),
		shimMessage = null;

		if (data.user_name != 'slackbot') {
			// Check that the message sent contains a identifier
			// TODO: need to check that regex is correct?
			var matches = message.match(/<?@[^:>]+>:?/g);
			if (matches != null) {
				var slackTeams = exports.config.slack_teams,
				userName,
				id,
				slackTeam = slackTeams[data.team_id];

				if (slackTeam) {

					for (var j = 0; j < matches.length; j++) {
						// replace identifier with empty string
						id = matches[j].replace(/[ :<>@]+/g, '');

						// find the user<commit_msg>hotfix old slack integration in master<commit_after>var express = require('express'),
bodyParser  = require("body-parser"),
request = require('request'),
shim = require.once("../shim.js"),
app = null,
server = null,
platform = null,

sendMessage = function(message, thread) {
	var slackTeams = exports.config.slack_teams,
	thread_components = thread.split('~', 2),
	thread_id = thread_components[0],
	thread_team_id = thread_components[1],
	slack_token = slackTeams[thread_team_id].slack_token;

	if (slack_token != null) {
		var body = {
			"token": slack_token,
			"channel": thread_id,
			"username": exports.config.name,
			"link_names": 1,
			"text": message,
			"unfurl_links": true,
			"icon_url": exports.config.icon
		};

		request({
			"uri": 'https://slack.com/api/chat.postMessage',
			"method": 'GET',
			"qs": body
		},
		function (error, response, body) {
			if (response.statusCode != 200) {
				console.log('error: ' + response.statusCode);
			}
		});
	}
	else {
		console.log('No slack team found!!!');
	}
},

sendFile = function(type, file, description, thread) {
	var slackTeams = exports.config.slack_teams,
	thread_components = thread.split('~', 2),
	thread_id = thread_components[0],
	thread_team_id = thread_components[1],
	slack_token = slackTeams[thread_team_id].slack_token;

	if (slack_token != null) {
		var body = {
			"token": slack_token,
			"channel": thread_id,
			"file": file,
			"filetype": type,
			"title": description
		};

		request({
			"uri": 'https://slack.com/api/files.upload',
			"method": 'GET',
			"qs": body
		},
		function (error, response, body) {
			if (response.statusCode != 200) {
				console.log('error: ' + response.statusCode);
			}
		});
	}
	else {
		console.log('No slack team found!!!');
	}
},

renameChannel = function(title, thread) {
	var slackTeams = exports.config.slack_teams,
	thread_components = thread.split('~', 2),
	thread_id = thread_components[0],
	thread_team_id = thread_components[1],
	slack_token = slackTeams[thread_team_id].slack_token;

	if (slack_token != null) {
		var body = {
			"token": slack_token,
			"channel": thread_id,
			"name": title
		};

		request({
			"uri": 'https://slack.com/api/channels.rename',
			"method": 'GET',
			"qs": body
		},
		function (error, response, body) {
			if (response.statusCode != 200) {
				console.log('error: ' + response.statusCode);
			}
		});
	}
	else {
		console.log('No slack team found!!!');
	}
},

getUsers = function(slackTeam) {
	var body = {"token": slackTeam.slack_token},
	userMap = [];
	request({
		"uri": 'https://slack.com/api/users.list',
		"method": 'GET',
		"qs": body
	},
	function (error, response, body) {
		body = JSON.parse(body);
		if (response.statusCode != 200 || body.ok == false) {
			console.log('failed to get list of users');
		}
		else {
			for (var i = 0; i < body.members.length; i++) {
				userMap[body.members[i].id] = body.members[i].name
			}
			slackTeam.users = userMap;
		}
	});
},

lookUpUserAddToTeam = function(slackTeam, userId, match, message, callback) {
	var body = {"token": slackTeam.slack_token, "user": userId},
	userMap = [];
	request({
		"uri": 'https://slack.com/api/users.info',
		"method": 'GET',
		"qs": body
	},
	function (error, response, body) {
		body = JSON.parse(body);
		if (response.statusCode != 200) {
			console.log('error: ' + response.statusCode + '\n' + error);
			return message
		}
		else {
			slackTeam.users[body.user.id] = body.user.name;
			return callback(body.user.name, message, match);
		}
	});
},

replaceUserIdWithUserName = function(userName, message, match) {
	if (userName) {
		var index = message.indexOf(match);
		return message.substr(0, index) + userName + message.substr(index + match.length);
	}
};



exports.start = function (callback) {
	var slackTeams = exports.config.slack_teams,
	slack_token = null;
	app = express();
	app.use(bodyParser.json());
	app.use(bodyParser.urlencoded({ extended: true }));

	platform = shim.createPlatformModule({
		commandPrefix: exports.config.commandPrefix,
		sendMessage: sendMessage,
		sendFile: sendFile,
		setTitle: renameChannel
	});

	for (var i = 0; i < slackTeams.length; i++) {
		getUsers(slackTeams[i]);
	}

	app.post('/', function (req, res) {
		var data = JSON.parse(req.body),
		event = [],
		message = data.text.trim(),
		shimMessage = null;

		if (data.user_name != 'slackbot') {
			// Check that the message sent contains a identifier
			// TODO: need to check that regex is correct?
			var matches = message.match(/<?@[^:>]+>:?/g);
			if (matches != null) {
				var slackTeams = exports.config.slack_teams,
				userName,
				id,
				slackTeam = slackTeams[data.team_id];

				if (slackTeam) {

					for (var j = 0; j < matches.length; j++) {
						// replace identifier with empty string
						id = matches[j].replace(/[ :<>@]+/g, '');

						// find the user
{'fraction_non_alphanumeric': 0.17098283931357255, 'fraction_numerical': 0.00592823712948518, 'mean_word_length': 3.7299557304476143, 'pattern_counts': {'":': 66, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '617081', 'n_tokens_mistral': 3862, 'n_tokens_neox': 3403, 'n_words': 733}