Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to receive a reply message with ReplyToMessage #113

Closed
akaspi opened this issue Apr 27, 2016 · 20 comments
Closed

Failed to receive a reply message with ReplyToMessage #113

akaspi opened this issue Apr 27, 2016 · 20 comments
Labels

Comments

@akaspi
Copy link

akaspi commented Apr 27, 2016

Hi,
I'm trying to run a simple test code according to the replyToMessage example and my replyToMessage callback is never called.

var TelegramBot = require('node-telegram-bot-api');
var bot = new TelegramBot(config.botToken, {polling: {timeout: 1, interval: 100}});

var opts = {
    reply_markup: JSON.stringify({ force_reply: true }
)};

bot.onText(/\/search/, function (msg) {
    var fromId = msg.from.id;
    bot.sendMessage(fromId, 'What should I search for?', opts)
        .then(function(sended) {
            var chatId = sended.chat.id;
            var messageId = sended.message_id;
            bot.onReplyToMessage(chatId, messageId, function (message) {
                console.log('OK. I\'ll search for %s', message.text);
            });
        })
});

console.log (in onReplyToMessage callback) is never called...

Am I doing something wrong?

  • I'm using 0.21.1
@akaspi akaspi changed the title Failed to use receive a reply messageon with ReplyToMessage Failed to use receive a reply message with ReplyToMessage Apr 27, 2016
@DenisIzmaylov
Copy link

ping

@mishachesnokov
Copy link

mishachesnokov commented May 2, 2016

Same problem. Only if I manually reply on the message, then callback is being called.

@akaspi
Copy link
Author

akaspi commented May 3, 2016

@mishachesnokov - what do you mean by 'manually replay on the message'?

@akaspi akaspi changed the title Failed to use receive a reply message with ReplyToMessage Failed to receive a reply message with ReplyToMessage May 3, 2016
@mishachesnokov
Copy link

mishachesnokov commented May 4, 2016

@akaspi - when I tap on bot's message in chat and press "Reply" then console.log gets my answer. But that's not what I want.

@akaspi
Copy link
Author

akaspi commented May 4, 2016

@mishachesnokov - Thanks. I wasn't familiar with this tapping on bot's message feature....
So, I believe what I need is that my following message after the bot's messages would invoke the callback.

However, if you think of it, the following message after the bot's message won't always be the replay.

edit:
According to the spec, the force_replay property should enforce the following message of the use to be considered as a reply.

@mishachesnokov
Copy link

@akaspi - That's the purpose of 'force_reply'. But it doesn't seem to work properly. I suppose the next message after bot's message is a user's reply, since if I asked something I need the answer now - before we proceed further.

Currently I am using another lib for nodejs - there it works fine.

@firo
Copy link

firo commented May 4, 2016

@mishachesnokov - Which node lib do you currently use ? thanks!

@mishachesnokov
Copy link

@firo - https://github.com/mdibaiee/node-telegram-api But the author says he can't put enough time in it now. Anyway the things he has done let me do enough. Just a little bit worry about telegram future updates.

@zigzag-way
Copy link

The same problem. Can not get the reply. My main msg handler recieves the msg but it doesnt look like a response. Response handler is nerver called.

@raphaklaus
Copy link

raphaklaus commented May 20, 2016

Palliative: I'm dealing this way:

You can create a class, and put it within.

  regexifyOptions(array){
    return new RegExp(array.toString().replace(/,/g, '|'));
  }

  reply(options){
    return new Promise((resolve, reject) => {
      try {
        bot.onText(this.regexifyOptions(options), (msg, response) => {
          if (msg.chat.id === id) 
            resolve(response[0]);
          else 
            return this.reply(options, id);
        });
      } catch (error) {
        reject(error);
      }
    });
  }

@zigzag-way
Copy link

Whats this?

@raphaklaus
Copy link

@zigzag-way is a class.

@Kaos1337
Copy link

@raphaklaus there is a problem, you can't add a listener when needed and remove it if not needed, because if 2 (or more) users at the same time are "enabling" it, the first that end the usage avoid to the second user to use it (the added listener).

@raphaklaus
Copy link

@Kaos1337, thanks for the advice. I edited the code to keep the callbacks, and check user id.

@bosslee
Copy link

bosslee commented Oct 7, 2016

You can create a class, and put it within.

HI @raphaklaus
May I ask in which file did you add the class code?
is it the telegram.js?

Thank you

@raphaklaus
Copy link

Please check my replies in this issue: #108

@GochoMugo
Copy link
Collaborator

As @mishachesnokov suggests above, the callback will only be fired if the user manually replies to it! Otherwise, you will have to implement, in your application code, a way to maintain a conversation (implies state) with your user!

However, I believe proper documentation will help users avoid this pitfall! We will close this issue once we update our docs!

@GochoMugo GochoMugo added the docs label Jan 6, 2017
@GochoMugo
Copy link
Collaborator

We have added a common pitfalls section in our docs! Please find more information there!

@makah
Copy link

makah commented Apr 20, 2020

I solved this problem by creating a onNextMessage(chatId, callback)

Create function onNextMessage()

this.bot = new TelegramBot(token, options);

this.bot.nextMessage = {};
this.bot.onNextMessage = (chatId, callback) => {
    let promise = new Promise((resolve) => {
        this.bot.nextMessage[chatId] = { callback: callback, next: resolve };
    });
    return promise;
}

Then you need to listen to any Message:

bot.on('message', (msg) => {
  const chatId = msg.chat.id;

  let nextMsg = bot.nextMessage[chatId];
  if (nextMsg) {
    nextMsg.callback(msg);
    nextMsg.next(msg);
    bot.nextMessage[chatId] = undefined;
  }
});    

Finally:

bot.onText(/\/chat$/, (msg) => {
  const chatId = msg.chat.id;
  let fullName = '';

  let resp = `May I take down your name, ${msg.chat.username}`;
  bot.sendMessage(chatId, resp).then(() => {
    return bot.onNextMessage(chatId, msg => fullName = msg.text);
  }).then(() => {
    let resp = `It's a pleasure to meet you ${fullName}`;
    bot.sendMessage(chatId, resp)
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

11 participants