yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.38k stars 1.52k forks source link

new-inline-keyboards Bot 2.0 #109

Closed nullifye closed 8 years ago

nullifye commented 8 years ago

does this module support new inline keyboard right now?

icdevin commented 8 years ago

You can set inline buttons with static URLs but you can't respond to callback_query POSTs. Watch #102.

edlvj commented 8 years ago

@icdevin Can you add code examples of using InlineKeyboardMarkup and InlineKeyboardButton?

icdevin commented 8 years ago

Sure thing. Note that the inline keyboard property is an array of arrays where each inner array is a row of buttons, so the below example gives 3 row-width buttons. You can make one nested array with 3 objects inside to make a single row with 3 buttons inside it.

Also note callback_data is a string containing up to 32 bytes.

var options = {
  reply_markup: JSON.stringify({
    inline_keyboard: [
      [{ text: 'Some button text 1', callback_data: '1' }],
      [{ text: 'Some button text 2', callback_data: '2' }],
      [{ text: 'Some button text 3', callback_data: '3' }]
    ]
  })
};
bot.sendMessage(msg.chat.id, 'Some text giving three inline buttons', options).then(function (sended) {
  // `sended` is the sent message.
});

Results in: [ Some button text 1 ] [ Some button text 2 ] [ Some button text 3 ]

As opposed to:

inline keyboard: [
  [{ text: 'Some button text 1', callback_data: '1' }, { text: 'Some button text 2', callback_data: '2' }, { text: 'Some button text 3', callback_data: '3' }]
]

Which results in: [ Some button text 1 ][ Some button text 2 ][ Some button text 3 ]

Inline button callbacks are not given in normal message responses, so in order to receive this inline button callback query data, you'll need to modify this node-telegram-bot-api to receive a new type of response, callback_query, in addition to message, chosen_inline_result, and so on (so you would be able to do bot.on('callback_query)...`.

Scram99 commented 8 years ago

Cool, that is exactly what i was looking for. But if I understand you correctly this lib "node-telegram-bot-api" does not support the "bot.on('callback_query)" at the moment!? :-( That's such a pity. Will that be implemented one day? Unfortunately I'm not skilled enough to do this.

edlvj commented 8 years ago

@icdevin thnx for reply

icdevin commented 8 years ago

I forked @tjhorner and added a few lines to support this on my branch here. You can use that but it's still missing some (one?) group-level feature and tests. Otherwise just watch #102.

nullifye commented 8 years ago

how to use your fork @icdevin? ive already cloned you fork but cannot start.

telegram.js:17 class TelegramBot extends EventEmitter { ^^^^^ SyntaxError: Unexpected reserved word at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) at Object. (/var/www/pendaftar/index.js:1:80) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32)

anything to do with fsevents? im using centOS by the way

icdevin commented 8 years ago

You need to use the version of telegram.js in the lib directory, not the src directory. If there is no lib directory, run npm run-script prepublish to create it. You will need to run an npm install before you can do that, if you haven't already.

nullifye commented 8 years ago

thanx @icdevin... working great. hope @yagop can merge this PR

Proxiweb commented 8 years ago

@icdevin I've installed your fork and managed to display keyboard. But I haven't found the way to get callback_data ... I've tried bot.on('callback_query', () => ...) but it doesn't work. a spinner is shown on the keyboard when I select it, but I can't get any data... can you provide an example ?

icdevin commented 8 years ago

@Proxiweb I'm using it just like that with OK results (it is running in a live bot).

// Inline button callback queries
bot.on('callback_query', function (msg) {
  console.log(msg); // msg.data refers to the callback_data
  bot.answerCallbackQuery(msg.id, 'Ok, here ya go!');
});

callback_query-type messages are only sent on inline keyboard buttons, nothing else. So in my case, the preceding message sending looks like this:

var options = {
  parse_mode: 'Markdown',
  disable_web_page_preview: true,
  reply_markup: createCinemaListInlineKeyboardMarkup(cinemas)
};
bot.sendMessage(msg.chat.id, createCinemaListMarkup(cinemas), options);

// . . .

function createCinemaListInlineKeyboardMarkup (cinemas) {
  return JSON.stringify({
    inline_keyboard: cinemas.map(function (cinema) {
      return [{
        text: `${cinema.brand} ${cinema.name}`,
        callback_data: `${cinema.source}:${cinema.code}:${cinema.area}:${cinema.groupnum}`
      }];
    })
  });
}
Proxiweb commented 8 years ago

@icdevin thanks a lot, it works fine now.

zigzag-way commented 8 years ago

I updated my Node to the latest one:

node -p process.versions.v8 5.0.71.35 node -v v6.1.0

... but still get errors: node_modules\node-telegram-bot-api\src\telegram.js:20 static messageTypes = [ ^ SyntaxError: Unexpected token =

Im sure its because of ES6... but dont understant how to fix it.

nullifye commented 8 years ago

read @icdevin answer here https://github.com/yagop/node-telegram-bot-api/issues/109#issuecomment-214145640

zigzag-way commented 8 years ago

As I understood I need Babel to be installed. There no info about it here. But it also doesnt help me (((

babel -d ./lib src

SyntaxError: src/telegram.js: Unexpected token (20:22) 18 | 19 | // Telegram message events

20 | static messageTypes = [ | ^ 21 | 'text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', 22 | 'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title', 23 | 'new_chat_photo', 'delete_chat_photo', 'group_chat_created'

Also I dont have LIB directory. I dont understand what is going on. There was a nice plugin and everything worked fine untill I decided to update it from @tjhorner tfork... Very strange type of update... Still dont know how to run it.

paOol commented 8 years ago

@icdevin

following your inline keyboard example

bot.sendMessage(fromId, 'Some text giving three inline buttons', options)
    .then(function (sended) {
      console.log(sended);
    });

how do you see the callback_data ? and what do you do with it after?

Ideally I would like to grab some parameters like such.

-User types /command -bot responds with inline keyboard [ desktop, tablet, mobile], -user picks one -bot responds with more [highest, medium, smallest], -user picks again -bot asks for a URL -user enters url -bot does stuff based on the given parameters

Kaos1337 commented 8 years ago

I need an example like @paOol too.

Anti-Zionist commented 8 years ago

hi , NewInlineKeyboardButtonsWithUrl