Skip to content

Commit 7d18c90

Browse files
committed
Update to [email protected] and refactor
1 parent 5e460c8 commit 7d18c90

17 files changed

+241
-238
lines changed

functions.js

-63
This file was deleted.

handlers/callbacks.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const codebottle = require(`codebottle`);
2+
const { inlineButtons, formatText } = require(`../helpers`);
3+
4+
module.exports = bot => {
5+
bot.action(/^code_(.+)$/, async ctx => {
6+
try {
7+
const [, id] = ctx.match;
8+
const data = await codebottle.fetch(id);
9+
10+
await ctx.editMessageText(
11+
formatText(data, true),
12+
Object.assign({
13+
parse_mode: `html`,
14+
}, inlineButtons(id))
15+
);
16+
17+
ctx.answerCbQuery();
18+
} catch (e) {
19+
ctx.answerCbQuery(`There was an error.`, true);
20+
console.log(e);
21+
}
22+
});
23+
24+
bot.action(/^desc_(.+)$/, async ctx => {
25+
try {
26+
const [, id] = ctx.match;
27+
const data = await codebottle.fetch(id);
28+
29+
await ctx.editMessageText(
30+
formatText(data),
31+
Object.assign({
32+
parse_mode: `html`,
33+
}, inlineButtons(id, true))
34+
);
35+
36+
ctx.answerCbQuery();
37+
} catch (e) {
38+
ctx.answerCbQuery(`There was an error.`, true);
39+
console.log(e);
40+
}
41+
});
42+
};

handlers/fetch.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const codebottle = require(`codebottle`);
2+
const { inlineButtons, formatText } = require(`../helpers`);
3+
4+
module.exports = bot =>
5+
bot.command(`fetch`, async ctx => {
6+
try {
7+
let { text, entities } = ctx.message;
8+
text = text.slice(entities[0].length + 1);
9+
10+
if (text) {
11+
const data = await codebottle.fetch(text);
12+
13+
ctx.reply(
14+
formatText(data),
15+
Object.assign({
16+
parse_mode: `html`,
17+
}, inlineButtons(data.id, true))
18+
);
19+
}
20+
} catch (e) {
21+
ctx.reply(`There was an error.`);
22+
console.log(e);
23+
}
24+
});

handlers/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const handlers = [
2+
`start`,
3+
`search`,
4+
`latest`,
5+
`fetch`,
6+
`inline`,
7+
`callbacks`,
8+
];
9+
10+
module.exports = bot =>
11+
handlers.forEach(handler =>
12+
require(`./${handler}`)(bot)
13+
);

handlers/inline.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const codebottle = require(`codebottle`);
2+
const { inlineButtons, formatText } = require(`../helpers`);
3+
4+
module.exports = bot =>
5+
bot.on(`inline_query`, async ctx => {
6+
const results = [];
7+
const { query } = ctx.inlineQuery;
8+
9+
try {
10+
const data = await (query && query.length > 2 ?
11+
codebottle.search({ query }) :
12+
codebottle.latest);
13+
14+
data.forEach(snippet => {
15+
results.push({
16+
type: `article`,
17+
id: snippet.id,
18+
title: snippet.title,
19+
input_message_content: {
20+
message_text: formatText(snippet),
21+
parse_mode: `html`,
22+
disable_web_page_preview: true,
23+
},
24+
url: `https://codebottle.io/s/${snippet.id}`,
25+
hide_url: true,
26+
...inlineButtons(snippet.id, true),
27+
});
28+
});
29+
} catch (e) {
30+
results.push({
31+
type: `article`,
32+
id: `error`,
33+
title: `There was an error.`,
34+
message_text: `¯\\_(ツ)_/¯`,
35+
});
36+
37+
console.log(e);
38+
} finally {
39+
ctx.answerInlineQuery(results.slice(0, 50));
40+
}
41+
});

handlers/latest.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const codebottle = require(`codebottle`);
2+
const { inlineButtons, formatText } = require(`../helpers`);
3+
4+
module.exports = bot =>
5+
bot.command(`latest`, async ctx => {
6+
try {
7+
const data = await codebottle.latest;
8+
ctx.reply(
9+
formatText(data[0]),
10+
Object.assign({
11+
parse_mode: `html`,
12+
}, inlineButtons(data[0].id, true))
13+
);
14+
} catch (e) {
15+
ctx.reply(`There was an error.`);
16+
console.log(e);
17+
}
18+
});

handlers/search.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const codebottle = require(`codebottle`);
2+
const { inlineButtons, formatText } = require(`../helpers`);
3+
4+
module.exports = bot =>
5+
bot.command(`search`, async ctx => {
6+
try {
7+
let { text, entities } = ctx.message;
8+
text = text.slice(entities[0].length + 1);
9+
10+
if (text) {
11+
const data = await codebottle.search({ query: text });
12+
13+
ctx.reply(
14+
formatText(data[0]),
15+
Object.assign({
16+
parse_mode: `html`,
17+
}, inlineButtons(data[0].id, true))
18+
);
19+
}
20+
} catch (e) {
21+
ctx.reply(`There was an error.`);
22+
console.log(e);
23+
}
24+
});

handlers/start.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = bot =>
2+
bot.command(`start`, async ctx => ctx.reply(`Hi I'm CodeBottle Bot.`));

helpers/escapeHtml.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const htmlEntities = {
2+
'<': `&lt;`,
3+
'>': `&gt;`,
4+
'&': `&amp;`,
5+
'"': `&quot;`,
6+
};
7+
8+
const escapeHtml = html => {
9+
return html.replace(
10+
new RegExp(`[${Object.keys(htmlEntities).join(``)}]`, `g`),
11+
char => htmlEntities[char]
12+
);
13+
};
14+
15+
module.exports = escapeHtml;

helpers/formatText.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const escapeHtml = require(`./escapeHtml`);
2+
const truncate = require(`./truncate`);
3+
const md2html = require(`./md2html`);
4+
5+
const formatText = (data, code) => {
6+
let message = `<a href="https://codebottle.io/s/${data.id}">${data.title}</a> `;
7+
message += `by <a href="https://codebottle.io/users/${data.username}">${data.username}</a>\n\n`;
8+
9+
if (code) {
10+
message += `<pre>${escapeHtml(truncate(data.code))}</pre>`;
11+
} else {
12+
message += md2html(
13+
escapeHtml(
14+
truncate(
15+
data.description || `No description.`
16+
)
17+
)
18+
);
19+
}
20+
21+
return message;
22+
};
23+
24+
module.exports = formatText;

helpers/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
inlineButtons: require(`./inlineButtons`),
3+
formatText: require(`./formatText`),
4+
};

helpers/inlineButtons.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Markup = require(`telegraf/markup`);
2+
3+
const inlineButtons = (id, code) =>
4+
Markup.inlineKeyboard([
5+
Markup.callbackButton(code ? `Code` : `Description`, `${code ? `code` : `desc`}_${id}`),
6+
Markup.urlButton(`Open`, `https://codebottle.io/s/${id}`),
7+
]).extra({
8+
disable_web_page_preview: true,
9+
});
10+
11+
module.exports = inlineButtons;

helpers/md2html.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const md2html = text => {
2+
return text
3+
.replace(/\*{2}(.+?)\*{2}/g, `<i>$1</i>`)
4+
.replace(/\*(.+?)\*/g, `<b>$1</b>`)
5+
.replace(/`{3}\w*\n(.+?)\n`{3}/gs, `<pre>$1</pre>`)
6+
.replace(/`(.+?)`/g, `<code>$1</code>`)
7+
.replace(/\[(.+?)\]\((.+?)\)/g, `<a href="$2">$1</a>`);
8+
};
9+
10+
module.exports = md2html;

helpers/truncate.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const truncate = code => code.length > 1000 ? `${code.slice(0, 1000)}...` : code;
2+
3+
module.exports = truncate;

0 commit comments

Comments
 (0)