Node-telegram-bot-api: callback for bot.sendmessage

Created on 10 Jul 2017  路  6Comments  路  Source: yagop/node-telegram-bot-api

I have this code and i want to recive a callback for sendMessage
can u help me to find callback for sendMessage

var register = function (chatId, from, command) {
var member = {
        fname:command,
        tCode:from.id,
        tFname:from.first_name,
        tLname:from.last_name
    }
    member.fname=command;
bot.sendMessage(chatId, 'insert your last name : ')
.then(payload => {
            console.log(payload)
        })
        .catch(function (error) {
            console.log(error);
        });
}
question

Most helpful comment

@milad145 I think you want to register on a reply to message. If that's the case, then your code will misbehave horribly if bot is used by more than one user (highly likely).

You probably want to use onReplyToMessage, like this:

// not tested, I probably made some small mistake somewhere
bot.sendMessage(chatId, 'insert a name', {
    reply_markup: {
        force_reply: true
    }
}).then(payload => {
    const replyListenerId = bot.onReplyToMessage(payload.chat.id, payload.message_id, msg => {
        bot.removeReplyListener(replyListenerId)
        console.log(msg) // here's the reply which is I think what you want
    })
})

All 6 comments

What do you mean as "callback"? Callback is a function which will be executed after sendMessage(), but you use .then() and execute code

i want to catch sendMessage callback. how can i catch that?

for inlineKeybord message we can use this

bot.sendMessage(chatId, "select answer", {
reply_markup: {
inline_keyboard: [{text:'text1',callback_data:'data1'},{text:'text2',callback_data:'data2'}],
},
})

.then(() => {
bot.once("callback_query", answer => {
console.log(answer) //this is my callback
})
})

but for text message how we can catch callback

bot.sendMessage(fromId, 'What should I search for?')
.then(() => {
// callback for sendMessage
})

I do it

 bot.sendMessage(chatId, 'insert a name')
    .then(payload` => {
        bot.once('message', (msg) => {
                       console.log(msg) // callback for sendMessage
               });
    })

@milad145 I think you want to register on a reply to message. If that's the case, then your code will misbehave horribly if bot is used by more than one user (highly likely).

You probably want to use onReplyToMessage, like this:

// not tested, I probably made some small mistake somewhere
bot.sendMessage(chatId, 'insert a name', {
    reply_markup: {
        force_reply: true
    }
}).then(payload => {
    const replyListenerId = bot.onReplyToMessage(payload.chat.id, payload.message_id, msg => {
        bot.removeReplyListener(replyListenerId)
        console.log(msg) // here's the reply which is I think what you want
    })
})

@GingerPlusPlus
thank you, it was very usefull
but for inline_keyboard what we can do?
how we can pass chatId to callback_query

bot.once("callback_query", answer => {
  console.log(answer) 
})

@milad145 Chat id is in the message field: https://core.telegram.org/bots/api#callbackquery

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jacopocappelli1989 picture jacopocappelli1989  路  4Comments

Hostile picture Hostile  路  3Comments

qunatumwhiskey picture qunatumwhiskey  路  4Comments

mbrammer picture mbrammer  路  3Comments

Dohoon-Kim picture Dohoon-Kim  路  3Comments