enwnbot

Converts MediaWiki [[links]] and {{templates}} to links on IRC
git clone http://git.hanabi.in/repos/enwnbot.git
Log | Files | Refs | README | LICENSE

commit 4ee2dc06446c93d0f0fc2a01f3c431e86cc07491
parent 8905d73e61bf1aff3cfe2d07fb4d639384213575
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Fri, 29 May 2020 18:11:15 +0530

a fallback with commands for w.wiki

Diffstat:
Mconfig.js | 1+
Mirc.js | 35++++++-----------------------------
Mpackage.json | 2+-
ApromUrlShortener.js | 157+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Myarn.lock | 87+------------------------------------------------------------------------------
5 files changed, 166 insertions(+), 116 deletions(-)

diff --git a/config.js b/config.js @@ -1,5 +1,6 @@ const config = { admins: ['jdoe'], + botAccount: 'bot@botaccount', botName: 'linkBOT', botPass: 'botPass', channels: ['#foo', '##bar'], diff --git a/irc.js b/irc.js @@ -1,5 +1,4 @@ const irc = require('irc'); -const MWBot = require('mwbot'); const { admins, @@ -21,31 +20,12 @@ const { urlShortener1, } = require('./utils'); +const { fallback, reset, short } = require('./promUrlShortener'); + const client = new irc.Client(server, botName, { channels, }); -let bot = new MWBot(); -const globalErr = {}; -(async function () { - try { - await bot.login({ - username: 'Enwnbot', - password: botPass, - apiUrl: 'https://meta.wikimedia.org/w/api.php', - }); - console.log('Ready to receive inputs...'); - channels.forEach((channel) => - client.say(channel, 'Ready to receive inputs...') - ); - } catch (error) { - channels.forEach((channel) => - client.say(channel, 'Error occurred. Using fallback method.') - ); - globalErr.error = error; - } -})(); - function pm(sender, msg) { client.say(sender, 'I am a bot.'); if (msg.startsWith(report)) @@ -83,12 +63,7 @@ async function announceRQ(sender, channel) { } async function sayShortUrls(urlList, channel) { - let shortUrls; - if (globalErr.error) { - shortUrls = await Promise.all(urlList.map(urlShortener1)); - } else { - shortUrls = await Promise.all(urlList.map((url) => urlShortener(url, bot))); - } + const shortUrls = await Promise.all(urlList.map(short)); shortUrls.forEach(({ url, err }) => { if (!err) client.say(channel, url); }); @@ -98,7 +73,9 @@ function groupChat(sender, channel, msg) { const lcMsg = msg.toLowerCase(); if (lcMsg.includes(`thanks ${botName}`)) client.say(channel, `You are welcome, ${sender}.`); - if (msg.includes(`${botName} /RQ`)) announceRQ(sender, channel); + if (msg.includes(`${botName} !RQ`)) announceRQ(sender, channel); + if (msg.includes(`${botName} !FB`)) fallback(); + if (msg.includes(`${botName} !TRY`)) reset(); const regex1 = /\[{2}(.*?)\]{2}/g; const regex2 = /\{{2}(.*?)\}{2}/g; const links = msg.match(regex1); diff --git a/package.json b/package.json @@ -1,8 +1,8 @@ { "dependencies": { "irc": "^0.5.2", - "mwbot": "^1.0.10", "node-fetch": "^2.6.0", + "request": "^2.88.2", "tinyurl": "^1.1.5" }, "scripts": { diff --git a/promUrlShortener.js b/promUrlShortener.js @@ -0,0 +1,157 @@ +const TUrl = require('tinyurl'); +const { promisify } = require('util'); +const R = require('request').defaults({ jar: true }); + +const { botAccount, botPass } = require('./config'); + +const request = promisify(R); +const get = promisify(R.get); +const post = promisify(R.post); + +const url = 'https://meta.wikimedia.org/w/api.php'; +const globalState = { + mainExecuted: false, +}; + +let logintoken; +let csrfToken; + +async function getLoginToken() { + const res = {}; + try { + const data = await get({ + url, + qs: { + action: 'query', + meta: 'tokens', + type: 'login', + format: 'json', + }, + }); + const { body } = data; + const parsed = JSON.parse(body); + res.logintoken = parsed.query.tokens.logintoken; + } catch (error) { + console.log(error); + res.error = error; + } + return res; +} + +async function loginRequest(TOKEN) { + const res = {}; + try { + const data = await post({ + url, + form: { + action: 'login', + lgname: botAccount, + lgpassword: botPass, + lgtoken: TOKEN, + format: 'json', + }, + }); + const { body } = data; + const parsed = JSON.parse(body); + const result = parsed.login.result; + if (result != 'Success') { + globalState.error = parsed.login.reason; + res.error = parsed.login.reason; + } else res.result = result; + } catch (error) { + console.log(error); + globalState.error = error; + res.error = error; + } + return res; +} + +async function getCsrfToken() { + const res = {}; + try { + const data = await get({ + url, + qs: { + action: 'query', + meta: 'tokens', + format: 'json', + }, + }); + const { body } = data; + const parsed = JSON.parse(body); + res.csrfToken = parsed.query.tokens.csrftoken; + } catch (error) { + res.error = error; + globalState.useFallback = true; + } + return res; +} + +async function main() { + try { + const maybeLoginToken = await getLoginToken(); + if (maybeLoginToken.error) globalState.useFallback = true; + else { + logintoken = maybeLoginToken.logintoken; + const maybeSuccess = await loginRequest(logintoken); + if (maybeSuccess.error) globalState.useFallback = true; + else { + const maybeCsrfToken = await getCsrfToken(); + if (maybeCsrfToken.error) globalState.useFallback = true; + else csrfToken = maybeCsrfToken.csrfToken; + } + } + } catch (error) { + console.error(error); + globalState.useFallback = true; + } + globalState.mainExecuted = true; +} + +main(); + +async function short(URI) { + if (!globalState.mainExecuted) main(); + if (!URI) + URI = + 'https://en.wikinews.org/wiki/Astronomer_tells_Wikinews_about_discovery_of_closest_black_hole_known_so_far'; + const res = {}; + if (!globalState.error && !globalState.useFallback) { + try { + const data = await post({ + url, + form: { + action: 'shortenurl', + url: URI, + format: 'json', + }, + }); + const { body } = data; + const parsed = JSON.parse(body); + res.url = parsed.shortenurl.shorturl; + } catch (error) { + globalState.error = error; + res.error = error; + } + } else { + try { + res.url = await TUrl.shorten(URI); + } catch (error) { + res.error = error; + console.error(error); + } + } + return res; +} + +function fallback() { + globalState.useFallback = true; +} + +function reset() { + globalState.mainExecuted = false; + globalState.error = false; + globalState.useFallback = false; +} + +module.exports = { fallback, reset, short }; diff --git a/yarn.lock b/yarn.lock @@ -12,16 +12,6 @@ ajv@^6.5.5: json-schema-traverse "^0.4.1" uri-js "^4.2.2" -ansi-regex@^2.0.0: - version "2.1.1" - resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df" - integrity sha1-w7M6te42DYbg5ijwRorn7yfWVN8= - -ansi-styles@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe" - integrity sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4= - asn1@~0.2.3: version "0.2.4" resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136" @@ -56,32 +46,11 @@ bcrypt-pbkdf@^1.0.0: dependencies: tweetnacl "^0.14.3" -bluebird@^3.4.6: - version "3.7.2" - resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f" - integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg== - caseless@~0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= -chalk@^1.1.3: - version "1.1.3" - resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" - integrity sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg= - dependencies: - ansi-styles "^2.2.1" - escape-string-regexp "^1.0.2" - has-ansi "^2.0.0" - strip-ansi "^3.0.0" - supports-color "^2.0.0" - -colors@^1.1.2: - version "1.4.0" - resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78" - integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA== - combined-stream@^1.0.6, combined-stream@~1.0.6: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" @@ -114,11 +83,6 @@ ecc-jsbn@~0.1.1: jsbn "~0.1.0" safer-buffer "^2.1.0" -escape-string-regexp@^1.0.2: - version "1.0.5" - resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" - integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= - extend@~3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa" @@ -178,13 +142,6 @@ har-validator@~5.1.3: ajv "^6.5.5" har-schema "^2.0.0" -has-ansi@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91" - integrity sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE= - dependencies: - ansi-regex "^2.0.0" - http-signature@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1" @@ -268,20 +225,6 @@ mime-types@^2.1.12, mime-types@~2.1.19: dependencies: mime-db "1.44.0" -minimist@^1.2.0: - version "1.2.5" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" - integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== - -mwbot@^1.0.10: - version "1.0.10" - resolved "https://registry.yarnpkg.com/mwbot/-/mwbot-1.0.10.tgz#a440bd6663a7628ab5b799609e98cb2fc4e133c9" - integrity sha1-pEC9ZmOnYoq1t5lgnpjLL8ThM8k= - dependencies: - bluebird "^3.4.6" - request "^2.75.0" - semlog "^0.6.10" - nan@^2.3.3, nan@^2.3.5: version "2.14.1" resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.1.tgz#d7be34dfa3105b91494c3147089315eff8874b01" @@ -309,14 +252,6 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= -prettyjson@^1.1.3: - version "1.2.1" - resolved "https://registry.yarnpkg.com/prettyjson/-/prettyjson-1.2.1.tgz#fcffab41d19cab4dfae5e575e64246619b12d289" - integrity sha1-/P+rQdGcq0365eV15kJGYZsS0ok= - dependencies: - colors "^1.1.2" - minimist "^1.2.0" - psl@^1.1.28: version "1.8.0" resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24" @@ -332,7 +267,7 @@ qs@~6.5.2: resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36" integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA== -request@^2.75.0: +request@^2.88.2: version "2.88.2" resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3" integrity sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw== @@ -368,14 +303,6 @@ safer-buffer@^2.0.2, safer-buffer@^2.1.0, safer-buffer@~2.1.0: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== -semlog@^0.6.10: - version "0.6.10" - resolved "https://registry.yarnpkg.com/semlog/-/semlog-0.6.10.tgz#0f225aea8eb3c2f24ce9358d8678d0f41a7fe05b" - integrity sha1-DyJa6o6zwvJM6TWNhnjQ9Bp/4Fs= - dependencies: - chalk "^1.1.3" - prettyjson "^1.1.3" - sshpk@^1.7.0: version "1.16.1" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877" @@ -391,18 +318,6 @@ sshpk@^1.7.0: safer-buffer "^2.0.2" tweetnacl "~0.14.0" -strip-ansi@^3.0.0: - version "3.0.1" - resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf" - integrity sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8= - dependencies: - ansi-regex "^2.0.0" - -supports-color@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" - integrity sha1-U10EXOa2Nj+kARcIRimZXp3zJMc= - tinyurl@^1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/tinyurl/-/tinyurl-1.1.5.tgz#b0e7b06e7d3dc578bc8c217880822d95d3817aa4"