This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
/
main.js
89 lines (69 loc) · 1.86 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const TelegramBot = require( 'node-telegram-bot-api' );
const cmds = require( './comandos' );
const config = require( './config.json' );
const bot = new TelegramBot( config.tokenBot, {polling: true});
const agruparLinhas = ( lista, linhas = 10 ) => {
let retorno = [];
let grupo = [];
for ( let x = 0; x < lista.length; x++ ) {
grupo.push( lista[x] );
if ( grupo.length >= linhas || x+1 === lista.length ) {
retorno.push( grupo.join( '\n' ) );
grupo = [];
}
}
return retorno;
}
const envio = ( chat, obj ) => {
switch ( typeof obj ) {
case 'string':
envioString( chat, obj );
break;
case 'object':
if ( obj.join ) {
envioLista( chat, agruparLinhas( obj, 10 ) );
break;
}
default:
console.log( ' *** Não entendi o que devo enviar! *** ' );
console.log( obj );
}
};
const envioString = ( chat, texto ) => bot.sendMessage( chat, texto );
const envioLista = ( id, lista, idx ) => {
if ( !idx ) {
idx = 0;
}
if ( idx >= lista.length ) {
return;
}
envioString( id, lista[idx] );
setTimeout( () => envioLista( id, lista, ++idx ), 1000 );
};
bot.on( 'message', ( msg ) => {
console.log( msg.message_id, ( msg.chat.type === 'private' ? 'PVT' : 'GRP' ), msg.chat.id, msg.from.username, msg.text );
if ( !msg.text ) {
return;
}
const parametros = msg.text.split( ' ' );
const comando = parametros.shift().substring( 1 ).split( '@' )[0];
if ( typeof cmds[comando] === 'undefined' || cmds[comando].exec === 'undefined' ) {
let resposta = `Oi @${msg.from.first_name}\nO comando digitado não foi reconhecido\n\nTente:\n`;
for ( key in cmds ) {
if ( cmds[key] && !cmds[key].ocultar ) {
resposta += `/${key}\n`
}
}
envio( msg.chat.id, resposta );
}
else {
cmds[comando].exec({
bot,
config,
comando,
parametros,
original: msg,
callback: ( resp ) => envio( msg.chat.id, resp )
});
}
});