1

I've a bot.telegram.sendPhoto() with this code:

bot.telegram.sendPhoto(
  channel_id,
  {source: filepath},
  { 
    caption: description.join("\n"), 
    parse_mode: 'MarkdownV2'
  }
)

(description is an array with some text.

So i would to add some buttons and then perform an action but how can i do? I've tried in this way:

const buttons = Markup.keyboard([
            ["Test", "Test2"]
        ]).oneTime().resize().extra()

and then added it into the {...} after parse_mode:

{ 
  caption: description.join("\n"), 
  parse_mode: 'MarkdownV2',
  buttons
}

but it doesn't work. And i tried also after the {...}

{ 
  caption: description.join("\n"), 
  parse_mode: 'MarkdownV2'
},
buttons

but it still doesn't work. So how can i do? Thank you

Mattia
  • 199
  • 1
  • 1
  • 12

1 Answers1

4

Markup.keyboard represents custom-keyboard (see here) for replying text - which can't possibly be used in channels (since members can't send messages in channels).

You probably are looking for inline-keyboards (buttons that are attached at the bottom of messages members can interact with).

Here is how you send inline keyboard in telegraf (example with callback_data buttons):

const buttons = Telegraf.Extra.markup((m) =>
  m.inlineKeyboard([
      [ m.callbackButton('Test', 'test') ],
      [ m.callbackButton('Test 2', 'test2') ]
  ])
)

bot.action('test', async (ctx) => {
  console.log(ctx)
  try {
    await ctx.answerCbQuery();    
  } catch (error) {
  }
})


bot.telegram.sendPhoto(
  channel_id,
  {source: filepath},
  { 
    caption: description.join("\n"), 
    parse_mode: 'MarkdownV2',
    reply_markup: buttons.reply_markup
  }
)

bot.launch()

If you want to use the telegraf/markup module instead, update the code as follows:

const Markup = require('telegraf/markup')

const buttons = Markup.inlineKeyboard([
  [Markup.callbackButton('Test', 'test')],
  [Markup.callbackButton('Test 2', 'test2')]
])

bot.telegram.sendPhoto(
  channel_id, {
    source: filepath
  }, {
    caption: description.join("\n"),
    parse_mode: 'MarkdownV2',
    reply_markup: buttons
  }
)

sample output:

enter image description here

Further Resources:

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • You must be doing something else wrong. The sample output I've attached is the direct result of the sample code I've given. [Edit your question](https://stackoverflow.com/posts/63882563/edit) to add the snippet you've written, or share it via pastebin/Github Gist - so that I can help – Tibebes. M Sep 14 '20 at 12:52
  • no ok sorry my bad i forgot that i added something with the markup and i forgot to escape a dot. It works perfectly. But why it doesn't work if i do Markup.inlineKeyboards? (From require('telegraf/markup');? – Mattia Sep 14 '20 at 16:19
  • 1
    I have added an example for you, please take a look at the updated answer – Tibebes. M Sep 14 '20 at 16:45