Skip to content

Commit

Permalink
Switch to WPP (object store from wppconnect-team/wa-js) for verifying…
Browse files Browse the repository at this point in the history
… WhatsApp contacts and send messages.
  • Loading branch information
limshengli committed Sep 11, 2023
1 parent 71478a4 commit 99a83db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 183 deletions.
75 changes: 34 additions & 41 deletions src/lib/wapi/functions/check-number-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,16 @@ export async function checkNumberStatus(id, conn = false) {
throw err;
}
}
const lid = await WAPI.getChat(id);
if (lid) {
if (id.endsWith('@g.us')) {
return await WPP.group
.ensureGroup(lid.id)
.then((result) => {
if (!!result && typeof result === 'object') {
const data = {
status: 200,
numberExists: true,
id: result.id,
};
return data;
}
throw Object.assign(err, {
connection: connection,
numberExists: false,
text: `The number does not exist`
});
})
.catch((err) => {
if (err.text) {
throw err;
}
throw Object.assign(err, {
connection: connection,
numberExists: false,
text: err
});
});;
}

return await WPP.contact
.queryExists(lid.id)
if (id.endsWith('@g.us')) {
return await WPP.group
.ensureGroup(id)
.then((result) => {
if (typeof result === 'object') {
if (!!result && typeof result === 'object') {
const data = {
status: 200,
numberExists: true,
id: result.wid
id: result.id
};
return data;
}
Expand All @@ -82,15 +52,38 @@ export async function checkNumberStatus(id, conn = false) {
text: err
});
});
} else {
throw Object.assign(err, {
connection: connection,
numberExists: false
});
}

return await WPP.contact
.queryExists(id)
.then((result) => {
if (!!result && typeof result === 'object') {
const data = {
status: 200,
numberExists: true,
id: result.wid
};
return data;
}
throw Object.assign(err, {
connection: connection,
numberExists: false,
text: `The number does not exist`
});
})
.catch((err) => {
if (err.text) {
throw err;
}
throw Object.assign(err, {
connection: connection,
numberExists: false,
text: err
});
});
} catch (e) {
return {
status: e.error,
status: e.error === undefined ? 500 : e.error,
text: e.text,
numberExists: e.numberExists,
connection: e.connection
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/send-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export function sendImage(
status = false,
passId
) {
return sendFile(imgBase64, chatid, filename, caption, type, status, passId);
return WPP.chat.sendFileMessage(chatid, imgBase64, {createChat: true, mimetype: type, caption: caption, filename: filename});
}
142 changes: 1 addition & 141 deletions src/lib/wapi/functions/send-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,145 +7,5 @@ export async function sendMessage(
forcingReturn = false,
delSend = true
) {
if (status && content.length > 700) {
return WAPI.scope(undefined, true, null, 'Use a maximum of 700 characters');
}

if (
status === false &&
(typeof content != 'string' || content.length === 0)
) {
return WAPI.scope(
undefined,
true,
null,
'It is necessary to write a text!'
);
}

if (status == false && (typeof to != 'string' || to.length === 0)) {
return WAPI.scope(to, true, 404, 'It is necessary to number');
}

const chat = checkNumber
? await WAPI.sendExist(to)
: await WAPI.returnChat(to);

if (chat && chat.status != 404 && chat.id) {
const t = status != false ? 'sendStatusText' : 'sendText';
const m = { type: t, text: content };
const newMsgId = !passId
? await window.WAPI.getNewMessageId(chat.id._serialized, checkNumber)
: await window.WAPI.setNewMessageId(passId, checkNumber);
const fromwWid = await Store.MaybeMeUser.getMaybeMeUser();

let inChat = await WAPI.getchatId(chat.id).catch(() => {
return WAPI.scope(chat.id, true, 404, 'Error to number ' + to);
});

if (inChat) {
chat.lastReceivedKey && chat.lastReceivedKey._serialized
? (chat.lastReceivedKey._serialized = inChat._serialized)
: '';
chat.lastReceivedKey && chat.lastReceivedKey.id
? (chat.lastReceivedKey.id = inChat.id)
: '';
}

if (!newMsgId) {
return WAPI.scope(to, true, 404, 'Error to gerate newId');
}

const message = {
id: newMsgId,
ack: 0,
body: content,
from: fromwWid,
to: chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
type: 'chat'
};

if (forcingReturn) {
if (delSend) {
while (true) {
const connection = Store.State.Socket.state;
if (connection === 'CONNECTED') {
const result = await Store.addAndSendMsgToChat(chat, message);
await WAPI.sleep(5000);
const statusMsg = chat.msgs._models.filter(
(e) => e.id === newMsgId._serialized && e.ack > 0
);
if (statusMsg.length === 0) {
await WAPI.deleteMessages(to, [newMsgId._serialized]);
} else {
let obj = WAPI.scope(
newMsgId,
false,
WAPI._serializeForcing(result),
content
);
Object.assign(obj, m);
return obj;
}
}
}
} else {
const result = await Store.addAndSendMsgToChat(chat, message);
let obj = WAPI.scope(
newMsgId,
false,
WAPI._serializeForcing(result),
content
);
Object.assign(obj, m);
return obj;
}
}

try {
const result = (
await Promise.all(Store.addAndSendMsgToChat(chat, message))
)[1];

if (
result === 'success' ||
result === 'OK' ||
result.messageSendResult === 'OK'
) {
const obj = WAPI.scope(newMsgId, false, result, content);
Object.assign(obj, m);
return obj;
}
if (result === 'ERROR_UNKNOWN' && to.includes('@g.us')) {
const obj = WAPI.scope(
newMsgId,
true,
result,
'Could not send message to this group, possibly you have been removed'
);
Object.assign(obj, m);
return obj;
}
} catch (result) {
let res = result;
if (result?.contact?.id) {
res = result.contact.id;
}
if (result?.message) {
res.message = result.message;
}
const obj = WAPI.scope(newMsgId, true, res, 'The message was not sent');
Object.assign(obj, m);
return obj;
}
} else {
if (!chat.erro) {
chat.erro = true;
}
return chat;
}
return WPP.chat.sendTextMessage(to, content, {createChat: checkNumber});
}

0 comments on commit 99a83db

Please sign in to comment.