diff --git a/bot.js b/bot.js index b2b25de..1b610ed 100644 --- a/bot.js +++ b/bot.js @@ -2,6 +2,7 @@ const Telegraf = require("telegraf"); const { BOT_API_KEY, FEEDBACK_ID } = process.env; const fs = require("fs").promises; const commands = require("./commands"); +const axios = require("axios"); const bot = new Telegraf(BOT_API_KEY); @@ -18,6 +19,6 @@ const data = [ Promise.all(data) .then(data => - commands(bot, data, FEEDBACK_ID)); + commands(bot, data, FEEDBACK_ID, axios)); bot.launch(); diff --git a/commands/index.js b/commands/index.js index 7fce72e..7ef817f 100644 --- a/commands/index.js +++ b/commands/index.js @@ -5,9 +5,10 @@ const is = require("./is"); const weebify = require("./weebify"); const absurdify = require("./absurdify"); const feedback = require("./feedback"); +const media_wiki = require("./media_wiki"); module.exports = (bot, [ questions, kys, insults, commands_list, words ], - feedback_id) => { + feedback_id, axios) => { bot.command("question", (ctx) => ctx.reply(random(questions)())); bot.command("word", (ctx) => ctx.reply(random(words)())); @@ -51,5 +52,11 @@ module.exports = (bot, [ questions, kys, insults, commands_list, words ], bot.command("weebify", (ctx) => ctx.reply(weebify()(ctx))); bot.command("absurdify", (ctx) => ctx.reply(absurdify()(ctx))); bot.command("feedback", (ctx) => ctx.reply(feedback(feedback_id)(ctx))); + bot.command("wiki", (ctx) => media_wiki(axios, + "https://en.wikipedia.org/w/api.php")(ctx).then(x => ctx.reply(x, + {parse_mode: "HTML"}))); + bot.command("arch_wiki", (ctx) => media_wiki(axios, + "https://wiki.archlinux.org/api.php")(ctx).then(x => ctx.reply(x, + {parse_mode: "HTML"}))); }; diff --git a/commands/media_wiki.js b/commands/media_wiki.js new file mode 100644 index 0000000..ce35186 --- /dev/null +++ b/commands/media_wiki.js @@ -0,0 +1,22 @@ +module.exports = (axios, url) => (ctx) => { + + const message = ctx.message.text.replace(/^[^ ]+/, ""); + if (message) { + + return axios.get(`${url}?action=opensearch&format=json&search=` + + `${encodeURIComponent(message)}`) + .then((res) => { + + const names = res.data[1]; + const urls = res.data[3]; + if (names.length == 0) + return "No results found"; + return "Results\n\n" + names.map((val, index) => + `${val}`).join("\n"); + + }); + + } else + return Promise.resolve("Missing search query."); + +}; diff --git a/package.json b/package.json index eff4865..ec1eb04 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "homepage": "https://gitlab.com/ceda_ei/Quadnite-Bot#readme", "dependencies": { + "axios": "^0.18.0", "telegraf": "^3.27.1" } } diff --git a/shrinkwrap.yaml b/shrinkwrap.yaml index b13e240..e540d2b 100644 --- a/shrinkwrap.yaml +++ b/shrinkwrap.yaml @@ -1,16 +1,46 @@ dependencies: + axios: 0.18.0 telegraf: 3.27.1 packages: /@types/node/10.12.24: dev: false resolution: integrity: sha512-GWWbvt+z9G5otRBW8rssOFgRY87J9N/qbhqfjMZ+gUuL6zoL+Hm6gP/8qQBG4jjimqdaNLCehcVapZ/Fs2WjCQ== + /axios/0.18.0: + dependencies: + follow-redirects: 1.6.1 + is-buffer: 1.1.6 + dev: false + resolution: + integrity: sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI= + /debug/3.1.0: + dependencies: + ms: 2.0.0 + dev: false + resolution: + integrity: sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== /debug/4.1.1: dependencies: ms: 2.1.1 dev: false resolution: integrity: sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw== + /follow-redirects/1.6.1: + dependencies: + debug: 3.1.0 + dev: false + engines: + node: '>=4.0' + resolution: + integrity: sha512-t2JCjbzxQpWvbhts3l6SH1DKzSrx8a+SsaVf4h6bG4kOXUuPYS/kg2Lr4gQSb7eemaHqJkOThF1BGyjlUkO1GQ== + /is-buffer/1.1.6: + dev: false + resolution: + integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w== + /ms/2.0.0: + dev: false + resolution: + integrity: sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= /ms/2.1.1: dev: false resolution: @@ -47,4 +77,5 @@ registry: 'https://registry.npmjs.org/' shrinkwrapMinorVersion: 9 shrinkwrapVersion: 3 specifiers: + axios: ^0.18.0 telegraf: ^3.27.1