<commit_before>var request = require.safe('request'),
shim = require.once('../shim.js'),
WebSocket = require.safe('ws'),
async = require.safe('async'),
sleep = require.safe('sleep'),
platform = null,
sockets = [],
eventReceivedCallback = null,
numSocketsToShutDown = 0,
shuttingDown = false,
timeouts = [],
/**
* Wait time between ping and pong response, in milliseconds.
*/
defaultPingResponseTimeout = 30000,
messageQueue = {},
inTransaction = {},
waitingForFirstTransaction = {},

sendMessage = function(message, thread) {
	var teamInfo = getChannelIdAndTeamId(thread);

	if (teamInfo.token != null) {
		var body = {
			"token": teamInfo.token,
			"channel": teamInfo.channel_id,
			"username": exports.config.name,
			"link_names": 1,
			"text": message,
			"unfurl_links": true,
			"icon_url": exports.config.icon
		};
		addMessageToQueue(teamInfo.team_id, body, 'https://slack.com/api/chat.postMessage');
	}
	else {
		console.debug('slack-> No slack team found!!!');
	}
},

addMessageToQueue = (teamId, body, uri, callback) => {
	waitingForFirstTransaction[teamId] = false;
	if (!messageQueue[teamId]) {
		messageQueue[teamId] = [];
	}
	messageQueue[teamId].push({"body": body, "uri": uri, "callback": callback});
	sendMessageWebAPI(teamId);
},

sendMessageWebAPI = teamId => {
	if (!inTransaction[teamId]) {
		inTransaction[teamId] = false;
	}
	if (messageQueue[teamId].length >= 1 && !inTransaction[teamId]) {
		inTransaction[teamId] = true;
		let message = messageQueue[teamId].shift();
		request({
			"uri": message.uri,
			"method": 'GET',
			"qs": message.body
		},
		(error, response, body) => {
			body = JSON.parse(body);
			if (response.statusCode != 200) {
				console.debug('slack-> error: ' + response.statusCode);
				message.callback(true, null);
			}
			else if (!body.ok) {
				console.debug('slack-> Failed to send message, error: ' + body.error);
				message.callback(true, null);
			}
			else {
				if (message.callback) {
					message.callback(false, body);
				}
			}
			inTransaction[teamId] = false;
			sendMessageWebAPI(teamId);
		});
	}
}

getChannelIdAndTeamId = function(thread) {
	var slackTeams = exports.config.slack_teams,<commit_msg>fix slack message queue<commit_after>var request = require.safe('request'),
shim = require.once('../shim.js'),
WebSocket = require.safe('ws'),
async = require.safe('async'),
sleep = require.safe('sleep'),
platform = null,
sockets = [],
eventReceivedCallback = null,
numSocketsToShutDown = 0,
shuttingDown = false,
timeouts = [],
/**
* Wait time between ping and pong response, in milliseconds.
*/
defaultPingResponseTimeout = 30000,
messageQueue = {},
inTransaction = {},
waitingForFirstTransaction = {},

sendMessage = function(message, thread) {
	var teamInfo = getChannelIdAndTeamId(thread);

	if (teamInfo.token != null) {
		var body = {
			"token": teamInfo.token,
			"channel": teamInfo.channel_id,
			"username": exports.config.name,
			"link_names": 1,
			"text": message,
			"unfurl_links": true,
			"icon_url": exports.config.icon
		};
		addMessageToQueue(teamInfo.team_id, body, 'https://slack.com/api/chat.postMessage');
	}
	else {
		console.debug('slack-> No slack team found!!!');
	}
},

addMessageToQueue = (teamId, body, uri, callback) => {
	waitingForFirstTransaction[teamId] = false;
	if (!messageQueue[teamId]) {
		messageQueue[teamId] = [];
	}
	messageQueue[teamId].push({"body": body, "uri": uri, "callback": callback});
	sendMessageWebAPI(teamId);
},

sendMessageWebAPI = function (teamId) {
	// Initialise for team
	if (inTransaction[teamId] === null || inTransaction[teamId] === undefined) {
		inTransaction[teamId] = false;
    }

	if (messageQueue[teamId].length >= 1 && !inTransaction[teamId]) {
	    inTransaction[teamId] = true;
	    var message = messageQueue[teamId].shift();
	    request({
	            "uri": message.uri,
	            "method": 'GET',
	            "qs": message.body
	        },
	        function (error, response, body) {
	            body = JSON.parse(body);
	            if (error) {
	                console.debug('slack-> error: ' + error);
	                message.callback(true, null);
	            } else if (!body.ok) {
	                console.debug('slack-> Failed to send message, error: ' + body.error);
	                message.callback(true, null);
	            } else if (message.callback) {
	                message.callback(false, body);
	            }
            inTransaction[teamId] = false;
            // Wait until message is sent before trying to send another message.
            sendMessageWebAPI(teamId);
	        });
	}
}

getChannelIdAndTeamId = function(thread) {
	var slackTeams = exports.config.slack_teams,
{'fraction_non_alphanumeric': 0.15612266781042247, 'fraction_numerical': 0.004074630066480806, 'mean_word_length': 3.5546875, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '1263530', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1477, 'n_words': 312}