0

In the bot menu, I have an online chat option with me. If a user clicks this button, bot saves this users state as chatMode. The scenario is like if the user's state is chatMode each of his messages should be forwarded to me, and when I reply to this message, my answer should be sent to this user - all of these communications should be vie my bot. This scenario is just like the service bot @LivegramBot. Can you provide a detailed answer for how to achieve this result?

Also, is there any way to group these users because if there are more users, my conversations will be all mixed on my side of the bot?

This is what I am trying:

if (user.state === 'chatMode') {
            bot.forwardMessage(xx4775xxx, msg.from.id, msg.message_id);

        } else if (user.telegramId === xx4775xxx) {
            if (msg.reply_to_message) {
                bot.sendMessage(msg.reply_to_message.chat.id, msg.text)
            }
        }

But here if (msg.reply_to_message) part is sending to myself again.

1 Answers1

-1

Each socket automatically joins a default room by self id. docs: http://socket.io/docs/rooms-and-namespaces/#default-room

So you can emit to a socket by id with following code:

io.to(socketid).emit('message', 'for your eyes only');

if (io.sockets.connected[socketid]) {
    io.sockets.connected[socketid].emit('message', 'for your eyes only');
}

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log(socket.id);
    io.to(socket.id).emit('chat message', msg+' you ID is:'+socket.id);
  });
});
David Shiref
  • 29
  • 1
  • 2