Edit config

This commit is contained in:
Francesco Esposito 2019-08-26 17:48:24 +02:00
parent b4c4d2c0c2
commit cb6d76d193
2 changed files with 22 additions and 13 deletions

View File

@ -7,22 +7,24 @@ import { initUser, setCity, findAssociedCity } from './db';
import { import {
searchCityTemplate, searchCityTemplate,
welcomeTemplate, welcomeTemplate,
helpTemplate,
errorTemplate, errorTemplate,
setLocationTemplate, setLocationTemplate,
weatherTemplate, weatherTemplate,
weatherDetailsTemplate, weatherDetailsTemplate,
} from './templates'; } from './templates';
const bot = new Telegraf(config.telegram.token); const bot = new Telegraf(config.telegraf.token);
bot.start((ctx) => { bot.start((ctx) => {
const { id, first_name } = ctx.from; const { id } = ctx.from;
initUser(id) initUser(id)
.then(() => ctx.reply(welcomeTemplate(first_name), { parse_mode: 'Markdown' })) .then(() => ctx.reply(welcomeTemplate(), config.telegraf.options.message))
.catch(() => ctx.reply(errorTemplate, { parse_mode: 'Markdown' })); .then(() => ctx.reply(helpTemplate(), config.telegraf.options.message))
.catch(() => ctx.reply(errorTemplate(), config.telegraf.options.message));
}); });
bot.help((ctx) => ctx.reply('help')); bot.help((ctx) => ctx.reply(helpTemplate()));
bot.command('/search', (ctx) => { bot.command('/search', (ctx) => {
@ -37,10 +39,10 @@ bot.command('/search', (ctx) => {
searchCity(cityName) searchCity(cityName)
.then((data) => { .then((data) => {
data.forEach((e) => { data.forEach((e) => {
ctx.reply(searchCityTemplate(e), { parse_mode: 'Markdown' }); ctx.reply(searchCityTemplate(e), config.telegraf.options.message);
}); });
}) })
.catch(() => ctx.reply(errorTemplate, { parse_mode: 'Markdown' })); .catch(() => ctx.reply(errorTemplate(), config.telegraf.options.message));
}); });
@ -57,7 +59,7 @@ bot.command('/set', (ctx) => {
setCity(id, cityId) setCity(id, cityId)
.then(() => ctx.reply(setLocationTemplate(cityId))) .then(() => ctx.reply(setLocationTemplate(cityId)))
.catch(() => ctx.reply(errorTemplate, { parse_mode: 'Markdown' })); .catch(() => ctx.reply(errorTemplate(), config.telegraf.options.message));
}); });
@ -68,14 +70,15 @@ bot.command('/get', (ctx) => {
.then((cityId) => rainCheck(cityId)) .then((cityId) => rainCheck(cityId))
.then((json) => { .then((json) => {
if (json.rain === 'Y') { if (json.rain === 'Y') {
ctx.reply(weatherTemplate(), { parse_mode: 'Markdown' }); ctx.reply(weatherTemplate(), config.telegraf.options.message);
} }
return json; return json;
}) })
.then((json) => { .then((json) => {
ctx.reply(weatherDetailsTemplate(json), { parse_mode: 'Markdown' }); ctx.reply(weatherDetailsTemplate(json), config.telegraf.options.message);
}) })
.catch((error) => ctx.reply(error)); .catch(() => ctx.reply(errorTemplate(), config.telegraf.options.message));
}); });

View File

@ -1,8 +1,13 @@
require('dotenv').config(); require('dotenv').config();
const telegram = { const telegraf = {
token: process.env.TELEGRAM_TOKEN, token: process.env.TELEGRAM_TOKEN,
options: {
message: {
parse_mode: 'Markdown',
},
},
}; };
const umbrello = { const umbrello = {
@ -15,8 +20,9 @@ const database = {
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,
}; };
const config = { const config = {
telegram, telegraf,
umbrello, umbrello,
database, database,
}; };