WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API
https://baileys.whiskeysockets.io/
MIT License
4.07k stars 1.38k forks source link

[BUG] Can't delete messages from group #1109

Closed henriquedepaularodrigues closed 1 week ago

henriquedepaularodrigues commented 3 weeks ago

Describe the bug Currently i wan't to delete a message from a group programatically but the message is not deleted when i send it in a group.

To Reproduce Steps to reproduce the behavior:

  1. Created a new connection
  2. Send a message with the current chat group in that way:
    const deleteMessage = ( conn: WaSocket,message:WAMessageUpdate)=>{
    conn.sendMessage(message.key.remoteJid, { delete: message.key })
    }
  3. when a message is sent in a group, the message is used in message.upsert event to delete it (due to my own reasons based on the text sent

Expected behavior Is expected the message to be deleted in the group Environment (please complete the following information):

Additional context Add any other context about the problem here. Also had some problems with send messages but with cache implementation, the code works properly

CSFelix commented 2 weeks ago

Try to delete the message like this:

await conn.sendMessage(message.key.remoteJid, { delete: JSON.parse(message.dataJson).key })

dataJson is literally the whole JSON object of the message. Currently, I'm storing it into a database and recovering it when deleting a message.


Besides, make sure that your Baileys is up to date (6.7.9).

bidwolf commented 2 weeks ago

Hey @CSFelix , I'm already on 6.7.9, i get my message data by id via cache, like Redis. But still won't work

bidwolf commented 2 weeks ago

after use sendMessage with a delete in a group I get the message below:

Closing stale open session for new outgoing prekey bundle
Closing session: SessionEntry {
  _chains: {
    BQ2cHxeAvUjn6qIn4m44qqYDJhIE6K98plQdBrcMmIoY: { chainKey: [Object], chainType: 2, messageKeys: [Object] },
    Bc9DNWRehebg3kTINSdRczboEKA1MHL6RVGDYgtaxdsa: { chainKey: [Object], chainType: 1, messageKeys: {} }
  },
  registrationId: 13370,
  currentRatchet: {
    ephemeralKeyPair: {
      pubKey: <Buffer 05 cf 43 35 64 5e 85 e6 e0 de 44 c8 35 27 51 73 36 e8 10 a0 35 30 72 fa 45 51 83 62 0b 5a c5 db 1a>,
      privKey: <Buffer 08 4a cf 6f 9b 3b fa d7 5e 97 17 5a ab 37 58 30 74 12 9d 28 3d 2e 89 77 04 bd 24 a0 ac 9e 68 78>
    },
    lastRemoteEphemeralKey: <Buffer 05 0d 9c 1f 17 80 bd 48 e7 ea a2 27 e2 6e 38 aa a6 03 26 12 04 e8 af 7c a6 54 1d 06 b7 0c 98 8a 18>,
    previousCounter: -1,
    rootKey: <Buffer c2 88 8d d9 3e 58 d0 b2 10 ac c0 eb 0f d3 ab d5 f6 8d d0 33 b9 75 10 94 9a 80 42 f0 85 25 ee 8a>
  },
  indexInfo: {
    baseKey: <Buffer 05 f0 bb f5 fd b7 cd 10 d5 d6 03 19 41 f3 01 72 86 df 73 38 a6 a0 4c 66 bb 4b c8 72 6e 92 9f 1b 73>,
    baseKeyType: 1,
    closed: -1,
    used: 1730833876024,
    created: 1730833876024,
    remoteIdentityKey: <Buffer 05 2b da 0d 41 30 fd 34 12 12 33 7d da 0a ac 76 3d 52 ab 03 8f 5b 32 b7 16 ea ca 90 03 84 c6 1e 7e>
  },
  pendingPreKey: {
    signedKeyId: 11,
    baseKey: <Buffer 05 f0 bb f5 fd b7 cd 10 d5 d6 03 19 41 f3 01 72 86 df 73 38 a6 a0 4c 66 bb 4b c8 72 6e 92 9f 1b 73>,
    preKeyId: 1552
  }
CSFelix commented 2 weeks ago

after use sendMessage with a delete in a group I get the message below:

Closing stale open session for new outgoing prekey bundle
Closing session: SessionEntry {
  _chains: {
    BQ2cHxeAvUjn6qIn4m44qqYDJhIE6K98plQdBrcMmIoY: { chainKey: [Object], chainType: 2, messageKeys: [Object] },
    Bc9DNWRehebg3kTINSdRczboEKA1MHL6RVGDYgtaxdsa: { chainKey: [Object], chainType: 1, messageKeys: {} }
  },
  registrationId: 13370,
  currentRatchet: {
    ephemeralKeyPair: {
      pubKey: <Buffer 05 cf 43 35 64 5e 85 e6 e0 de 44 c8 35 27 51 73 36 e8 10 a0 35 30 72 fa 45 51 83 62 0b 5a c5 db 1a>,
      privKey: <Buffer 08 4a cf 6f 9b 3b fa d7 5e 97 17 5a ab 37 58 30 74 12 9d 28 3d 2e 89 77 04 bd 24 a0 ac 9e 68 78>
    },
    lastRemoteEphemeralKey: <Buffer 05 0d 9c 1f 17 80 bd 48 e7 ea a2 27 e2 6e 38 aa a6 03 26 12 04 e8 af 7c a6 54 1d 06 b7 0c 98 8a 18>,
    previousCounter: -1,
    rootKey: <Buffer c2 88 8d d9 3e 58 d0 b2 10 ac c0 eb 0f d3 ab d5 f6 8d d0 33 b9 75 10 94 9a 80 42 f0 85 25 ee 8a>
  },
  indexInfo: {
    baseKey: <Buffer 05 f0 bb f5 fd b7 cd 10 d5 d6 03 19 41 f3 01 72 86 df 73 38 a6 a0 4c 66 bb 4b c8 72 6e 92 9f 1b 73>,
    baseKeyType: 1,
    closed: -1,
    used: 1730833876024,
    created: 1730833876024,
    remoteIdentityKey: <Buffer 05 2b da 0d 41 30 fd 34 12 12 33 7d da 0a ac 76 3d 52 ab 03 8f 5b 32 b7 16 ea ca 90 03 84 c6 1e 7e>
  },
  pendingPreKey: {
    signedKeyId: 11,
    baseKey: <Buffer 05 f0 bb f5 fd b7 cd 10 d5 d6 03 19 41 f3 01 72 86 df 73 38 a6 a0 4c 66 bb 4b c8 72 6e 92 9f 1b 73>,
    preKeyId: 1552
  }

Hmmmm, it's weird. It looks like something related to the session's prekeys directly. Let's wait to see what others will reply. By the way, are you into Baileys' Discord Server? You can create a topic there about this issue.

bidwolf commented 2 weeks ago

FOUND A SOLUTION @CSFelix

[!NOTE]

My solution used to be like this

A number send a message that i want to delete > I send a reply telling my application to delete that message > my code get >the id of that message in the reply (code below) > search that id in the cache > send a delete (code below too).


if (messageUpdate.message) {
      messageUpdate.getQuotedObj = messageUpdate.getQuotedMessage = async () => {
        if (!messageUpdate.quoted.id) return false;
        const q = await store.loadMessage(messageUpdate.chat, messageUpdate.quoted.id) as ExtendedWAMessageUpdate;
        if (!q) return false;
        return transformMessageUpdate(conn, q, store); // transform the message quoted in a message object like the messageUpdate itself
      };
      messageUpdate.quoted.delete = () => conn.sendMessage(messageUpdate.chat, {
        delete: {
          remoteJid: messageUpdate.chat,
          fromMe: messageUpdate.quoted.fromMe,
          id: messageUpdate.quoted.id,
          participant: messageUpdate.quoted.sender
        }
      });

When you said that you save your messages in a database, and to delete you get messages from there, i though that can be a problem in my store method. So i solve this question by implementing a database Collection for messages in my group.

const mongoose = require('mongoose');

const messageSchema = new mongoose.Schema({
  remoteJid: { type: String, required: true },
  fromMe: { type: Boolean, required: true },
  id: { type: String, required: true },
  participant: { type: String, required: true },
  timestamp: { type: Date, default: Date.now }
});

const Message = mongoose.model('Message', messageSchema);

module.exports = Message;
// in my group schema i added something like :   messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }] // Relacionar mensagens

And then I use this collection to save each message on the group:

const newMessage = new Message({
            remoteJid: parsedMessage.chat,
            fromMe: parsedMessage.fromMe,
            id: parsedMessage.id,
            participant: parsedMessage.participant,
            message: parsedMessage.text,
            timestamp: new Date()
          });
          await newMessage.save();
          const group = await Group.findOne({ key: this.key, groupId: parsedMessage.key.remoteJid }).exec();
          group.messages.push(newMessage._id);
          await group.save();

And in my logic to delete a replied message:

  async deleteQuotedMessage(props: validateCommandProps, messageUpdate: ExtendedWAMessageUpdate) {
    try {
      if (!messageUpdate.quoted) {
        throw new Error('Quoted message not found')
      }

      const group = await Group.findOne({ groupId: messageUpdate.chat }).populate('messages').exec()
      if (!group) {
        throw new Error('Group not found')
      }

      const message = group.messages.find(m => m.id === messageUpdate.quoted.id)
      if (!message) {
        throw new Error('Message not found')
      }

      const currentMessage = await Message.findById(message._id).exec()
      if (!currentMessage) {
        throw new Error('Message not found')
      }

      await props.instance.sendMessage(messageUpdate.chat, {
        delete: {
          remoteJid: currentMessage.remoteJid,
          fromMe: currentMessage.fromMe,
          id: currentMessage.id,
          participant: currentMessage.participant,
        }
      })

      group.messages = group.messages.filter(m => m.id !== currentMessage.id)
      await currentMessage.delete()
      await group.save()
    } catch (error) {
      this.logger.error(error)
    }
  }

1000136696

I would like to thank you @CSFelix ! You help me way more than you think.