Skip to content

Commit

Permalink
feat: начальная поддержка переменных в запросах к вебхукам
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Feb 2, 2019
1 parent 9f9e4b0 commit c72e8a6
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/commands/core/customWebhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 || 'Готово');
}
};

0 comments on commit c72e8a6

Please sign in to comment.