Skip to content

Commit

Permalink
feat(message): add country code validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mimamch committed Oct 2, 2023
1 parent b0c7b8a commit 3398ffb
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wa-multi-session",
"version": "3.3.0",
"version": "3.3.1",
"description": "Multi Session Whatsapp Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
120 changes: 114 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/Utils/phone-to-jid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { WhatsappError } from "../Error";
import { SendMessageTypes } from "../Types";
import { PHONENUMBER_MCC } from "@whiskeysockets/baileys";

const isPhoneNumberValidCountry = (phone: string) => {
return Object.keys(PHONENUMBER_MCC).some((key) => {
return phone.startsWith(key);
});
};

export const phoneToJid = ({
to,
Expand All @@ -10,6 +16,9 @@ export const phoneToJid = ({
}): string => {
if (!to) throw new WhatsappError('parameter "to" is required');
let number = to.toString();
if (!isPhoneNumberValidCountry(number)) {
throw new WhatsappError("phone number must start with valid country code");
}
if (isGroup) {
number = number.replace(/\s|[+]|[-]/gim, "");
if (!number.includes("@g.us")) number = number + "@g.us";
Expand Down

0 comments on commit 3398ffb

Please sign in to comment.