Skip to content

Instantly share code, notes, and snippets.

@rizaumami
Created November 13, 2017 01:51
Show Gist options
  • Save rizaumami/4115ef80150a65a544d66d67fdbf62f9 to your computer and use it in GitHub Desktop.
Save rizaumami/4115ef80150a65a544d66d67fdbf62f9 to your computer and use it in GitHub Desktop.
const Telegraf = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
function unescapeHtml(str){
const map = {
amp: '&',
lt: '<',
le: '≤',
gt: '>',
ge: '≥',
quot: '"',
'#039': "'"
}
return str.replace(/&([^;]+);/g, (m, c) => map[c]|| '')
}
function strikethrough(str){
return str.replace(/./g, chr => {
return chr + '\u0336';
});
}
bot.on('inline_query', ctx => {
let results = []
if (ctx.inlineQuery.query) {
const text = ctx.inlineQuery.query.replace(/<br\/>/g, '\n')
results = [
{
type: 'article',
id: `bold_${ctx.inlineQuery.id}`,
title: 'Bold',
input_message_content: {
message_text: `<b>${text}</b>`,
parse_mode: 'HTML'
},
description: 'The text will be bolded',
thumb_url: 'https://i.imgur.com/90jd2L8.png'
},
{
type: 'article',
id: `italic_${ctx.inlineQuery.id}`,
title: 'Italic',
input_message_content: {
message_text: `<i>${text}</i>`,
parse_mode: 'HTML'
},
description: 'The text will be italized',
thumb_url: 'https://i.imgur.com/3FeoOWE.png'
},
{
type: 'article',
id: `mono_${ctx.inlineQuery.id}`,
title: 'Monospace',
input_message_content: {
message_text: `<code>${text}</code>`,
parse_mode: 'HTML'
},
description: 'The text will be monospaced',
thumb_url: 'https://i.imgur.com/s7ctUP8.png'
},
{
type: 'article',
id: `st_${ctx.inlineQuery.id}`,
title: 'Strikethrough',
input_message_content: {
message_text: `${strikethrough(text)}`,
},
description: 'The text will be striked trough',
thumb_url: 'https://i.imgur.com/bTc8EHC.png'
},
{
type: 'article',
id: `custom_${ctx.inlineQuery.id}`,
title: 'Custom',
input_message_content: {
message_text: `${unescapeHtml(text)}`,
parse_mode: 'HTML',
disable_web_page_preview: true
},
description: 'The text will be sent as custom HTML formatted text',
thumb_url: 'https://i.imgur.com/7WgtmoK.png'
}
]
}
return ctx.answerInlineQuery(results).catch(error => {
ctx.answerInlineQuery([{
type: 'article',
id: `error_${ctx.inlineQuery.id}`,
title: '⚠️ Error',
message_text: `${error}`,
description: `${error}`
}])
})
})
const secret = Math.random().toString(36).slice(2)
bot.telegram.setWebhook(`${process.env.NOW_URL}/${secret}`)
bot.startWebhook(`/${secret}`, null, process.env.PORT, process.env.HOST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment