diff --git a/src/commands/core/customWebhooks.js b/src/commands/core/customWebhooks.js index 80b459b..46d9d21 100644 --- a/src/commands/core/customWebhooks.js +++ b/src/commands/core/customWebhooks.js @@ -2,19 +2,32 @@ const axios = require('axios'); const matchers = require('../../matchers'); +const placeholderRegex = /\{\{[a-zA-Z0-9_-]+\}\}/; +const values = {}; + +// умеет отрабатывать строки типа "скачай фильм {{q}}" const getWebhook = ctx => { - return ctx.user.state.webhooks.find(webhook => { + let matched = false; + let found = ctx.user.state.webhooks.find(webhook => { webhook.regs = webhook.strings.map(string => { + string = string.replace(placeholderRegex, '(.*)?'); const reg = new RegExp(string); return reg; }); - const match = webhook.regs.find(reg => { + const matchReg = webhook.regs.find(reg => { return reg.test(ctx.message); }); - return match; + if (matchReg) matched = matchReg.exec(ctx.message); + return matchReg; }); + + if (found) { + found.matched = matched; + } + + return found; }; module.exports = { @@ -25,13 +38,18 @@ module.exports = { strings: ['тестовый вебхук'], url: 'https://yandex.ru' } */ - return getWebhook(ctx) ? 1 : 0; + return getWebhook(ctx) ? 0.9 : 0; }, async handler(ctx) { ctx.logMessage(`> ${ctx.message} (customWebhooks)`); const webhook = getWebhook(ctx); - const answer = axios.get(webhook.url); + + // if (webhook.url.match(placeholderRegex)) { + values.q = webhook.matched[1]; // TODO: всегда передается q, хотя могут быть другие имена + // webhook.url = webhook.url.replace(placeholderRegex, ); + // } + const answer = axios.get(webhook.url, { params: values }); return ctx.reply(webhook.answer || 'Готово'); } };