diff --git a/handlers/regex/groupLinker.js b/handlers/regex/groupLinker.js new file mode 100644 index 0000000..ed0bf27 --- /dev/null +++ b/handlers/regex/groupLinker.js @@ -0,0 +1,27 @@ +'use strict'; + +const { hears } = require('telegraf'); +const XRegExp = require('xregexp'); + +const { managesGroup } = require.main.require('./stores/group'); + +const regex = /^(?:Link to\s+)?(.+) group(?: link)?\s*\?*$/i; + +const handler = async (ctx, next) => { + let [ , groupName ] = ctx.match; + if (groupName.toLowerCase() === 'this') { + if (!ctx.chat.title) return next(); + groupName = ctx.chat.title; + } + + const $regex = XRegExp.tag('i')`${groupName}`; + + const group = await managesGroup({ title: { $regex } }); + const { link } = group || {}; + + if (!link) return next(); + + return ctx.reply(link, { reply_to_message_id: ctx.message.message_id }); +}; + +module.exports = hears(regex, handler); diff --git a/handlers/regex/index.js b/handlers/regex/index.js new file mode 100644 index 0000000..1d8f2b5 --- /dev/null +++ b/handlers/regex/index.js @@ -0,0 +1,9 @@ +'use strict'; + +const { compose } = require('telegraf'); + +/* eslint-disable global-require */ + +module.exports = compose([ + require('./groupLinker'), +]); diff --git a/handlers/unmatched.js b/handlers/unmatched.js new file mode 100644 index 0000000..bfc597e --- /dev/null +++ b/handlers/unmatched.js @@ -0,0 +1,9 @@ +'use strict'; + +const R = require('ramda'); +const Tf = require('telegraf'); + +module.exports = Tf.optional( + R.pathEq([ 'chat', 'type' ], 'private'), + Tf.reply('Sorry, I couldn\'t understand that, do you need /help?') +); diff --git a/index.js b/index.js index 816a48e..cf1415b 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ const bot = require('./bot'); bot.telegram.getMe().then((botInfo) => { bot.options.username = botInfo.username; + bot.context.botInfo = botInfo; }); bot.use( @@ -18,6 +19,8 @@ bot.use( require('./handlers/messages'), require('./plugins'), require('./handlers/commands'), + require('./handlers/regex'), + require('./handlers/unmatched'), ); diff --git a/package-lock.json b/package-lock.json index b47ba63..0498138 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1421,6 +1421,11 @@ "mkdirp": "0.5.1" } }, + "xregexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/xregexp/-/xregexp-4.0.0.tgz", + "integrity": "sha512-PHyM+sQouu7xspQQwELlGwwd05mXUFqwFYfqPO0cC7x4fxyHnnuetmQr6CjJiafIDoH4MogHb9dOoJzR/Y4rFg==" + }, "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", diff --git a/package.json b/package.json index 12c0c88..eb563c1 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "millisecond": "^0.1.2", "nedb-promise": "^2.0.1", "ramda": "^0.25.0", - "telegraf": "^3.15.3" + "telegraf": "^3.15.3", + "xregexp": "^4.0.0" }, "engines": { "node": ">=8.1.3"