Skip to content

Commit

Permalink
feat: pollCreation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed May 25, 2023
1 parent 9f6b6bd commit 962d539
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 78 deletions.
96 changes: 55 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# 🕷Venom Bot🕸

[![npm version](https://img.shields.io/npm/v/venom-bot.svg?color=green)](https://www.npmjs.com/package/venom-bot)
Expand All @@ -14,14 +13,12 @@

> 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.

## Get started fast and easy! Official API!

<p align="center">
<a href="https://docs.orkestral.io/venom">Full Documentation</a>
</p>


<p>
It's a high-performance alternative API to whatzapp, you can send, text messages, files, images, videos and more.
</p>
Expand All @@ -47,8 +44,8 @@ Use it in your favorite language like PHP, Python, C# and others. as long as you

<a target="_blank" href="https://discord.gg/uBRjk6vecs" target="_blank"><img title="Discord" height="110" width="375" src="https://s2.glbimg.com/GUbCgnBxJERAmuaYcrjBzTXD5ws=/0x0:800x272/984x0/smart/filters:strip_icc()/i.s3.glbimg.com/v1/AUTH_63b422c2caee4269b8b34177e8876b93/internal_photos/bs/2021/d/N/zJs579QOGxKVRxfPILCA/discord-app.png"></a>


## Meet the Superchats

<br>
<a href='https://github.com/orkestral/superchats'><img src='https://github.com/orkestral/superchats/raw/main/img/superchats.png' height='60' alt='SuperChats' aria-label='https://github.com/orkestral/superchats' /></a>
<br>
Expand All @@ -69,21 +66,21 @@ The value of the license is $50 monthly dollars, to acquire contact in whatsapp

## 🕷🕷 Functions Venom🕷🕷

| | |
|---------------------------------------------------------------|---|
| 🚻 Automatic QR Refresh ||
| 📁 Send **text, image, video, audio and docs** ||
| 👥 Get **contacts, chats, groups, group members, Block List** ||
| 📞 Send contacts ||
| Send Buttons ||
| Send stickers ||
| Send stickers GIF ||
| Multiple Sessions ||
| ⏩ Forward Messages ||
| 📥 Receive message ||
| 👤 insert user section ||
| 📍 Send location!! ||
| 🕸🕸 **and much more** ||
| | |
| ------------------------------------------------------------- | --- |
| 🚻 Automatic QR Refresh | |
| 📁 Send **text, image, video, audio and docs** | |
| 👥 Get **contacts, chats, groups, group members, Block List** | |
| 📞 Send contacts | |
| Send Buttons | |
| Send stickers | |
| Send stickers GIF | |
| Multiple Sessions | |
| ⏩ Forward Messages | |
| 📥 Receive message | |
| 👤 insert user section | |
| 📍 Send location!! | |
| 🕸🕸 **and much more** | |

Documentation at https://orkestral.github.io/venom/index.html

Expand Down Expand Up @@ -114,7 +111,7 @@ const venom = require('venom-bot');

venom
.create({
session: 'session-name', //name of session
session: 'session-name' //name of session
})
.then((client) => start(client))
.catch((erro) => {
Expand Down Expand Up @@ -201,7 +198,7 @@ venom
userProxy: '', // Proxy login username
userPass: '' // Proxy password
},

// BrowserInstance
(browser, waPage) => {
console.log('Browser PID:', browser.process().pid);
Expand All @@ -221,7 +218,7 @@ venom
Gets the return if the session is `isLogged` or `notLogged` or `browserClose` or `qrReadSuccess` or `qrReadFail` or `autocloseCalled` or `desconnectedMobile` or `deleteToken` or `chatsAvailable` or `deviceNotConnected` or `serverWssNotConnected` or `noOpenBrowser` or `initBrowser` or `openBrowser` or `connectBrowserWs` or `initWhatsapp` or `erroPageWhatsapp` or `successPageWhatsapp` or `waitForLogin` or `waitChat` or `successChat` or `Create session wss return "serverClose" case server for close`

| Status | Condition |
|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isLogged` | When the user is already logged in to the browser |
| `notLogged` | When the user is not connected to the browser, it is necessary to scan the QR code through the cell phone in the option WhatsApp Web |
| `browserClose` | If the browser is closed this parameter is returned |
Expand All @@ -230,7 +227,7 @@ Gets the return if the session is `isLogged` or `notLogged` or `browserClose` or
| `autocloseCalled` | The browser was closed using the autoClose command |
| `desconnectedMobile` | Client has desconnected in to mobile |
| `serverClose` | Client has desconnected in to wss |
| `deleteToken` | If you pass true within the function |
| `deleteToken` | If you pass true within the function |
| `chatsAvailable` | When Venom is connected to the chat list |
| `deviceNotConnected` | Chat not available because the phone is disconnected `(Trying to connect to the phone)` |
| `serverWssNotConnected` | The address wss was not found! |
Expand All @@ -243,20 +240,17 @@ Gets the return if the session is `isLogged` or `notLogged` or `browserClose` or
| `successPageWhatsapp` | Page Whatsapp successfully accessed |
| `waitForLogin` | Waiting for login verification! |
| `waitChat` | Waiting for the chat to load |
| `successChat` | Chat successfully loaded! |
| `successChat` | Chat successfully loaded! |

```javascript
const venom = require('venom-bot');
venom
.create(
'sessionName',
undefined,
(statusSession, session) => {
console.log('Status Session: ', statusSession);
//return isLogged || notLogged || browserClose || qrReadSuccess || qrReadFail || autocloseCalled || desconnectedMobile || deleteToken || chatsAvailable || deviceNotConnected || serverWssNotConnected || noOpenBrowser || initBrowser || openBrowser || connectBrowserWs || initWhatsapp || erroPageWhatsapp || successPageWhatsapp || waitForLogin || waitChat || successChat
//Create session wss return "serverClose" case server for close
console.log('Session name: ', session);
},
)
.create('sessionName', undefined, (statusSession, session) => {
console.log('Status Session: ', statusSession);
//return isLogged || notLogged || browserClose || qrReadSuccess || qrReadFail || autocloseCalled || desconnectedMobile || deleteToken || chatsAvailable || deviceNotConnected || serverWssNotConnected || noOpenBrowser || initBrowser || openBrowser || connectBrowserWs || initWhatsapp || erroPageWhatsapp || successPageWhatsapp || waitForLogin || waitChat || successChat
//Create session wss return "serverClose" case server for close
console.log('Session name: ', session);
})
.then((client) => {
start(client);
})
Expand Down Expand Up @@ -345,6 +339,27 @@ available can be found in [here](/src/api/layers) and

```javascript

// Send Poll
const poll = {
name: 'new poll',
options: [
{
name: 'option 1'
},
{
name: 'option 2'
}
],
selectableOptionsCount: 1
};
await client.sendPollCreation('000000000000@c.us', poll)
.then((result) => {
console.log('Result: ', result); //return object success
})
.catch((erro) => {
console.error('Error when sending: ', erro); //return object error
});

// Send List menu
const list = [
{
Expand Down Expand Up @@ -694,12 +709,14 @@ const url = await client.getProfilePicFromServer('000000000000@c.us');
const chat = await client.getChat('000000000000@c.us');

// Check if the number exists
const chat = await client.checkNumberStatus('000000000000@c.us')
.then((result) => {
const chat = await client
.checkNumberStatus('000000000000@c.us')
.then((result) => {
console.log('Result: ', result); //return object success
}).catch((erro) => {
})
.catch((erro) => {
console.error('Error when sending: ', erro); //return object error
});
});
```

## Group Functions
Expand Down Expand Up @@ -771,13 +788,11 @@ await client.setProfilePic('path/to/image.jpg');

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

```

## Device Functions

```javascript

// Disconnect from service
await client.logout();

Expand Down Expand Up @@ -946,7 +961,6 @@ await client.blockContact('000000000000@c.us');

// Unlocks contacts (returns true if it works)
await client.unblockContact('000000000000@c.us');

```
## Misc
Expand Down
3 changes: 2 additions & 1 deletion src/api/helpers/exposed.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export enum ExposedFn {
onStreamChange = 'onStreamChange',
onFilePicThumb = 'onFilePicThumb',
onChatState = 'onChatState',
onUnreadMessage = 'onUnreadMessage'
onUnreadMessage = 'onUnreadMessage',
onPoll = 'onPoll'
}
18 changes: 18 additions & 0 deletions src/api/layers/listener.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare global {
func: any;
onLiveLocation: any;
waitNewMessages: any;
onPoll: any;
}
}

Expand Down Expand Up @@ -103,6 +104,9 @@ export class ListenerLayer extends ProfileLayer {
window.WAPI.onAck((e: any) => {
window.onAck(e);
});
window.WAPI.onPoll((e: any) => {
window.onPoll(e);
});
})
.catch(() => {});
}
Expand Down Expand Up @@ -130,6 +134,20 @@ export class ListenerLayer extends ProfileLayer {
.catch(() => {});
}

public async onPoll(fn: (ack: any) => void) {
this.listenerEmitter.on(ExposedFn.onPoll, (e) => {
fn(e);
});

return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onPoll, (e) => {
fn(e);
});
}
};
}

/**
* @event Listens to all new messages
* @param to callback
Expand Down
20 changes: 20 additions & 0 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ export class SenderLayer extends ListenerLayer {
});
}

/**
* Create poll
* @param idUser chat id: xxxxx@us.c
*/
public async sendPollCreation(idUser: string, poll: any) {
return new Promise(async (resolve, reject) => {
const result = await this.page.evaluate(
({ idUser, poll }) => {
return WAPI.sendPollCreation(idUser, poll);
},
{ idUser, poll }
);
if (result['erro'] == true) {
return reject(result);
} else {
return resolve(result);
}
});
}

//*PRO_
/**
* @param filePath path, http link or base64Encoded
Expand Down
1 change: 1 addition & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export { addChatWapi } from './add-chat-wapi';
export { sendTypeButtons } from './send-type-buttons';
export { onlySendAdmin } from './only-send-admin';
export { createCommunity } from './create-community';
export { pollCreation } from './poll-creation';

/////
export { baseSendMessage } from './send-message-scope';
Expand Down
51 changes: 51 additions & 0 deletions src/lib/wapi/functions/poll-creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export async function pollCreation(idUser, poll) {
if (typeof poll !== 'object') {
return WAPI.scope(idUser, true, 404, 'poll must be an object');
}
if (!poll?.name) {
return WAPI.scope(idUser, true, 404, 'Missing object name');
}
if (!poll?.options) {
return WAPI.scope(idUser, true, 404, 'Missing object options');
}
if (
typeof poll.selectableOptionsCount !== 'number' ||
(poll.selectableOptionsCount !== 1 && poll.selectableOptionsCount !== 0)
) {
return WAPI.scope(
idUser,
true,
404,
'Error checking selectableOptionsCount!'
);
}

const options = poll.options;
if (Array.isArray(options) && options.length > 0) {
for (let index in options) {
if (typeof options[index] !== 'function') {
if (!options[index].name) {
return WAPI.scope(idUser, true, 404, 'Missing object name');
}
if (typeof options[index].name !== 'string') {
return WAPI.scope(idUser, true, 404, 'Passed string value in name');
}
}
}
}

const chat = await WAPI.sendExist(idUser);
if (chat && chat.status !== 404 && chat.id) {
await Store.Survey.sendPollCreation({
chat: chat,
poll: poll,
quotedMsg: null
});
return { error: false, lastReceivedKey: chat.lastReceivedKey };
} else {
if (!chat.error) {
chat.error = true;
}
return chat;
}
}
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/send-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function sendButtons(to, title, buttons, subtitle) {
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
type: 'ciphertext',
type: 'chat',
body: title,
caption: title,
content: title,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/wapi/listeners/add-on-poll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function addOnPoll() {
window.WAPI.onPoll = function (callback) {
Store.PollVote.on('change', (e) => {
callback(e);
});
return true;
};
}
1 change: 1 addition & 0 deletions src/lib/wapi/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { addonFilePicThumb } from './add-on-pictumb-change';
export { addonChatState } from './add-on-chatstate-change';
export { addonUnreadMessage } from './add-unread-message';
export { addOnStream } from './add-on-stream';
export { addOnPoll } from './add-on-poll';
34 changes: 0 additions & 34 deletions src/lib/wapi/store/get-store.d.ts

This file was deleted.

Loading

0 comments on commit 962d539

Please sign in to comment.