From 5d31fffc321717afb6c0cbb0f5dab9bd8f844eda Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 7 Mar 2023 13:04:37 +0300 Subject: Conditional modules initialization --- vnext/server/durov.js | 27 ++++++++++++++++++--------- vnext/server/hms.js | 33 ++++++++++++++++++--------------- vnext/server/index.js | 10 ++++++---- 3 files changed, 42 insertions(+), 28 deletions(-) (limited to 'vnext') diff --git a/vnext/server/durov.js b/vnext/server/durov.js index df33f743..e9bbdb9a 100644 --- a/vnext/server/durov.js +++ b/vnext/server/durov.js @@ -7,17 +7,26 @@ import config from 'config'; import { formatQuote, formatTitle } from './common/MessageUtils'; import { format } from '../src/utils/embed'; -const bot_token = config.get('service.durov.token'); +let sender; +let demouser; +let bot_token; +const durov_token_key = 'service.durov.token'; -const sender = new TelegramBot(bot_token); -const demouser = config.get('service.durov.demouser'); -sender.setWebHook(`${config.get('service.baseURL')}/api/${bot_token}`).then(() => { - log('Webhook is set'); -}).catch(console.log); +if (config.has(durov_token_key)) { + bot_token = config.get(durov_token_key); + sender = new TelegramBot(bot_token); + demouser = config.get('service.durov.demouser'); + sender.setWebHook(`${config.get('service.baseURL')}/api/${bot_token}`).then(() => { + log('Webhook is set'); + }).catch(console.log); + sender.on('message', msg => { + log(`MESSAGE: ${JSON.stringify(msg)}`); + }); +} -sender.on('message', msg => { - log(`MESSAGE: ${JSON.stringify(msg)}`); -}); +export const webhookPath = () => { + return bot_token; +}; export const webhook = (req, res) => { sender.processUpdate(JSON.parse(req.body)); diff --git a/vnext/server/hms.js b/vnext/server/hms.js index e9068c58..1f897cef 100644 --- a/vnext/server/hms.js +++ b/vnext/server/hms.js @@ -3,23 +3,26 @@ import config from 'config'; import debug from 'debug'; var log = debug('hms'); -const { client_id, client_secret } = config.get('service.hms'); +const { client_id, client_secret } = config.has('service.hms') ? config.get('service.hms') : {}; const refreshToken = async () => { - const params = new URLSearchParams(); - params.append('grant_type', 'client_credentials'); - params.append('client_id', client_id); - params.append('client_secret', client_secret); - const res = await axios.post('https://oauth-login.cloud.huawei.com/oauth2/v3/token', params).catch(console.log); - try { - log(`HMS response: ${JSON.stringify(res.data)}`); - const access = res.data; - log(`HMS access token: ${access.access_token}`); - return access.access_token; - } catch (error) { - log(error); - return ''; + if (client_id) { + const params = new URLSearchParams(); + params.append('grant_type', 'client_credentials'); + params.append('client_id', client_id); + params.append('client_secret', client_secret); + const res = await axios.post('https://oauth-login.cloud.huawei.com/oauth2/v3/token', params).catch(console.log); + try { + log(`HMS response: ${JSON.stringify(res.data)}`); + const access = res.data; + log(`HMS access token: ${access.access_token}`); + return access.access_token; + } catch (error) { + log(error); + return ''; + } } + return ''; }; export const send = async (msg, tokenList = []) => { @@ -36,7 +39,7 @@ export const send = async (msg, tokenList = []) => { 'android': { 'fast_app_target': 2 }, - 'data': { + 'data': { 'pushtype': 1, 'pushbody': { 'custom': { diff --git a/vnext/server/index.js b/vnext/server/index.js index b4c637bf..5bfa9070 100644 --- a/vnext/server/index.js +++ b/vnext/server/index.js @@ -10,11 +10,9 @@ import event from './middleware/event'; import oembed from './middleware/oembed'; import urlExpand from './middleware/urlexpand'; -const bot_token = config.get('service.durov.token'); - const PORT = 8081; import path from 'path'; -import { webhook } from './durov'; +import { webhook, webhookPath } from './durov'; // initialize the application and create the routes const app = express(); @@ -25,7 +23,11 @@ const router = express.Router(); router.post('/api/sender', event); router.get('/api/oembed', oembed); router.get('/api/urlexpand', urlExpand); -router.post(`/api/${bot_token}`, webhook); + +const durov_webhook = webhookPath(); +if (durov_webhook) { + router.post(`/api/${durov_webhook}`, webhook); +} router.use('^/$', serverRenderer); const STATIC_ROOT = config.get('service.static_root') || path.resolve(__dirname, 'public'); -- cgit v1.2.3