Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
whatsva
Advanced tools
Readme
Whatsva fully uses nodejs without Selenium or any other browser to connect to WhatsApp Web. how to work whatsva via WebSocket and connect to Whatsapp Web. that way it can save ram usage on the server.
npm install whatsva
Then import in your code using: const WAWeb = require('whatsva')
const client = new WAWeb()
client.connect()
.then (([user, chats, contacts, unread]) => {
console.log ("oh hello " + user.name + " (" + user.id + ")")
console.log ("you have " + unread.length + " unread messages")
console.log ("you have " + chats.length + " chats")
})
.catch (err => console.log("unexpected error: " + err) )
If the connection is successful, you will see a QR code printed on your terminal screen, scan it with WhatsApp on your phone and you'll be logged in!
If you don't want to wait for WhatsApp to send all your chats while connecting, you can use the following function:
const client = new WAWeb()
client.connectSlim() // does not wait for chats & contacts
.then (user => {
console.log ("oh hello " + user.name + " (" + user.id + ")")
client.receiveChatsAndContacts () // wait for chats & contacts in the background
.then (([chats, contacts, unread]) => {
console.log ("you have " + unread.length + " unread messages")
console.log ("you have " + chats.length + " chats")
})
})
.catch (err => console.log("unexpected error: " + err))
Handling Events Implement the following callbacks in your code:
Called when you have a pending unread message or recieve a new message
client.setOnUnreadMessage (m => {
const [notificationType, messageType] = client.getNotificationType(m) // get what type of notification it is -- message, group add notification etc.
console.log("got notification of type: " + notificationType) // message, groupAdd, groupLeave
console.log("message type: " + messageType) // conversation, imageMessage, videoMessage, contactMessage etc.
}, false) // set to `true` if you want to receive outgoing messages that may be sent from your phone
Called when you recieve an update on someone's presence, they went offline or online
client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type))
Called when your message gets delivered or read
client.setOnMessageStatusChange (json => {
let sent = json.to
if (json.participant) // participant exists when the message is from a group
sent += " ("+json.participant+")" // mention as the one sent to
// log that they acknowledged the message
console.log(sent + " acknowledged message(s) " + json.ids + " as " + json.type + " at " + json.timestamp)
})
Called when the connection gets disconnected (either the server loses internet or the phone gets unpaired)
client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) )
Sending Messages It's super simple
Send text messages using
client.sendTextMessage(id, "oh hello there!")
Send text messages & quote another message using
const options = {quoted: quotedMessage}
client.sendTextMessage(id, "oh hello there", options)
quotedMessage
is a message object
Send a location using
client.sendLocationMessage(id, 24.121231, 55.1121221) // the latitude, longitude of the location
Send a contact using
// format the contact as a VCARD
const vcard = 'BEGIN:VCARD\n' // metadata of the contact card
+ 'VERSION:3.0\n'
+ 'FN:Jeff Singh\n' // full name
+ 'ORG:Ashoka Uni;\n' // the organization of the contact
+ 'TEL;type=CELL;type=VOICE;waid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number
+ 'END:VCARD'
client.sendContactMessage(id, "Jeff", vcard)
Send a media (image, video, sticker, pdf) message using
const buffer = fs.readFileSync("example/ma_gif.mp4") // load some gif
const options = {gif: true, caption: "hello!"} // some metadata & caption
client.sendMediaMessage(id, buffer, WAWeb.MessageType.video, options)
ffmpeg
installed on your systemmediaBuffer
is just a Buffer containing the contents of the media you want to sendmediaType
represents the type of message you are sending. This can be one of the following:
[
WAWeb.MessageType.image, // an image message
WAWeb.MessageType.video, // a video message
WAWeb.MessageType.audio, // an audio message
WAWeb.MessageType.sticker // a sticker message
]
The last parameter when sending messages is info
, a JSON object, providing some information about the message. It can have the following optional values:
info = {
caption: "hello there!", // (for media messages) the caption to send with the media (cannot be sent with stickers though)
thumbnail: "23GD#4/==", /* (for location & media messages) has to be a base 64 encoded JPEG if you want to send a custom thumb,
or set to null if you don't want to send a thumbnail.
Do not enter this field if you want to automatically generate a thumb
*/
mimetype: "application/pdf", /* (for media messages) specify the type of media (optional for all media types except documents),
for pdf files => set to "application/pdf",
for txt files => set to "application/txt"
etc.
*/
gif: true, // (for video messages) if the video should be treated as a GIF
quoted: quotedMessage, // the message you want to quote (can be used with sending all kinds of messages)
timestamp: Date() // optional, if you want to manually set the timestamp of the message
}
id
is the WhatsApp id of the person or group you're sending the message to.
It must be in the format [country code][phone number]@s.whatsapp.net
, for example +19999999999@s.whatsapp.net
for people. For groups, it must be in the format 123456789-123345@g.us
. Do not attach @c.us
for individual people IDs, It won't work
client.sendReadReceipt(id, messageID)
The id is in the same format as mentioned earlier. The message ID is the unique identifier of the message that you are marking as read. On a message object, it can be accessed using messageID = message.key.id
.
client.updatePresence(id, WAWeb.Presence.available)
This lets the person/group with id
know whether you're online, offline, typing etc. where presence
can be one of the following:
[
WAWeb.Presence.available, // online
WAWeb.Presence.unavailable, // offline
WAWeb.Presence.composing, // typing...
WAWeb.Presence.recording // recording...
]
client.setOnUnreadMessage (m => {
const messageType = client.getMessageType(m.message) // get what type of message it is -- text, image, video
// if the message is not a text message
if (messageType !== WAWeb.MessageType.text && messageType !== WAWeb.MessageType.extendedText) {
client.decodeMediaMessage(m.message, "filename") // extension applied automatically
.then (meta => console.log(m.key.remoteJid + " sent media, saved at: " + meta.filename))
.catch (err => console.log("error in decoding message: " + err))
}
}
const authJSON = client.base64EncodedAuthInfo()
Then you can use this JSON to log back in without needing to scan a QR code using
const authJSON = JSON.parse( fs.readFileSync("auth_info.json") )
client.connect (authJSON)
.then (([user, chats, contacts, unread]) => console.log ("yay connected"))
client.isOnWhatsApp ("xyz@c.us")
.then (([exists, id]) => console.log(id + (exists ? " exists " : " does not exist") + "on WhatsApp"))
client.loadConversation ("xyz-abc@g.us", 25) // query the last 25 messages (replace 25 with the number of messages you want to query)
.then (messages => console.log("got back " + messages.length + " messages"))
You can also load the entire conversation history if you want
client.loadEntireConversation ("xyz@c.us", (message) => console.log("Loaded message with ID: " + message.key.id))
.then (() => console.log("queried all messages")) // promise resolves once all messages are retreived
client.getStatus ("xyz@c.us") // leave empty to get your own
.then ((json, q) => console.log("status: " + json.status))
client.getProfilePicture ("xyz@g.us") // leave empty to get your own
.then (([json, q]) => console.log("download profile picture from: " + json.eurl))
client.requestPresenceUpdate ("xyz@c.us")
// the presence update is fetched and called here
client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type))
Of course, replace xyz
with an actual ID. Also, append @c.us
for individuals & @g.us
for groups.
client.groupMetadata ("abcd-xyz@g.us")
.then (([json, _]) => console.log(json.id + ", title: " + json.subject + ", description: " + json.desc))
client.groupCreate ("My Fab Group", ["abcd@s.whatsapp.net", "efgh@s.whatsapp.net"]) // title & participants
.then (([json, _]) => {
console.log ("created group with id: " + json.gid)
client.sendTextMessage(json.gid, "hello everyone") // say hello to everyone on the group
})
client.groupAdd ("abcd-xyz@g.us", ["abcd@s.whatsapp.net", "efgh@s.whatsapp.net"]) // id & people to add to the group
.then (([json, _]) => console.log("added successfully: " + (json.status===200)))
client.groupMakeAdmin ("abcd-xyz@g.us", ["abcd@s.whatsapp.net", "efgh@s.whatsapp.net"]) // id & people to make admin
.then (([json, _]) => console.log("made admin successfully: " + (json.status===200)))
client.groupLeave ("abcd-xyz@g.us")
.then (([json, _]) => console.log("left group successfully: " + (json.status===200)))
client.groupInviteCode ("abcd-xyz@g.us")
.then (code => console.log(code))
#- Note we are not affiliated with WhatsApp. It is written for educational purposes only. Use at your own discretion.
FAQs
whatsapp API by kejarkoding
The npm package whatsva receives a total of 35 weekly downloads. As such, whatsva popularity was classified as not popular.
We found that whatsva demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.