Message can be in aNyCAse. We need it only in lowercase:
let stext =message.toLowerCase();
We use functions in the code. So code more simple:
// search keyword in the message (stext)functionhaveAnyKeyword(item){for(var ind initem.keywords){// exact searhingif(item.exact){// exact searchingif(stext==item.keywords[ind]){ returntrue }continue; }if(stext.indexOf(item.keywords[ind])>-1){ returntrue } }}// build answerfunctiongetAnswerFor(item){if((user.telegramid==admin_tg_id)&&(!item.answerToAdmin)){// no any answer for adminreturn; }let answer =item.answer;if(!answer){ answer ="" }if(item.url){ answer = answer +"\nhttp://"+item.url }return answer;}// just bust all keywords list functiondoSearch(){let item;let answer;for(var ind in list){ item = list[ind];if(haveAnyKeyword(item.keywords)){returngetAnswerFor(item); } }}
Functions create your code more simple. It is good for it rewriting and improvement.
Perform searching. And if we get answer - send message:
let answer =doSearch();if(answer){Bot.sendMessage(answer, {is_reply:true});}
In this command - we have only one Bot.sendMessage function!