-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.js
62 lines (55 loc) · 1.84 KB
/
router.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
const Utils = require('./lib/utils');
const Persistent = require('./lib/persistent');
const api = require('./lib/api')();
const Controllers = {
History: require('./controllers/history'),
News: require('./controllers/news'),
Help: require('./controllers/help'),
Search: require('./controllers/search'),
Register: require('./controllers/register'),
Inline: require('./controllers/inline'),
Callback: require('./controllers/callback')
};
/**
* Checks incoming string to be valid with route command
* @param command
* @returns {function(*)}
*/
const checkRoute = (command) => {
return (message) => {
if (!message.text) return false;
let test = message.text.match(command);
return test ? !!test.length : false;
};
};
/**
* Exports
* @param {Telegram} tg
* @param {Object} config
*/
module.exports = function(tg, config) {
Utils.setTg(tg);
Persistent.connect(config);
api.texts = tg._telegramDataSource._localization._localizations[0].phrases.errors;
tg.addScopeExtension(Persistent);
tg.addScopeExtension(Utils);
const searchController = new Controllers.Search(config, api);
const historyController = new Controllers.History(config);
historyController.delegate = searchController;
/**
* Routes
* /h private
* /start public
* /news public
* /search private-inline
*/
tg.router
.when({ name: 'History', test: checkRoute(/h$/) }, historyController)
.when([{ name: 'Help', test: checkRoute(/help(@.+)*$/) }, '/start'], new Controllers.Help(config))
.when(['/start', '/stop'], new Controllers.Register(config))
.when('/news', new Controllers.News(config))
.when('/search :request', searchController)
.otherwise(searchController)
.inlineQuery(new Controllers.Inline(config, Persistent, api))
.callbackQuery(new Controllers.Callback(config, tg._telegramDataSource._api, tg));
};