UNPKG

vvlad1973-telegram-framework

Version:
173 lines (122 loc) 4.57 kB
'use strict' import util from 'util'; import { ValidationError } from 'vvlad1973-error-definitions' import SimpleLogger from 'vvlad1973-simple-logger'; import { resolvePath, getRandomValue } from 'vvlad1973-utils'; import { replaceTemplate } from '../helpers/utils.js'; export class Filler { context; logger; constructor(params = {}, strings = {}, options = { stringsFromJson: false, jsonKeyId: '_id', jsonValue: 'Text' }, logger) { this.params = params; this.strings = options.stringsFromJson ? this.loadStringsFromJson( strings, { jsonKeyId: (typeof options.jsonKeyId === 'undefined') ? '_id' : options.jsonKeyId, jsonValue: (typeof options.jsonValue === 'undefined') ? 'Text' : options.jsonValue }) : strings; this.logger = logger ?? new SimpleLogger(); } loadStringsFromJson(json, options) { let result = {}; json.forEach(function (item, index) { if (typeof item[options.jsonKeyId] !== 'undefined' && typeof item[options.jsonValue] !== 'undefined') { result[item[options.jsonKeyId]] = item[options.jsonValue]; } else { throw new ValidationError(`Error loading strings from JSON: item #${index}`); } }); return result; } addParam(key, value) { this.params[key] = value; } deleteParam(key) { delete this.params[key]; } clearParams() { this.params = {}; } async completeText(text) { let stringIdArray = []; let params = []; let values = []; let paramsList = this.params; let parts; if (text.match(/\{\{([\w.]+)\}\}/g)) { text = await replaceTemplate(text, this.strings, this.context); } parts = text.match(/^(.*)@([A-Za-z_0-9]+|\/.+\/)?(\:\[(.+)\])?$/ms); if (text.match(/\{\{([\w.]+)\}\}/g)) { text = await replaceTemplate(text, this.strings, this.context); } if (!parts) return text; if (typeof parts[4] !== 'undefined') { params = parts[4].split('@').slice(1); for (let item of params) { let value; if (typeof paramsList[item] !== 'undefined') { if (typeof paramsList[item] === 'function') value = await paramsList[item]() else value = await paramsList[item]; } else { value = resolvePath(this.context, item); if (typeof value === 'undefined') { value = 'undefined' this.logger.warn(`Parameter ${item} is not defined!`); } else if (value instanceof Promise) value = await value; } values.push(value); } } if (typeof parts[2] === 'undefined') { text = parts[1] } else { let matches = parts[2].match(/\/(.+)\//); if (Array.isArray(matches) && typeof matches[1] === 'string') text = getRandomValue(matches[1], this.strings) else text = this.strings[parts[2]]; } if (text) stringIdArray = [text].concat(values) else stringIdArray = values; text = util.format.apply(util, stringIdArray); if (text.match(/\{\{([\w.]+)\}\}/g)) { text = await replaceTemplate(text, this.strings, this.context); } return text; } async completeArray(object, self) { self = self ?? this; if (typeof object === 'object') { for (let i = 0; i < object.length; i++) { if (typeof object[i] === 'object') { if (typeof object[i].text === 'string') { object[i].text = await self.completeText(object[i].text); if (typeof object[i].url === 'string') object[i].url = await self.completeText(object[i].url); } else object[i] = await self.completeArray(object[i], self); } else if (typeof object[i] === 'string') object[i] = await self.completeText(object[i]); } return object; } } async completeReplyMarkup(markup) { if (typeof markup.keyboard === 'object') markup.keyboard = await this.completeArray(markup.keyboard, this) else if (typeof markup.inline_keyboard === 'object') markup.inline_keyboard = await this.completeArray(markup.inline_keyboard, this); if (typeof markup.input_field_placeholder === 'string') markup.input_field_placeholder = await this.completeText(markup.input_field_placeholder); return markup; } }