Skip to content

Commit

Permalink
feat: add support for switchToChat and switchToCurrentChat
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Sep 10, 2018
1 parent 26cb6a1 commit 32a242d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ Updates menu when doFunc() resolved.

### `menu.urlButton(text, url, {hide, joinLastRow})`

Url button. This button is just a pass thru and has no affect on the actionCode system.
Url button. This button is just a pass through and has no effect on the actionCode system.

`text` and `url` can be `string` or `function(ctx)`.
`hide(ctx)` (optional) can hide the button when return is true.

### `menu.switchToChatButton(text, value, {hide, joinLastRow})`
### `menu.switchToCurrentChatButton(text, value, {hide, joinLastRow})`

Switch buttons. These buttons are just pass throughs and don't have an effect on the actionCode system.

`text` and `value` can be `string` or `function(ctx)`.
`hide(ctx)` (optional) can hide the button when return is true.

### `menu.submenu(text, menu, {hide, joinLastRow})`

Creates a Button in the menu to a submenu
Expand Down
6 changes: 5 additions & 1 deletion build-keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function buildKeyboardRow(row, ctx) {
return result
}

async function buildKeyboardButton({text, textPrefix, actionCode, url, hide}, ...args) {
async function buildKeyboardButton({text, textPrefix, actionCode, url, switchToChat, switchToCurrentChat, hide}, ...args) {
if (hide) {
hide = await hide(...args)
if (hide) {
Expand Down Expand Up @@ -46,6 +46,10 @@ async function buildKeyboardButton({text, textPrefix, actionCode, url, hide}, ..
buttonWithPromises.callback_data = actionCode
} else if (url) {
buttonWithPromises.url = url
} else if (switchToChat) {
buttonWithPromises.switch_inline_query = switchToChat
} else if (switchToCurrentChat) {
buttonWithPromises.switch_inline_query_current_chat = switchToCurrentChat
} else {
throw new Error('button was not completly intialized')
}
Expand Down
11 changes: 10 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mainMenu.urlButton('EdJoPaTo.de', 'https://edjopato.de')

mainMenu.button('test', 'Do nothing', () => {})

mainMenu.switchToCurrentChatButton('Interessant', 'nope')

const eventMenu = new TelegrafInlineMenu('e', 'Hier gibts Events')
let someValue = false
eventMenu.toggle('t', 'toggle me', (ctx, newState) => {
Expand Down Expand Up @@ -141,7 +143,14 @@ bot.command('test', ctx => ctx.reply('test', Extra.markup(

bot.action(/.+/, ctx => ctx.reply('action not handled: ' + ctx.match[0]))

bot.use(ctx => ctx.reply('something not handled'))
bot.use(ctx => {
if (ctx.updateType === 'inline_query') {
// This bot example has no inline mode.
// The switchToCurrentChatButton example will trigger it and fail
return
}
return ctx.reply('something not handled')
})

bot.catch(error => {
if (error.description === 'Bad Request: message is not modified') {
Expand Down
19 changes: 16 additions & 3 deletions telegraf-inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,29 @@ class TelegrafInlineMenu {
}

urlButton(text, url, {hide, joinLastRow} = {}) {
if (!hide) {
hide = () => false
}
this.addButton({
text,
url,
hide
}, !joinLastRow)
}

switchToChatButton(text, value, {hide, joinLastRow} = {}) {
this.addButton({
text,
switchToChat: value,
hide
}, !joinLastRow)
}

switchToCurrentChatButton(text, value, {hide, joinLastRow} = {}) {
this.addButton({
text,
switchToCurrentChat: value,
hide
}, !joinLastRow)
}

submenu(text, submenu, {hide, joinLastRow} = {}) {
if (!hide) {
hide = () => false
Expand Down

0 comments on commit 32a242d

Please sign in to comment.