102 lines
2.7 KiB
TypeScript
102 lines
2.7 KiB
TypeScript
import { Bot, InlineKeyboard } from 'grammy'
|
||
import open from 'open'
|
||
|
||
//Store bot screaming status
|
||
let screaming = false
|
||
|
||
//Create a new bot
|
||
const bot = new Bot(process.env.BOT_API_TOKEN)
|
||
|
||
//This function handles the /scream command
|
||
bot.command('scream', () => {
|
||
screaming = true
|
||
})
|
||
|
||
//This function handles /whisper command
|
||
bot.command('whisper', () => {
|
||
screaming = false
|
||
})
|
||
|
||
//Pre-assign menu text
|
||
const firstMenu =
|
||
'<b>Добро пожаловать</b>\n\nЭтот бот позволит вам отправлять \nотложенные сообщения в чаты.'
|
||
const secondMenu =
|
||
'<b>Добро пожаловать</b>\n\nДля этого, вам нужно выбрать чат \nи указать текст сообщения и даты отправки.'
|
||
|
||
//Pre-assign button text
|
||
const nextButton = 'Далее'
|
||
const backButton = 'Назад'
|
||
const tutorialButton = 'Обучение'
|
||
|
||
//Build keyboards
|
||
const firstMenuMarkup = new InlineKeyboard().text(nextButton)
|
||
|
||
const secondMenuMarkup = new InlineKeyboard()
|
||
.text(backButton)
|
||
.text(tutorialButton)
|
||
|
||
//This handler sends a menu with the inline buttons we pre-assigned above
|
||
bot.command('menu', async (ctx: any) => {
|
||
await ctx.reply(firstMenu, {
|
||
parse_mode: 'HTML',
|
||
reply_markup: firstMenuMarkup
|
||
})
|
||
})
|
||
|
||
//This handler processes back button on the menu
|
||
bot.callbackQuery(backButton, async (ctx: any) => {
|
||
//Update message content with corresponding menu section
|
||
try {
|
||
await ctx.editMessageText(firstMenu, {
|
||
reply_markup: firstMenuMarkup,
|
||
parse_mode: 'HTML'
|
||
})
|
||
} catch (error) {
|
||
console.log(error)
|
||
}
|
||
})
|
||
|
||
//This handler processes next button on the menu
|
||
bot.callbackQuery(nextButton, async (ctx: any) => {
|
||
//Update message content with corresponding menu section
|
||
try {
|
||
await ctx.editMessageText(secondMenu, {
|
||
reply_markup: secondMenuMarkup,
|
||
parse_mode: 'HTML'
|
||
})
|
||
} catch (error) {
|
||
console.error(error)
|
||
}
|
||
})
|
||
|
||
//This handler processes tutorial button on the menu
|
||
bot.callbackQuery(tutorialButton, async (ctx: any) => {
|
||
//Open link in browser
|
||
try {
|
||
await open('https://core.telegram.org/bots/tutorial')
|
||
} catch (error) {
|
||
console.error(error)
|
||
}
|
||
})
|
||
|
||
//This function would be added to the dispatcher as a handler for messages coming from the Bot API
|
||
bot.on('message', async (ctx: any) => {
|
||
//Print to console
|
||
console.log(
|
||
`${ctx.from.first_name}: ${'text' in ctx.message ? ctx.message.text : ''}`
|
||
)
|
||
|
||
if (screaming && ctx.message.text) {
|
||
//Scream the message
|
||
await ctx.reply(ctx.message.text.toUpperCase(), {
|
||
entities: ctx.message.entities
|
||
})
|
||
} else {
|
||
//This is equivalent to forwarding, without the sender's name
|
||
await ctx.copyMessage(ctx.message.chat.id)
|
||
}
|
||
})
|
||
|
||
//Start the Bot
|
||
bot.start()
|