Created
August 11, 2022 07:34
-
-
Save mcpeblocker/14c09d00ff9f43091325e183c974eb7a to your computer and use it in GitHub Desktop.
Telegraf frameworki yordamida foydalanuvchilar boshqalarga yuborgan havolalarni yoki xabarlarni sanab borish.
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
// @BotFather orqali inline mode va inline feedback featurelari yoqilishi shart. | |
const TOKEN = ""; | |
const { Telegraf, Markup } = require('telegraf'); | |
const bot = new Telegraf(TOKEN); | |
let links = [ | |
{ id: 1, url: 'https://google.com' }, | |
{ id: 2, url: 'https://twitter.com' } | |
]; | |
bot.command('link', (ctx) => { | |
// get random link | |
let id = Math.ceil(Math.random() * links.length); | |
let keyboard = Markup.inlineKeyboard([ | |
Markup.button.switchToChat("Havolani ulashish", id) | |
]); | |
ctx.reply("Marhamat, havolani ulashishingiz mumkin", keyboard); | |
}); | |
bot.on('inline_query', (ctx) => { | |
// extract id from query | |
let id = +ctx.inlineQuery.query; | |
if (typeof id !== 'number') return; | |
// find link | |
let link = links.find(l => l.id === id); | |
if (!link) return; | |
// answer | |
ctx.answerInlineQuery([{ | |
id, | |
type: 'article', | |
title: ${id}-havolani ulashish, | |
input_message_content: { | |
message_text: Salom. Men bu havolani topdim!\n\n${link.url} | |
} | |
}]); | |
}) | |
bot.on('chosen_inline_result', (ctx) => { | |
let user = ctx.from; | |
let linkId = +ctx.chosenInlineResult.query; | |
let link = links.find(l => l.id === linkId); | |
if (!link) return; | |
// you can increment counter here | |
ctx.telegram.sendMessage(user.id, Siz ${link.url} havolasini yubordingiz!); | |
}) | |
bot.launch().then(() => { | |
console.log("Started"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment