WebWhatsapp-Wrapper icon indicating copy to clipboard operation
WebWhatsapp-Wrapper copied to clipboard

WAPI.sendMessageToID does not work

Open kivlanzein opened this issue 3 years ago • 59 comments

please help me, why sendMessageToID not work? , i can't send messages to the new number??

please help me..

kivlanzein avatar Jan 29 '22 05:01 kivlanzein

same issue.

mraakashjoshi avatar Jan 31 '22 06:01 mraakashjoshi

// use this code window.WAPI.sendMessageToID2 = function (id, message, done) { try { // Create user var idx = new window.Store.UserConstructor(id, {intentionallyUsePrivateConstructor: true}); // Get chat var chat = window.Store.Chat.find(idx); // Send message chat._value.sendMessage(message); } catch (e) { return false;

}
if (done !== undefined) done(false);
return false;

}; //window.WAPI.sendMessageToID2 ('[email protected]','oi');

duzaq avatar Jan 31 '22 12:01 duzaq

// use this code window.WAPI.sendMessageToID2 = function (id, message, done) { try { // Create user var idx = new window.Store.UserConstructor(id, {intentionallyUsePrivateConstructor: true}); // Get chat var chat = window.Store.Chat.find(idx); // Send message chat._value.sendMessage(message); } catch (e) { return false;

}
if (done !== undefined) done(false);
return false;

}; //window.WAPI.sendMessageToID2 ('[email protected]','oi');

not sending on new number on which there is no chat history

mraakashjoshi avatar Feb 01 '22 10:02 mraakashjoshi

// use this code window.WAPI.sendMessageToID2 = function (id, message, done) { try { // Create user var idx = new window.Store.UserConstructor(id, {intentionallyUsePrivateConstructor: true}); // Get chat var chat = window.Store.Chat.find(idx); // Send message chat._value.sendMessage(message); } catch (e) { return false;

}
if (done !== undefined) done(false);
return false;

}; //window.WAPI.sendMessageToID2 ('[email protected]','oi');

this was the solution I found, and the sending to new numbers is working

duzaq avatar Feb 01 '22 13:02 duzaq

// use this code window.WAPI.sendMessageToID2 = function (id, message, done) { try { // Create user var idx = new window.Store.UserConstructor(id, {intentionallyUsePrivateConstructor: true}); // Get chat var chat = window.Store.Chat.find(idx); // Send message chat._value.sendMessage(message); } catch (e) { return false;

}
if (done !== undefined) done(false);
return false;

}; //window.WAPI.sendMessageToID2 ('[email protected]','oi');

this was the solution I found, and the sending to new numbers is working

getting this error javascript error: Unexpected token 'if'

mraakashjoshi avatar Feb 02 '22 06:02 mraakashjoshi

@duzaq Please share latest wapi.js

Parveen-Mothsara avatar Feb 02 '22 07:02 Parveen-Mothsara

@duzaq Please share latest wapi.js

just add below line in WAPI.sendMessageToID in try quote

var chat = window.Store.Chat.find(id); if(chat._value.sendMessage(message)){done(true);return true;}

no need of WAPI.sendMessageToID2

mraakashjoshi avatar Feb 02 '22 08:02 mraakashjoshi

// use this code window.WAPI.sendMessageToID2 = function (id, message, done) { try { // Create user var idx = new window.Store.UserConstructor(id, {intentionallyUsePrivateConstructor: true}); // Get chat var chat = window.Store.Chat.find(idx); // Send message chat._value.sendMessage(message); } catch (e) { return false;

}
if (done !== undefined) done(false);
return false;

}; //window.WAPI.sendMessageToID2 ('[email protected]','oi');

this was the solution I found, and the sending to new numbers is working

getting this error javascript error: Unexpected token 'if'

just add below line in WAPI.sendMessageToID in try quote

var chat = window.Store.Chat.find(id); if(chat._value.sendMessage(message)){done(true);return true;}

no need of WAPI.sendMessageToID2

mraakashjoshi avatar Feb 02 '22 08:02 mraakashjoshi

just add below line in WAPI.sendMessageToID in try quote

var chat = window.Store.Chat.find(id); if(chat._value.sendMessage(message)){done(true);return true;}

no need of WAPI.sendMessageToID2

mraakashjoshi avatar Feb 02 '22 08:02 mraakashjoshi

this worked for me.

ruanduarte avatar Feb 22 '22 11:02 ruanduarte

just add below line in WAPI.sendMessageToID in try quote

var chat = window.Store.Chat.find(id); if(chat._value.sendMessage(message)){done(true);return true;}

no need of WAPI.sendMessageToID2

It doesn't work on New id.How to resolve it?

fly2012 avatar Feb 27 '22 07:02 fly2012

@fly2012 I have the same problem, do u solve it?

santirlopez1124 avatar Mar 01 '22 20:03 santirlopez1124

Hello, with the code bellow i can send messages to new ids without problem. There's just one weird thing about this function that its always returns "false". But the message is sent successfuly.

window.WAPI.sendMessageToID = function (id, message, done) {
    try {
        window.getContact = (id) => {
            return Store.WapQuery.queryExist(id);
        }
        window.getContact(id).then(contact => {
            if (contact.status === 404) {
                done(true);
            } else {
                if(typeof contact.jid === 'undefined') {
                        contact.jid = {
                            '_serialized' : id,
                            'server' : '@' + id.split('@')[1],
                            'user' : id.split('@')[0]
                        }
                 }
                Store.FindChat.findChat(contact.jid).then(chat => {
                    chat.sendMessage(message);
                    return true;
                }).catch(reject => {
                    if (WAPI.sendMessage(id, message)) {
                        done(true);
                        return true;
                    }else{
                        done(false);
                        return false;
                    }
                });
            }
        });
    } catch (e) {
        if (window.Store.Chat.length === 0)
            return false;

        firstChat = Store.Chat.models[0];
        var originalID = firstChat.id;
        firstChat.id = typeof originalID === "string" ? id : new window.Store.UserConstructor(id, { intentionallyUsePrivateConstructor: true });
        if (done !== undefined) {
            firstChat.sendMessage(message).then(function () {
                firstChat.id = originalID;
                done(true);
            });
            return true;
        } else {
            firstChat.sendMessage(message);
            firstChat.id = originalID;
            return true;
        }
    }
    if (done !== undefined) done(false);
    return false;
}

ruanduarte avatar Mar 02 '22 16:03 ruanduarte

Hi @ruanduarte, did u check it in whatsapp business multidevice version? Because it doesn't work to me

santirlopez1124 avatar Mar 02 '22 17:03 santirlopez1124

@santirlopez1124, no, I didn't. I'm using the normal version without multidevice. :/

ruanduarte avatar Mar 03 '22 11:03 ruanduarte

I solved like this:

  1. Add the line with FindChat in neededObjects
if (!window.Store) {
  (function () {
    function getStore(modules) {
      let foundCount = 0;
      let neededObjects = [
          ...,
          { id: "FindChat", conditions: (module) => module.findChat ? module : module.default && module.default.findChat ? module.default : null }
     ]
     ....
  1. Replace your getChat implementation with this one
window.WAPI.getChat = function (id, done) {
 
  // New version WhatsApp Beta Multi Device
  if (WAPI.isMultiDeviceVersion()) {
    let chat = window.Store.Chat.get(id);
    if (chat) {
      if (chat.sendMessage) {
        if (done) done(chat);
        return chat;
      } else {
        if (done) done(chat._value);
        return chat._value;
      }
    } else {
      // Create user
      var idx = new window.Store.UserConstructor(id, { intentionallyUsePrivateConstructor: true });

      window.Store.FindChat.findChat(idx).then(chat => {
        if (done) done(chat);
      }).catch(e => {
        if (done) done(null);
      })

      return undefined;
    }
  } else
  // Old version
  {
    id = typeof id == "string" ? id : id._serialized;
    const found = window.Store.Chat.get(id);
    found.sendMessage = (found.sendMessage) ? found.sendMessage : function () { return window.Store.sendMessage.apply(this, arguments); };
    if (done !== undefined) done(found);
    return found;
  }
}
  1. This is my isMultiDeviceVersion method
window.WAPI.isMultiDeviceVersion = function () {
  try {
    let resp = window.Store.FeatureChecker.GK.features['MD_BACKEND'];
    return resp;
  } catch {
    return false;
  }
}
  1. My sendMessageToID method
window.WAPI.sendMessageToID = function (id, message, done) {

  try {
    // New version WhatsApp Beta
    if (WAPI.isMultiDeviceVersion()) {
      WAPI.getChat(id, chat => {
        if (chat) {
          chat.sendMessage(message);
          done(true);
          return true;
        } else {
          done(false);
          return false;
        }
      });
    } else {
      // Old version of WhatsApp
      try {
        window.getContact = (id) => {
          return Store.WapQuery.queryExist(id);
        }
        window.getContact(id).then(contact => {
          if (contact.status === 404) {
            done(true);
          } else {
            Store.Chat.find(contact.jid).then(chat => {
              chat.sendMessage(message);
              done(true);
              return true;
            }).catch(reject => {
              if (WAPI.sendMessage(id, message)) {
                done(true);
                return true;
              } else {
                done(false);
                return false;
              }
            });
          }
        });
      } catch (e) {
        if (window.Store.Chat.length === 0) {
          done(false);
          return false;
        }

        firstChat = Store.Chat.models[0];
        var originalID = firstChat.id;
        firstChat.id = typeof originalID === "string" ? id : new window.Store.UserConstructor(id, { intentionallyUsePrivateConstructor: true });
        if (done !== undefined) {
          firstChat.sendMessage(message).then(function () {
            firstChat.id = originalID;
            done(true);
          });
          return true;
        } else {
          firstChat.sendMessage(message);
          firstChat.id = originalID;
          done(true);
          return true;
        }
      }
    }
  } catch (e) {
    done(false);
    return false;
  }
}

vilsonei avatar Mar 05 '22 20:03 vilsonei

Vilson ney, It worked for me! Tk you very much for your solution Did you have had any number banned after send message to new Id?

dekkardnexus5 avatar Mar 08 '22 21:03 dekkardnexus5

@vilsonei, hello! I tried to use the algorithm that you suggest, but I came across the fact that in all cases a message is sent, even if the number is not registered in WA. Is this how it should be, or did I make some mistake?

alexeykirichek avatar Mar 09 '22 11:03 alexeykirichek

Vilson ney, It worked for me! Tk you very much for your solution Did you have had any number banned after send message to new Id?

Not yet! I believe that WhatsApp has lowered the sensitivity of robots in the latest version.

vilsonei avatar Mar 09 '22 14:03 vilsonei

@vilsonei, hello! I tried to use the algorithm that you suggest, but I came across the fact that in all cases a message is sent, even if the number is not registered in WA. Is this how it should be, or did I make some mistake?

I do like this:

BWAPI.enviarMensagem = (numero, msg, resolve) => {
  WAPI.checkNumberStatus(numero, data => {

    // Se o status é 200 envia a mensagem
    // if (data.status === 200) {
    if (data && data.status === 200) {
      numero = data.id._serialized;
      WAPI.sendMessageToID(numero, msg, data => {
        resolve(true);
      })

    } else
    // Caso contrário tente utilizar com o nono digito
    if ( numero.length < 18 ) {
      let numeroNonoDigito = window.BWAPI.addNonoDigito(numero);
      window.BWAPI.enviarMensagem(numeroNonoDigito, msg, resolve)
    } else {
      resolve(false);
    }
  });
}

BWAPI.addNonoDigito = (numero) => {
	return numero.substring(0,4) + '9' + numero.substring(4, numero.length);
}

vilsonei avatar Mar 09 '22 14:03 vilsonei

Vilsonei, wich version do you use as base from your script?

If is possible, could you share your complete wapi.js?

I'm trying make your changes but I thing my initial version is not the same.

pablodosreis avatar Mar 09 '22 14:03 pablodosreis

Vilsonei, wich version do you use as base from your script?

If is possible, could you share your complete wapi.js?

I'm trying make your changes but I thing my initial version is not the same.

This is my lasted customized version: Link for download

There are some test codes I forgot to remove 😅

vilsonei avatar Mar 09 '22 14:03 vilsonei

@vilsonei How can I check whether the WA account exists using this script? "Store.QueryExist.queryExist("[email protected]")" is no wrok

RDKclick avatar Mar 11 '22 03:03 RDKclick

Receving this message when calling sendMessage sendMessage JavaScript stack: TypeError: Cannot read properties of undefined (reading 'then') at Object.window.WAPI.sendMessage (eval at executeScript

luidiva avatar Mar 11 '22 05:03 luidiva

@vilsonei How can I check whether the WA account exists using this script? "Store.QueryExist.queryExist("[email protected]")" is no wrok

Yes! Try this using my custom script:

WAPI.checkNumberStatus(numero, data => {

    // if status 200 send message
    // if (data.status === 200) {
    if (data && data.status === 200) {
      numero = data.id._serialized;
      WAPI.sendMessageToID(numero, msg, data => {
        resolve(true);
      })

    } else {
      resolve(false);
    }
  });

See my custom script here.

vilsonei avatar Mar 11 '22 10:03 vilsonei

Receving this message when calling sendMessage sendMessage JavaScript stack: TypeError: Cannot read properties of undefined (reading 'then') at Object.window.WAPI.sendMessage (eval at executeScript

Are you using this script? See script

vilsonei avatar Mar 11 '22 11:03 vilsonei

Hi @vilsonei I merged your script into mine, and tried to use it with multi-devices,

For some reason, after scanning the QR code, I get an error asking me if my computer has internet. I'm running it from Azure, and if I'm trying to go to another website it's obviously working.

Do you have any idea what can be the error?

roigreenberg avatar Mar 11 '22 11:03 roigreenberg

Hi @vilsonei I merged your script into mine, and tried to use it with multi-devices,

For some reason, after scanning the QR code, I get an error asking me if my computer has internet. I'm running it from Azure, and if I'm trying to go to another website it's obviously working.

Do you have any idea what can be the error?

Do you see any errors in the browser's debug console?

vilsonei avatar Mar 11 '22 11:03 vilsonei

No. I now noticed that if I delete the local storage and rescan the QR, it works, but after a restart, it happens again.

roigreenberg avatar Mar 11 '22 11:03 roigreenberg

No. I now noticed that if I delete the local storage and rescan the QR, it works, but after a restart, it happens again.

You should analyze the moment you are injecting WAPI.js

WAPI.js should only be injected after WhatsApp is fully loaded so that the context variables are not lost or injected incorrectly.

vilsonei avatar Mar 11 '22 12:03 vilsonei