Context
Variable yang menghandle update pesan dari Telegram dan metod-metod respon cepat terhadapnya
4 menit dibaca
Dimana itu ctx
?
Lihatlah code berikut:
bot.cmd('ping', ctx => ctx.reply('Pong!'));
Keterangan:
ctx
adalah context itu.reply
adalah method (shorthand) untuk menghandle pesan
Berikut kerangka isi dari ctx.
Context
Context dibuat per request dan berisi beberapa properti:
Property | Description |
---|---|
ctx.telegram |
Telegram client instance |
ctx.tg |
alias ctx.telegram |
ctx.updateType |
Update type (message, inline_query, etc.) |
[ctx.updateSubTypes] |
Update subtypes (text, sticker, audio, etc.) |
[ctx.message] |
Received message |
[ctx.editedMessage] |
Edited message |
[ctx.inlineQuery] |
Received inline query |
[ctx.chosenInlineResult] |
Received inline query result |
[ctx.callbackQuery] |
Received callback query |
[ctx.shippingQuery] |
Shipping query |
[ctx.preCheckoutQuery] |
Pre-checkout query |
[ctx.channelPost] |
New incoming channel post of any kind — text, photo, sticker, etc. |
[ctx.editedChannelPost] |
New version of a channel post that is known to the bot and was edited |
[ctx.poll] |
New version of a anonymous poll that is known to the bot and was changed |
[ctx.pollAnswer] |
This object represents an answer of a user in a non-anonymous poll. |
[ctx.chat] |
Current chat info |
[ctx.from] |
Sender info |
[ctx.match] |
Regex match |
[ctx.broadcast] |
update type bot.on |
Shortcuts
Context shortcuts for message update:
Context shortcuts for callback_query update:
Shortcut | Bound to | Available | Params |
---|---|---|---|
addStickerToSet |
telegram.addStickerToSet |
||
answerCbQuery |
telegram.answerCbQuery |
text, showAlert, extra |
|
answerGameQuery |
telegram.answerGameQuery |
url |
|
createNewStickerSet |
telegram.createNewStickerSet |
||
deleteChatPhoto |
telegram.deleteChatPhoto |
||
deleteMessage |
telegram.deleteMessage |
||
deleteStickerFromSet |
telegram.deleteStickerFromSet |
||
editMessageCaption |
telegram.editMessageCaption |
caption, extra |
|
editMessageMedia |
telegram.editMessageMedia |
media, extra |
|
editMessageReplyMarkup |
telegram.editMessageReplyMarkup |
markup |
|
editMessageText |
telegram.editMessageText |
text, extra |
|
exportChatInviteLink |
telegram.exportChatInviteLink |
||
forwardMessage |
telegram.forwardMessage |
||
getChat |
telegram.getChat |
||
getChatAdministrators |
telegram.getChatAdministrators |
||
getChatMember |
telegram.getChatMember |
||
getChatMembersCount |
telegram.getChatMembersCount |
||
getStickerSet |
telegram.getStickerSet |
||
leaveChat |
telegram.leaveChat |
||
pinChatMessage |
telegram.pinChatMessage |
||
reply |
telegram.sendMessage |
text, extra |
|
replyWithAnimation |
telegram.sendAnimation |
animation, extra |
|
replyWithAudio |
telegram.sendAudio |
||
replyWithChatAction |
telegram.sendChatAction |
||
replyWithDice |
telegram.sendDice |
||
replyWithDocument |
telegram.sendDocument |
||
replyWithGame |
telegram.sendGame |
||
replyWithHTML |
telegram.sendMessage |
text, extra |
|
replyWithInvoice |
telegram.sendInvoice |
||
replyWithLocation |
telegram.sendLocation |
||
replyWithMarkdown |
telegram.sendMessage |
text, extra |
|
replyWithMediaGroup |
telegram.sendMediaGroup |
||
replyWithPhoto |
telegram.sendPhoto |
||
replyWithPoll |
telegram.sendPoll |
||
replyWithSticker |
telegram.sendSticker |
||
replyWithVideo |
telegram.sendVideo |
||
replyWithVideoNote |
telegram.sendVideoNote |
||
replyWithVoice |
telegram.sendVoice |
||
setChatDescription |
telegram.setChatDescription |
||
setChatPhoto |
telegram.setChatPhoto |
||
setChatTitle |
telegram.setChatTitle |
||
setStickerPositionInSet |
telegram.setStickerPositionInSet |
sticker, position |
|
setStickerSetThumb |
telegram.setStickerSetThumb |
||
stopPoll |
telegram.stopPoll |
||
unpinChatMessage |
telegram.unpinChatMessage |
||
unpinAllChatMessages |
telegram.unpinAllChatMessages |
||
uploadStickerFile |
telegram.uploadStickerFile |
||
unbanChatMember |
telegram.unbanChatMember |
||
replyIt |
telegram.sendMessage |
text, extra |
|
replyItWithAudio |
telegram.sendAudio |
(v3.6) | audio, extra |
replyItWithDocument |
telegram.sendDocument |
(v3.6) | doc, extra |
replyItWithHTML |
telegram.sendMessage |
(v3.6) | text, extra |
replyItWithLocation |
telegram.sendLocation |
(v3.6) | lat, lon, extras |
replyItWithMarkdown |
telegram.sendMessage |
(v3.6) | text, extra |
replyItWithPhoto |
telegram.sendPhoto |
(v3.6) | photo, extra |
replyItWithSticker |
telegram.sendSticker |
(v3.6) | sticker, extra |
replyItWithVideo |
telegram.sendVideo |
(v3.6) | video, extra |
Context shortcuts for inline_query update:
Shortcut | Bound to |
---|---|
answerInlineQuery |
telegram.answerInlineQuery |
Context shortcuts for shipping_query update:
Shortcut | Bound to |
---|---|
answerShippingQuery |
telegram.answerShippingQuery |
Context shortcuts for pre_checkout_query update:
Shortcut | Bound to |
---|---|
answerPreCheckoutQuery |
telegram.answerPreCheckoutQuery |
Example
Check in this code:
const bot = new lumpia.init(token)
bot.command('quit', (ctx) => {
// Explicit usage
ctx.telegram.leaveChat(ctx.message.chat.id)
})
bot.on('text', (ctx) => {
// Explicit usage
ctx.telegram.sendMessage(ctx.message.chat.id, `Echo: ${ctx.message.text}`)
})
bot.on('callback_query', (ctx) => {
// Explicit usage
ctx.telegram.answerCbQuery(ctx.callbackQuery.id)
})
bot.on('inline_query', (ctx) => {
const result = []
// Explicit usage
ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, result)
})
const bot = new lumpia.init(token)
bot.command('quit', (ctx) => {
// Explicit usage
ctx.tg.leaveChat(ctx.chat.id)
})
bot.on('text', (ctx) => {
// Explicit usage
ctx.tg.sendMessage(ctx.chat.id, `Echo: ${ctx.message.text}`)
})
bot.on('callback_query', (ctx) => {
// Explicit usage
ctx.tg.answerCbQuery(ctx.callbackQuery.id)
})
bot.on('inline_query', (ctx) => {
const result = []
// Explicit usage
ctx.tg.answerInlineQuery(ctx.inlineQuery.id, result)
})
const bot = new lumpia.init(token)
bot.command('quit', (ctx) => {
// Using context shortcut
ctx.leaveChat()
})
bot.on('text', (ctx) => {
// Using context shortcut
ctx.reply(`Echo: ${ctx.message.text}`)
})
bot.on('callback_query', (ctx) => {
// Using context shortcut
ctx.answerCbQuery()
})
bot.on('inline_query', (ctx) => {
const result = []
// Using context shortcut
ctx.answerInlineQuery(result)
})
Kesinambungan
Adalah konsep jika dimasukkan sebuah trigger, jika trigger ditemukan maka tetep diteruskan ke trigger selanjutnya.
Untuk dapat berkesinambungan, perlu dijalankan fungsi next()
pada context.
const bot = new lumpia.init(token)
bot.cmd('ping', (ctx) => {
// User typing: /ping
ctx.reply('pong!')
})
// tidak akan dieksekusi
bot.cmd('ping', (ctx) => {
// User typing: /ping
ctx.reply('pong 2!');
})
const bot = new lumpia.init(token)
bot.cmd('ping', (ctx, next) => {
// User typing: /ping
ctx.reply('pong!');
next();
})
// perintah tetap dieksekusi
bot.cmd('ping', (ctx) => {
// User typing: /ping
ctx.reply('pong 2!')
})