From cb6d76d1934cb8cf0c96143b02fa078d1f1fab28 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frsposito@users.noreply.github.com> Date: Mon, 26 Aug 2019 17:48:24 +0200 Subject: [PATCH] Edit config --- src/app.js | 25 ++++++++++++++----------- src/config.js | 10 ++++++++-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/app.js b/src/app.js index 2733ac3..55d3349 100644 --- a/src/app.js +++ b/src/app.js @@ -7,22 +7,24 @@ import { initUser, setCity, findAssociedCity } from './db'; import { searchCityTemplate, welcomeTemplate, + helpTemplate, errorTemplate, setLocationTemplate, weatherTemplate, weatherDetailsTemplate, } from './templates'; -const bot = new Telegraf(config.telegram.token); +const bot = new Telegraf(config.telegraf.token); bot.start((ctx) => { - const { id, first_name } = ctx.from; + const { id } = ctx.from; initUser(id) - .then(() => ctx.reply(welcomeTemplate(first_name), { parse_mode: 'Markdown' })) - .catch(() => ctx.reply(errorTemplate, { parse_mode: 'Markdown' })); + .then(() => ctx.reply(welcomeTemplate(), config.telegraf.options.message)) + .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) => { @@ -37,10 +39,10 @@ bot.command('/search', (ctx) => { searchCity(cityName) .then((data) => { 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) .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((json) => { if (json.rain === 'Y') { - ctx.reply(weatherTemplate(), { parse_mode: 'Markdown' }); + ctx.reply(weatherTemplate(), config.telegraf.options.message); } + return 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)); }); diff --git a/src/config.js b/src/config.js index cc7186c..5f7816b 100644 --- a/src/config.js +++ b/src/config.js @@ -1,8 +1,13 @@ require('dotenv').config(); -const telegram = { +const telegraf = { token: process.env.TELEGRAM_TOKEN, + options: { + message: { + parse_mode: 'Markdown', + }, + }, }; const umbrello = { @@ -15,8 +20,9 @@ const database = { password: process.env.DB_PASSWORD, }; + const config = { - telegram, + telegraf, umbrello, database, };