0

在机器人菜单中,我有一个在线聊天选项。如果用户单击此按钮,bot 会将此用户状态保存为chatMode. 这种情况就像用户的状态是chatMode他的每条消息都应该转发给我,当我回复这条消息时,我的答案应该发送给这个用户——所有这些通信都应该通过我的机器人进行。这种情况就像服务机器人@LivegramBot。您能否提供有关如何实现此结果的详细答案?

另外,有没有办法对这些用户进行分组,因为如果有更多用户,我的对话将在我的机器人这边混在一起?

这就是我正在尝试的:

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)
            }
        }

但这里if (msg.reply_to_message)的部分再次发送给自己。

4

1 回答 1

-1

每个套接字通过自身 ID 自动加入一个默认房间。文档:http ://socket.io/docs/rooms-and-namespaces/#default-room

因此,您可以使用以下代码通过 id 向套接字发出:

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);
  });
});
于 2020-04-15T14:00:19.920 回答