var logger = require('log4js').getLogger('telegram');

var nconf = require('nconf');

if (process.env.NODE_ENV === 'unit-test') {
	nconf.file('bots', __dirname + '/../../test/config/bots.json')
		.file('receviers', __dirname + '/../../test/config/receivers.json')
		.file('senders', __dirname + '/../../test/config/senders.json');
} else {
	nconf.file('bots', __dirname + '/../config/bots.json')
		.file('receviers', __dirname + '/../config/receivers.json')
		.file('senders', __dirname + '/../config/senders.json');
}

var request = require('request');

function sendMessage(receiver, message, botname, callback) {

	try {
		var chat_id = nconf.get(receiver).chat_id;
		var token = nconf.get(botname).token;
	} catch (ex) {

		err = {
			description: 'chat_id or token not found'
		};

		logger.error(err);

		callback(err);

        return;
	}

	request.post({
			url: 'https://api.telegram.org/bot' + token + '/sendMessage',
			form: {
				chat_id: chat_id,
				text: message
			}
		},
		function(err, res, body) {

			body = JSON.parse(body);

			if (err) {
				logger.error(err);
				callback(err, null);
				return;
			}

			if (body['error_code']) {

				logger.error(body['description']);

				err = {
					error: body['error_code'],
					description: body['description']
				};

				callback(err, null);

				return;
			}

			logger.info('Message sent!');

			var successMessage = {
				receiver: receiver,
				message: message,
				sendTime: new Date()
			};

			callback(null, successMessage);

		});
}

module.exports = {
	'sendMessage': sendMessage
};

@@ -736,38 +736,30 @@
 %0A%09%09%09
-description: 'chat_id or token
+error:'receiver or bot
  not
@@ -764,16 +764,17 @@
 ot found
+!
 '%0A%09%09%7D;%0A%0A
@@ -805,24 +805,29 @@
 callback(err
+,null
 );%0A%0A        

Fix the error message of chat_id or token not found