Naltox / telegram-node-bot

Node module for creating Telegram bots.
MIT License
720 stars 144 forks source link

How to send some date to the runInlineMenu callback? #195

Open dangear opened 6 years ago

dangear commented 6 years ago

Have generate inlineMenu from some json-array. Code:

var sectionTitles=[];
var layoutList=[];
for(data in someArray){
     sectionTitles.push({
          text: data,
          callback: (mes) => {
               console.log(data)
          }
     })
     layoutList.push('1');
}   
tg.runInlineMenu(tgUser, 'sendMessage', 'Press some button:', {}, sectionTitles, layoutList)

Buttons name from JSON-array put correctly. But in console.log show me "undefined". How I can send data from JSON-array in runInlineMenu callback?

RobotCharly commented 6 years ago

I have same question, you fix it?

dangear commented 6 years ago

Yes. But use other way: I use callback_data field when build buttons array with some parameter (id, short_string and other) and then check callbacks with callbackQueries. Something like this:

var kb=[]
for(var i=0;i<5;i++){
  kb.push([{text:i, callback_data:i}]) //generate buttons array with callback_data field
}
$.sendMessage('Some message', {
  reply_markup: JSON.stringify({
    inline_keyboard: kb //input buttons
  })
});

and then

tg.callbackQueries(($) => { //wait callbacks
  console.log($.data) //then find what you need
})

It's work for me.

RobotCharly commented 6 years ago

Nice, Thanks a lot. In my code instead of the "i" I'm using the controller's and command /yes that is in the tutorial. You have some idea how does this work? I want to go from the keyboard button to another command/controller with different code.

dangear commented 6 years ago

I think you can use _callback_data_ with /command and args (if need it). Something like this: {text:'Button text', callback_data:'/command, args'}; And then parse it in callbackQueries method. I'm not shure that is correct way. But I think it's can work.