Giter Site home page Giter Site logo

venom's Introduction

πŸ•·Venom BotπŸ•Έ

enter image description here

Venom is a high-performance system developed with JavaScript to create a bot for WhatsApp, support for creating any interaction, such as customer service, media sending, sentence recognition based on artificial intelligence and all types of design architecture for WhatsApp.

πŸ•·πŸ•· Functions VenomπŸ•·πŸ•·

Automatic QR Refresh βœ”
Send text, image, video, audio and docs βœ”
Get contacts, chats, groups, group members,Block List βœ”
Send contacts βœ”
Send stickers βœ”
Multiple Sessions βœ”
Forward Messages βœ”
Receive message βœ”
πŸ“ Send location!! (beta) βœ”
πŸ•ΈπŸ•Έ and much more βœ”

Installation

> npm i --save venom-bot

Getting started

// Supports ES6
// import { create, Whatsapp } from 'venom-bot';
const venom = require('venom-bot');

venom.create().then((client) => start(client));

function start(client) {
  client.onMessage((message) => {
    if (message.body === 'Hi') {
      client.sendText(message.from, 'Welcome Venom πŸ•·');
    }
  });
}
After executing create() function, venom will create an instance of whatsapp web. If you are not logged in, it will print a QR code in the terminal. Scan it with your phone and you are ready to go!
Venom will remember the session so there is no need to authenticate everytime.
Multiples sessions can be created at the same time by pasing a session name to create() function:
// Init sales whatsapp bot
venom.create('sales').then((salesClient) => {...});

// Init support whatsapp bot
venom.create('support').then((supportClient) => {...});

Optional create parameters

Venom create() method third parameter can have the following optional parameters:

create('sessionName', qrCallback, statusFind, {
  headless: true, // Headless chrome
  devtools: false, // Open devtools by default
  useChrome: true, // If false will use Chromium instance
  debug: false, // Opens a debug session
  logQR: true, // Logs QR automatically in terminal
  browserArgs: [''], // Parameters to be added into the chrome browser instance
  refreshQR: 15000, // Will refresh QR every 15 seconds, 0 will load QR once. Default is 30 seconds
  autoClose: 60000, // Will auto close automatically if not synced, 'false' won't auto close. Default is 60 seconds (#Important!!! Will automatically set 'refreshQR' to 1000#)
  disableSpins: true, // Will disable Spinnies animation, useful for containers (docker) for a better log
});

Callback Status Session

Gets the return if the session is isLogged or if it is notLogged

create('sessionName', qrCallback, (statusFind) => {
  console.log(statusFind);
})
  .then((client) => {
    start(client);
  })
  .catch((erro) => console.log(erro));

Exporting QR Code

By default QR code will appear on the terminal. If you need to pass the QR somewhere else heres how:

const fs = require('fs');

// Second create() parameter is the QR callback
venom.create('sessionMarketing', (base64Qr, asciiQR) => {
  // To log the QR in the terminal
  console.log(asciiQR);

  // To write it somewhere else in a file
  exportQR(base64Qr, 'marketing-qr.png');
});

// Writes QR in specified path
function exportQR(qrCode, path) {
  qrCode = qrCode.replace('data:image/png;base64,', '');
  const imageBuffer = Buffer.from(qrCode, 'base64');

  // Creates 'marketing-qr.png' file
  fs.writeFileSync(path, imageBuffer);
}

Downloading Files

Puppeteer takes care of the file downloading. The decryption is being done as fast as possible (outruns native methods). Supports big files!

import fs = require('fs');
import mime = require('mime-types');

client.onMessage( async (message) => {
  if (message.isMedia == true) {
    const buffer = await client.decryptFile(message); 
    // At this point you can do whatever you want with the buffer
    // Most likely you want to write it into a file
    const fileName = `some-file-name.${mime.extension(message.mimetype)}`;
    await fs.writeFile(fileName, buffer, (err) => {
      ...
    });
  }
});

Basic Functions (usage)

Not every available function is listed, for further look, every function available can be found in here and here

Chatting

Here, chatId could be <phoneNumber>@c.us or <phoneNumber>-<groupId>@c.us
//Automatically sends a link with the auto generated link preview. You can also add a custom message to be added.
await client.sendLinkPreview("[email protected]", "https://www.youtube.com/watch?v=V1bFr2SWP1I", "Link title");

// Send basic text
await client.sendText(chatId, 'πŸ‘‹ Hello from venom!');

// Send image
await client.sendImage(
  chatId,
  'path/to/img.jpg',
  'image-name.jpg',
  'Caption text'
);

// Send @tagged message
await client.sendMentioned(chatId, 'Hello @5218113130740 and @5218243160777!', [
  '5218113130740',
  '5218243160777',
]);

// Reply to a message
await client.reply(chatId, 'This is a reply!', message.id.toString());

// Reply to a message with mention
await client.reply(
  chatId,
  'Hello @5218113130740 and @5218243160777! This is a reply with mention!',
  message.id.toString(),
  ['5218113130740', '5218243160777']
);

// Send file (venom will take care of mime types, just need the path)
await client.sendFile(chatId, 'path/to/file.pdf', 'cv.pdf', 'Curriculum');

// Send gif
await client.sendVideoAsGif(
  chatId,
  'path/to/video.mp4',
  'video.gif',
  'Gif image file'
);

// Send contact
// contactId: [email protected]
await client.sendContact(chatId, contactId);

// Forwards messages
await client.forwardMessages(chatId, [message.id.toString()], true);

//Generates sticker from the provided animated gif image and sends it (Send image as animated sticker)
//image path imageBase64 A valid gif image is required. You can also send via http/https (http://www.website.com/img.gif)
await client.sendImageAsStickerGif("[email protected]", './image.gif');

//Generates sticker from given image and sends it (Send Image As Sticker)
// image path imageBase64 A valid png, jpg and webp image is required. You can also send via http/https (http://www.website.com/img.jpg)
await client.sendImageAsSticker("[email protected]", './image.jpg');

// Send location
await client.sendLocation(
  chatId,
  25.6801987,
  -100.4060626,
  'Some address, Washington DC',
  'Subtitle'
);

// Send seen βœ”οΈβœ”οΈ
await client.sendSeen(chatId);

// Start typing...
await client.startTyping(chatId);

// Stop typing
await client.stopTyping(chatId);

// Set chat state (0: Typing, 1: Recording, 2: Paused)
await client.setChatState(chatId, 0 | 1 | 2);

Retrieving Data

// Calls your list of blocked contacts (returns an array)
const getBlockList = await client.getBlockList();

// Retrieve contacts
const contacts = await client.getAllContacts();

// Retrieve all messages in chat
const allMessages = await client.loadAndGetAllMessagesInChat(chatId);

// Retrieve contact status
const status = await client.getStatus(contactId);

// Retrieve user profile
const user = await client.getNumberProfile(contactId);

// Retrieve all unread message
const messages = await client.getAllUnreadMessages();

// Retrieve all chats
const chats = await client.getAllChats();

// Retrieve all groups
const chats = await client.getAllGroups();

// Retrieve profile fic (as url)
const url = await client.getProfilePicFromServer(chatId);

// Retrieve chat/conversation
const chat = await client.getChat(chatId);

Group Functions

// groupId or chatId: leaveGroup [email protected]

// Leave group
await client.leaveGroup(groupId);

// Get group members
await client.getGroupMembers(groupId);

// Get group members ids
await client.getGroupMembersIds(groupId);

// Generate group invite url link
await client.getGroupInviteLink(groupId);

// Create group (title, participants to add)
await client.createGroup('Group name', ['[email protected]', '[email protected]']);

// Remove participant
await client.removeParticipant(groupId, '[email protected]');

// Add participant
await client.addParticipant(groupId, '[email protected]');

// Promote participant (Give admin privileges)
await client.promoteParticipant(groupId, '[email protected]');

// Demote particiapnt (Revoke admin privileges)
await client.demoteParticipant(groupId, '[email protected]');

// Get group admins
await client.getGroupAdmins(groupId);

// Return the group status, jid, description from it's invite link
await client.getGroupInfoFromInviteLink(InviteCode);

// Join a group using the group invite code
await client.joinGroup(InviteCode);

Profile Functions

// Set client status
await client.setProfileStatus('On vacations! ✈️');

// Set client profile name
await client.setProfileName('Venom bot');

// Set client profile photo
await client.setProfilePic('path/to/image.jpg');

Device Functions

//Delete the Service Worker
await client.killServiceWorker();

//Load the service again
await client.restartService();

// Get device info
await client.getHostDevice();

// Get connection state
await client.getConnectionState();

// Get battery level
await client.getBatteryLevel();

// Is connected
await client.isConnected();

// Get whatsapp web version
await client.getWAVersion();

Events

// Listen to messages
client.onMessage(message => {
  ...
})

// Listen to state changes
client.onStateChange(state => {
  ...
});

// Listen to ack's
client.onAck(ack => {
  ...
});

// Listen to live location
// chatId: '[email protected]'
client.onLiveLocation(chatId, (liveLocation) => {
  ...
});

// chatId looks like this: '[email protected]'
// Event interface is in here: https://github.com/s2click/venom/blob/master/src/api/model/participant-event.ts
client.onParticipantsChanged(chatId, (event) => {
  ...
});

// Listen when client has been added to a group
client.onAddedToGroup(chatEvent => {
  ...
});

Other

//Change the theme
//string types "dark" or "light"
await client.setTheme(types);

//Receive the current theme
//returns string light or dark
await client.getTheme();

// Delete chat
await client.deleteChat(chatId);

// Clear chat messages
await client.clearChat(chatId);

// Delete message (last parameter: delete only locally)
await client.deleteMessage(chatId, message.id.toString(), false);

// Mark chat as not seen (returns true if it works)
await client.markUnseenMessage('[email protected]');

//blocks a user (returns true if it works)
await client.blockContact('[email protected]');

//unlocks contacts (returns true if it works)
await client.unblockContact('[email protected]');

// Retrieve a number profile / check if contact is a valid whatsapp number
const profile = await client.getNumberProfile('[email protected]');

Misc

There are some tricks for a better usage of venom.

Keep session alive:

// In case of being logged out of whatsapp web
// Force it to keep the current session
// State change
client.onStateChange((state) => {
  console.log(state);
  const conflits = [
    venom.SocketState.CONFLICT,
    venom.SocketState.UNPAIRED,
    venom.SocketState.UNLAUNCHED,
  ];
  if (conflits.includes(state)) {
    client.useHere();
  }
});

Send message to new contacts (non-added)

Also see Whatsapp links Be careful since this can pretty much could cause a ban from Whatsapp, always keep your contacts updated!

await client.sendMessageToId('[email protected]', 'Hello from venom! πŸ‘‹');

Multiple sessions

If you need to run multiple sessions at once just pass a session name to create() method, not use hyphen for name of sessions.

async () => {
  const marketingClient = await venom.create('marketing');
  const salesClient = await venom.create('sales');
  const supportClient = await venom.create('support');
};

Closing (saving) sessions

Close the session properly to ensure the session is saved for the next time you log in (So it wont ask for QR scan again). So instead of CTRL+C,

// Catch ctrl+C
process.on('SIGINT', function() {
  client.close();
});

// Try-catch close
try {
   ...
} catch (error) {
   client.close();
}

Auto closing unsynced sessions

The auto close is enabled by default and the timeout is setted to 60 sec. Receives the time in milliseconds to countdown until paired.

Important with autoClose enabled the "refreshQR" parameter is changed to 1000 (1 sec.)!

Use "autoClose: false" to disable auto closing.

Debugging

Development

Building venom is really simple altough it contians 3 main projects inside

  1. Wapi project
> npm run build:wapi
  1. Middleeware
> npm run build:middleware
> npm run build:jsQR
  1. Venom
> npm run build:venom

To build the entire project just run

> npm run build

Maintainers

Maintainers are needed, I cannot keep with all the updates by myself. If you are interested please open a Pull Request.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

venom's People

Contributors

orkestral avatar jonalan7 avatar joedutra avatar dogcalas avatar humbertorodrigues avatar zafranf avatar joaosouz4dev avatar cosmicsnow avatar dihak avatar imbios avatar murilopereirame avatar dependabot[bot] avatar hbdbim avatar garciabruno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.