Help bot
In Bots.Business chat we have many generic questions. This bot can answer for thems.
Bot have only one command. This command is Master command - *.

Code description

Bot receive all messages from chat with Master command.
So we do not need any notifications about new chat members and etc:
1
if(!message){ return }
Copied!
Do you have big case for command execution? You can use return.
Bot have keywords for searching in user's messages. Bot show answer and url on keywoard in message:
1
list = [
2
{ url: "status.bots.business", keywords: [ 'status' ],
3
answer: 'Seems do you need to know uptime status?' },
4
{ keywords: [ '/start' ], answer: 'Please do not touch it here' },
5
{ keywords: [ 'php ', ' php' ], answer: 'PHP? Really? I love BJS only' },
6
{ keywords: [ 'hi!', 'hello' ], answer: 'Hey!' }
7
]
Copied!
Also admin can write anything and do not need any help. So we have key - answerToAdmin:
1
let admin_tg_id = 519829299;
2
...
3
{ url: "status.bots.business", keywords: [ 'status' ], answerToAdmin: true }
Copied!
Sometimes we need exact searhing:
1
{ url: "help.bots.business", keywords: [ 'help' ], exact:true}
Copied!
Message can be in aNyCAse. We need it only in lowercase:
1
let stext = message.toLowerCase();
Copied!
We use functions in the code. So code more simple:
1
// search keyword in the message (stext)
2
function haveAnyKeyword(item){
3
for(var ind in item.keywords){
4
// exact searhing
5
if(item.exact){
6
// exact searching
7
if(stext==item.keywords[ind]){ return true }
8
continue;
9
}
10
​
11
if(stext.indexOf(item.keywords[ind])>-1){ return true }
12
}
13
}
14
​
15
// build answer
16
function getAnswerFor(item){
17
if((user.telegramid==admin_tg_id)&&(!item.answerToAdmin)){
18
// no any answer for admin
19
return;
20
}
21
22
let answer = item.answer;
23
if(!answer){ answer = "" }
24
​
25
if(item.url){
26
answer = answer + "\nhttp://" + item.url
27
}
28
return answer;
29
}
30
​
31
// just bust all keywords list
32
function doSearch(){
33
let item;
34
let answer;
35
​
36
for(var ind in list){
37
item = list[ind];
38
if(haveAnyKeyword(item.keywords)){
39
return getAnswerFor(item);
40
}
41
}
42
}
Copied!
Functions create your code more simple. It is good for it rewriting and improvement.
Perform searching. And if we get answer - send message:
1
​
2
let answer = doSearch();
3
if(answer){
4
Bot.sendMessage(answer, {is_reply: true});
5
}
6
​
Copied!
In this command - we have only one Bot.sendMessage function!
It is good - code more simple.
Last modified 2yr ago
Copy link