mirror of
https://github.com/franjsco/trackmyd-bot
synced 2025-02-16 11:31:46 +01:00
implements logic (cmd)
This commit is contained in:
parent
6ff357d765
commit
00b274ae92
107
server.js
107
server.js
@ -2,53 +2,112 @@ const Telebot = require('telebot');
|
|||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const auth = require('./auth');
|
const auth = require('./auth');
|
||||||
|
const api = require('./api');
|
||||||
|
|
||||||
const bot = new Telebot({
|
const bot = new Telebot({
|
||||||
token: config.app.tokenBot,
|
token: config.app.tokenBot,
|
||||||
usePlugins: ['askUser'],
|
usePlugins: ['askUser'],
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/*', (msg) => {
|
|
||||||
bot.sendMessage(msg.from.id, 'qualsiasi');
|
|
||||||
});
|
|
||||||
|
|
||||||
bot.on('/start', (msg) => {
|
bot.on('/start', (msg) => {
|
||||||
auth(msg.from.id, () => {
|
if (!auth(msg.from.id)) {
|
||||||
bot.sendMessage(msg.from.id, utils.templateStart(), { parseMode: 'Markdown' });
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
bot.event('/help', msg);
|
}
|
||||||
},
|
bot.sendMessage(msg.from.id, utils.templateStart(), { parseMode: 'Markdown' });
|
||||||
() => bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' }));
|
bot.event('/help', msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/help', (msg) => {
|
bot.on('/help', (msg) => {
|
||||||
auth(msg.from.id, () => {
|
if (!auth(msg.from.id)) {
|
||||||
bot.sendMessage(msg.from.id, utils.templateHelp(), { parseMode: 'Markdown' });
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
});
|
}
|
||||||
|
bot.sendMessage(msg.from.id, utils.templateHelp(), { parseMode: 'Markdown' });
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/list', (msg) => {
|
bot.on('/list', (msg) => {
|
||||||
auth(msg.from.id, () => {
|
if (!auth(msg.from.id)) {
|
||||||
bot.sendMessage(msg.from.id, 'list template');
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
});
|
}
|
||||||
|
|
||||||
|
api.getDevices()
|
||||||
|
.then(res => res.json())
|
||||||
|
.then((json) => {
|
||||||
|
json.forEach((elem) => {
|
||||||
|
bot.sendMessage(msg.from.id, utils.templateDevicesList(elem));
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
bot.sendMessage(msg.from.id, 'errore');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/position', (msg) => {
|
bot.on('/position', (msg) => {
|
||||||
auth(msg.from.id, () => {
|
if (!auth(msg.from.id)) {
|
||||||
bot.sendMessage(msg.from.id, 'position template');
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
});
|
}
|
||||||
|
|
||||||
|
api.getDevices()
|
||||||
|
.then(res => res.json())
|
||||||
|
.then((devicesJSON) => {
|
||||||
|
const devices = [];
|
||||||
|
devicesJSON.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 });
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
bot.sendMessage(msg.from.id, 'Error1234');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.on('ask.devicePosition', (msg) => {
|
||||||
|
if (!auth(msg.from.id)) {
|
||||||
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
|
}
|
||||||
|
|
||||||
|
api.getInfoDevice(msg.text)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then((json) => {
|
||||||
|
bot.sendLocation(msg.from.id, [json.position.latitude, json.position.longtitude]);
|
||||||
|
bot.sendMessage(msg.from.id, utils.templatePosition(json));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
bot.sendMessage(msg.from.id, err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/add', (msg) => {
|
bot.on('/add', (msg) => {
|
||||||
auth(msg.from.id, () => {
|
if (!auth(msg.from.id)) {
|
||||||
bot.sendMessage(msg.from.id, 'add');
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
});
|
}
|
||||||
|
bot.sendMessage(msg.from.id, 'insert name', { ask: 'addDevice' });
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.on('ask.addDevice', (msg) => {
|
||||||
|
if (!auth(msg.from.id)) {
|
||||||
|
return bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' });
|
||||||
|
}
|
||||||
|
api.addDevice(msg.text)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then((json) => {
|
||||||
|
bot.sendMessage(msg.from.id, JSON.stringify(json));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/remove', (msg) => {
|
bot.on('/remove', (msg) => {
|
||||||
auth(msg.from.id, () => {
|
bot.sendMessage(msg.from.id, 'Insert Device ID', { ask: 'removeDevice' });
|
||||||
bot.sendMessage(msg.from.id, 'remove');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bot.on('ask.removeDevice', (msg) => {
|
||||||
|
api.removeDevice(msg.text)
|
||||||
|
.then((res) => {
|
||||||
|
bot.sendMessage(msg.from.id, 'Device Deleted');
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
bot.sendMessage(msg.from.id, 'Error');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
bot.start();
|
bot.start();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user