const express     = require('express');
const bodyParser  = require('body-parser');
const app         = express();
const TelegramBot = require('node-telegram-bot-api');
const CONF        = process.env;
const PORT        = CONF.PORT || 5000;

app.use(bodyParser.json());

console.log(process.env);

const server = app.listen(PORT, "0.0.0.0", function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('Web server started at http://%s:%s', host, port);
});

app.post('/bot', function(req, res) {
	bot.processUpdate(req.body);
	res.sendStatus(200);
});

const bot = new TelegramBot(CONF.TOKEN);
bot.setWebHook(process.env.HEROKU_URL + 'bot');

bot.on('message', (msg) => {
	const chatId = msg.chat.id;

	console.log('new', msg);

	if (msg.text) {
		if (msg.text.match(/‼️/i)) {
			bot.sendVideo(chatId, 'http://i.giphy.com/ntts0sjEqNaRG.gif', {reply_to_message_id: msg.message_id});
		}

		if (checkForMatch(msg.text, ['миша', 'мишка', 'darmy', 'misha'])) {
			bot.sendVideo(chatId, 'http://i.giphy.com/SWCKeoosjekyQ.gif', {reply_to_message_id: msg.message_id});
		}

		if (checkForMatch(msg.text, ['bro', 'бро'])) {
			bot.sendVideo(chatId, 'http://i.giphy.com/Tv2btKgK06tPy.gif', {reply_to_message_id: msg.message_id});
		}

		if (checkForMatch(msg.text, ['ахаха'])) {
			bot.sendVideo(chatId, 'http://i.giphy.com/tqf2j43xoEcow.gif', {reply_to_message_id: msg.message_id});
		}
	}
});

function checkForMatch(string, array) {
	for (let i = 0; i < array.length; i++) {
		if (string.search("/(.*?(\b" + array[i] + "\b)[^$]*)/i")) {
			return true;
		}
	}
}

@@ -327,17 +327,17 @@
 RT, 
-%22
+'
 0.0.0.0
-%22
+'
 , fu
@@ -1520,53 +1520,97 @@
 ring
-.search(%22/(.*?(%5Cb%22 + array%5Bi%5D + %22%5Cb)%5B%5E$%5D*)/i%22
+ === array%5Bi%5D %7C%7C new RegExp('(?:%5E%7C%5C%5Cs)' + array%5Bi%5D + '(?:%5C%5C?%7C%5C%5C!%7C%5C%5Cs)', 'gi').test(string
 )) %7B

fix regexp