aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2023-03-07 13:04:37 +0300
committerGravatar Vitaly Takmazov2023-03-07 13:04:37 +0300
commit5d31fffc321717afb6c0cbb0f5dab9bd8f844eda (patch)
tree3bd96dc3ec6bb5e63a8442992c04f6c96ec35cff
parentb4ce703820264a7ed16b5b3efee973aadc43335d (diff)
Conditional modules initialization
-rw-r--r--config/default.json2
-rw-r--r--vnext/server/durov.js27
-rw-r--r--vnext/server/hms.js33
-rw-r--r--vnext/server/index.js10
4 files changed, 43 insertions, 29 deletions
diff --git a/config/default.json b/config/default.json
index 618489ca..895509d2 100644
--- a/config/default.json
+++ b/config/default.json
@@ -6,4 +6,4 @@
"token": ""
}
}
-} \ No newline at end of file
+}
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');