trackmyd-bot/server.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-02-22 11:34:42 +01:00
/* eslint-disable consistent-return */
2019-02-19 14:21:58 +01:00
const Telebot = require('telebot');
const config = require('./config');
2019-02-19 14:57:47 +01:00
const utils = require('./utils');
2019-02-20 14:31:23 +01:00
const auth = require('./auth');
2019-02-20 17:48:26 +01:00
const api = require('./api');
2019-02-21 14:56:08 +01:00
const logger = require('./logger');
2019-02-22 17:41:59 +01:00
const templates = require('./templates');
2019-02-21 14:56:08 +01:00
logger.logConsole('Server started');
logger.logInfo('Server started');
2019-02-19 14:21:58 +01:00
const bot = new Telebot({
token: config.app.tokenBot,
usePlugins: ['askUser'],
});
bot.on('/start', (msg) => {
2019-02-20 17:48:26 +01:00
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
2019-02-22 17:41:59 +01:00
bot.sendMessage(msg.from.id, templates.welcome(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
bot.event('/help', msg);
2019-02-19 14:21:58 +01:00
});
2019-02-19 14:29:57 +01:00
bot.on('/help', (msg) => {
2019-02-20 17:48:26 +01:00
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
2019-02-22 17:41:59 +01:00
bot.sendMessage(msg.from.id, templates.help(), { parseMode: 'Markdown' });
2019-02-19 14:29:57 +01:00
});
bot.on('/list', (msg) => {
2019-02-20 17:48:26 +01:00
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
api.getDevices()
2019-02-22 10:05:24 +01:00
.then((res) => {
2019-02-22 17:41:59 +01:00
res.forEach((elem) => {
bot.sendMessage(msg.from.id, templates.deviceList(elem), { parseMode: 'Markdown' });
});
2019-02-20 17:48:26 +01:00
})
.catch((err) => {
2019-02-22 17:41:59 +01:00
utils.sendErrorMessage(err, msg);
2019-02-20 17:48:26 +01:00
});
2019-02-19 14:29:57 +01:00
});
bot.on('/position', (msg) => {
2019-02-20 17:48:26 +01:00
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
api.getDevices()
2019-02-22 10:05:24 +01:00
.then((res) => {
2019-02-22 17:41:59 +01:00
const devices = [];
res.forEach((device) => {
devices.push(device.name);
});
const replyMarkup = bot.keyboard([devices], { resize: true, once: true });
bot.sendMessage(msg.from.id, 'Select device', { ask: 'devicePosition', replyMarkup });
2019-02-20 17:48:26 +01:00
})
.catch((err) => {
2019-02-22 17:41:59 +01:00
utils.sendErrorMessage(err, msg);
2019-02-20 17:48:26 +01:00
});
});
bot.on('ask.devicePosition', (msg) => {
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
api.getInfoDevice(msg.text)
2019-02-21 14:56:08 +01:00
.then((res) => {
2019-02-22 17:41:59 +01:00
bot.sendLocation(msg.from.id, [res.position.latitude, res.position.longtitude]);
bot.sendMessage(msg.from.id, templates.position(res), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
})
.catch((err) => {
2019-02-22 17:41:59 +01:00
utils.sendErrorMessage(err, msg);
2019-02-20 17:48:26 +01:00
});
2019-02-19 14:29:57 +01:00
});
bot.on('/add', (msg) => {
2019-02-20 17:48:26 +01:00
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
bot.sendMessage(msg.from.id, 'insert name', { ask: 'addDevice' });
});
bot.on('ask.addDevice', (msg) => {
if (!auth(msg.from.id)) {
2019-02-22 17:41:59 +01:00
return bot.sendMessage(msg.from.id, templates.unauthorizedUser(), { parseMode: 'Markdown' });
2019-02-20 17:48:26 +01:00
}
2019-02-22 17:41:59 +01:00
2019-02-20 17:48:26 +01:00
api.addDevice(msg.text)
2019-02-21 14:56:08 +01:00
.then((res) => {
2019-02-22 17:41:59 +01:00
bot.sendMessage(msg.from.id, templates.addDeviceURL(res));
bot.sendMessage(msg.from.id, templates.addDeviceHeader());
bot.sendMessage(msg.from.id, templates.addDeviceBody());
2019-02-21 14:56:08 +01:00
})
2019-02-22 11:34:42 +01:00
.catch((err) => {
2019-02-22 17:41:59 +01:00
utils.sendErrorMessage(err, msg);
2019-02-20 17:48:26 +01:00
});
2019-02-19 14:29:57 +01:00
});
bot.on('/remove', (msg) => {
2019-02-20 17:48:26 +01:00
bot.sendMessage(msg.from.id, 'Insert Device ID', { ask: 'removeDevice' });
2019-02-19 14:29:57 +01:00
});
2019-02-20 17:48:26 +01:00
bot.on('ask.removeDevice', (msg) => {
api.removeDevice(msg.text)
2019-02-22 17:41:59 +01:00
.then((res) => {
bot.sendMessage(msg.from.id, 'Device Removed');
2019-02-20 17:48:26 +01:00
})
.catch((err) => {
2019-02-22 17:41:59 +01:00
utils.sendErrorMessage(err, msg);
2019-02-20 17:48:26 +01:00
});
});
2019-02-19 14:21:58 +01:00
bot.start();
2019-02-21 14:56:08 +01:00
process.on('SIGINT', () => {
logger.logInfo('Server stopped');
logger.logConsole('Server stopped');
process.exit(0);
});
2019-02-22 17:41:59 +01:00
process.on('uncaughtException', (err) => {
logger.logError(err);
});