const { Extra, Markup } = require('telegraf');
// Validate number between 1-100
const isBetween100 = number => {
let valid = false;
if (typeof number === 'number') {
if (1 <= number && number <= 100) {
valid = true;
}
}
return valid;
};
module.exports = bot => {
bot.command('test', async (ctx, next) => {
// /foo 123456789 opts="foo"
bot.hears(/^(\/foo|\/bar|\/baz) (?:([0-9]+)|([0-9]+) ([\w\W]+\S))$/, async (ctx, next) => {
// /foo|!foo|/bar|!bar foo bar quality="30"
bot.hears(/^(\/foo|!foo|\/bar|!bar) (?:([.+\S]+)|([.+\S]+) ([\w\W]+\S))$/, async (ctx, next) => {
const match = ctx.match;
const ban = match[1] === '/foo' ? true : false;
const unban = match[1] === '/bar' ? true : false;
const info = match[1] === '/baz' ? true : false;
const matchId = match[2] ? match[2] : match[3];
const matchFoo = (match[4] && match[4].includes('foo')) || false;
const matchBar = (match[4] && match[4].includes('bar')) || false;
const matchOpts = match[4] && (match[4].includes('opts="') || match[4].includes("opts='")) ? true : false;
const opts = matchOpts ? match[4].match(/opts=[\"|\'](.+?)[\"|\']/)[1] : '';
const matchQuality = match[4] && (match[4].includes('quality="') || match[4].includes("quality='")) ? true : false;
const qualityValue = matchQuality && match[4].match(/quality=[\"|\']([0-9]+)[\"|\']/);
const qualityNumber = qualityValue && Number(qualityValue[1]);
const qualityBetween = qualityNumber && isBetween100(qualityNumber);
const quality = qualityBetween && qualityNumber;
if (ctx.chat.id === OWNER_ID) {
const msg = ctx.i18n.t('test');
/*const inline_keyboard = [
[
{
text: '🚀 Go to GitHub',
url: 'https://github.com/illvart',
},
{
text: 'Telegraf',
url: 'https://github.com/telegraf/telegraf',
},
],
[
{
text: 'Google',
url: 'https://google.com',
},
],
[
{
text: 'Hello',
callback_data: 'hello',
},
],
];*/
//const reply_markup = JSON.stringify({inline_keyboard});
await ctx.sendAction('typing');
await ctx
.send(
msg,
/*{
parse_mode: 'markdown',
reply_markup,
reply_to_message_id: ctx.message.message_id,
disable_web_page_preview: true, // false
},*/
Extra.markdown()
.markup(
Markup.inlineKeyboard([
[Markup.urlButton('🚀 Go to GitHub', 'https://github.com/illvart'), Markup.urlButton('Telegraf', 'https://github.com/telegraf/telegraf')],
[Markup.urlButton('Google', 'https://google.com')],
[Markup.callbackButton('Hello', 'hello', Math.random() > 0.5)],
])
)
.inReplyTo(ctx.message.message_id)
.webPreview(true)
)
.then(async res => {
await sleep(8000);
await ctx.del(res.message_id);
})
.catch(err => debug(err, { error: true }));
await ctx
.send(msg, {
parse_mode: 'html',
reply_to_message_id: ctx.message.message_id,
disable_web_page_preview: true,
})
.catch(err => debug(err, { error: true }));
await ctx.send(msg, Extra.HTML().inReplyTo(ctx.message.message_id).webPreview(false)).catch(err => debug(err, { error: true }));
await ctx.send(msg, Extra.markdown().inReplyTo(ctx.message.message_id).webPreview(false)).then(async ({chat, message_id}) => {
await ctx.editText(chat.id, message_id, undefined, msg), Extra.markdown()).then(async ({chat, message_id}) => {
await ctx.sendAction('upload_document');
await ctx.sendDocument({source: buffer, filename: 'foo.png'}, {caption: 'Foo', reply_to_message_id: message_id, parse_mode: 'Markdown'});
});
});
}
});
};
Last active
July 15, 2020 21:24
-
-
Save illvart/6690bbb8c1860b8fcc94b9715b316582 to your computer and use it in GitHub Desktop.
Telegraf.js snippet for Telegram bot Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {Context} = require('telegraf'); | |
class Aliases extends Context { | |
constructor(update, telegram, options) { | |
super(update, telegram, options); | |
this.t = telegram; | |
} | |
sendAction(...args) { | |
return super.replyWithChatAction(...args); | |
} | |
send(...args) { | |
return super.reply(...args); | |
} | |
sendDocument(...args) { | |
return super.replyWithDocument(...args); | |
} | |
editText(...args) { | |
return super.telegram.editMessageText(...args); | |
} | |
del(...args) { | |
return super.deleteMessage(...args); | |
} | |
} | |
module.exports = Aliases; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {Telegraf} = require('telegraf'); | |
const {BOT_TOKEN, BOT_USERNAME} = require('../config'); | |
const {debug} = require('./'); | |
const aliases = require('./aliases'); | |
const bot = new Telegraf(BOT_TOKEN, {username: BOT_USERNAME, contextType: aliases}); | |
// Bot info | |
bot.telegram.getMe().then((res) => debug(res)); | |
module.exports = bot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {readdirSync, readFileSync} = require('fs'); | |
const {Markup} = require('telegraf'); | |
const {safeLoad} = require('js-yaml'); | |
module.exports = (bot) => { | |
bot.command('lang', async (ctx) => { | |
await ctx.send(ctx.i18n.t('lang'), { | |
reply_markup: languageKeyboard(), | |
}); | |
}); | |
bot.action( | |
localesFiles().map((file) => file.split('.')[0]), | |
async (ctx) => { | |
let db = ctx.dbuser; | |
const lang = ctx.callbackQuery.data; | |
if (db.banned.state === false && lang !== db.lang) { | |
db.lang = lang; | |
ctx.i18n.locale(lang); | |
await db.save(); | |
} | |
const message = ctx.callbackQuery.message; | |
await ctx.editText(message.chat.id, message.message_id, undefined, ctx.i18n.t('lang_selected'), { | |
parse_mode: 'markdown', | |
}); | |
} | |
); | |
}; | |
function languageKeyboard() { | |
const locales = localesFiles(); | |
//const result = []; | |
const inline_keyboard = []; | |
locales.forEach((locale, index) => { | |
const localeCode = locale.split('.')[0]; | |
const localeName = safeLoad(readFileSync(`${__dirname}/../../locales/${locale}`, 'utf8')).name; | |
if (index % 2 == 0) { | |
if (index === 0) { | |
//result.push([Markup.callbackButton(localeName, localeCode)]); | |
inline_keyboard.push([ | |
{ | |
text: localeName, | |
callback_data: localeCode, | |
}, | |
]); | |
} else { | |
//result[result.length - 1].push(Markup.callbackButton(localeName, localeCode)); | |
inline_keyboard[inline_keyboard.length - 1].push({ | |
text: localeName, | |
callback_data: localeCode, | |
}); | |
} | |
} else { | |
//result[result.length - 1].push(Markup.callbackButton(localeName, localeCode)); | |
inline_keyboard[inline_keyboard.length - 1].push({ | |
text: localeName, | |
callback_data: localeCode, | |
}); | |
if (index < locales.length - 1) { | |
//result.push([]); | |
inline_keyboard.push([]); | |
} | |
} | |
}); | |
//return Markup.inlineKeyboard(result); | |
return JSON.stringify({inline_keyboard}); | |
} | |
function localesFiles() { | |
return readdirSync(`${__dirname}/../../locales`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment