Skip to content

Commit

Permalink
Add group functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Jun 10, 2019
1 parent a8678bc commit 84e592c
Show file tree
Hide file tree
Showing 11 changed files with 1,037 additions and 1,211 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.js
6 changes: 4 additions & 2 deletions src/api/model/chat.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Contact } from './contact';
import { GroupMetadata } from './group-metadata';
import { Id } from './id';

export interface Chat {
archive: boolean;
changeNumberNewJid: any;
changeNumberOldJid: any;
contact: Contact;
groupMetadata: any;
id: any;
groupMetadata: GroupMetadata;
id: Id;
isAnnounceGrpRestrict: any;
isGroup: boolean;
isReadOnly: boolean;
Expand Down
8 changes: 7 additions & 1 deletion src/api/model/contact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Id } from './id';



export interface Contact {
formattedName: string;
id: any;

id: Id;

isBusiness: boolean;
isEnterprise: boolean;
isHighLevelVerified: any;
Expand Down
13 changes: 13 additions & 0 deletions src/api/model/group-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Id } from './id';

export interface GroupMetadata {
id: Id;
creation: number;
owner: {
server: string;
user: string;
_serialized: string;
};
participants: any[];
pendingParticipants: any[];
}
5 changes: 5 additions & 0 deletions src/api/model/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Id {
server: string;
user: string;
_serialized: string;
}
15 changes: 14 additions & 1 deletion src/api/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ExposedFn } from './functions/exposed.enum';
import { Chat } from './model/chat';
import { Contact } from './model/contact';
import { Message } from './model/message';
import { Id } from './model/id';

declare module WAPI {
const waitNewMessages: (rmCallback: boolean, callback: Function) => void;
Expand All @@ -11,7 +12,8 @@ declare module WAPI {
const getAllContacts: () => Contact[];
const getAllChats: () => Chat[];
const getAllChatsWithNewMsg: () => Chat[];
const getAllGroups: () => any[];
const getAllGroups: () => Chat[];
const getGroupParticipantIDs: (groupId: string) => Promise<Id[]>;
}

export class Whatsapp {
Expand Down Expand Up @@ -78,4 +80,15 @@ export class Whatsapp {
return chats.filter(chat => chat.isGroup);
}
}

/**
* Retrieves group members as [Id] objects
* @param groupId group id
*/
public async getGroupMembersId(groupId) {
return await this.page.evaluate(
groupId => WAPI.getGroupParticipantIDs(groupId),
groupId
);
}
}
6 changes: 3 additions & 3 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import puppeteer from 'puppeteer';
import * as puppeteer from 'puppeteer';
import { puppeteerConfig } from '../config/puppeteer.config';

export async function initWhatsapp() {
Expand All @@ -26,8 +26,8 @@ export async function injectApi(page: puppeteer.Page) {

async function initBrowser() {
const browser = await puppeteer.launch({
//headless: false,
headless: true,
headless: false,
// headless: true,
devtools: false,
userDataDir: path.join(process.cwd(), 'session'),
args: [...puppeteerConfig.chroniumArgs]
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import { create } from './controllers/initializer';

export { Chat, Contact, Message } from './api/model';
export { Whatsapp } from './api/whatsapp';
export { create } from './controllers/initializer';

async function start() {
const client = await create();

// Group
const groups = await client.getAllGroups();
const group = groups.find(g => g.name === 'NEGATIVE VIBES/SHIT TALK');
const metadata = await client.getGroupMembersId(group.id._serialized);
console.log(metadata);

// const contacts = await client.getAllContacts();
// const berni = contacts.find(c => c.name === 'Berni');
// console.log(berni.name);
}

start();
1 change: 0 additions & 1 deletion src/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var ExposedFn;
*/
WAPI.waitNewMessages(false, function (data) {
data.forEach(function (message) {
console.log(message);
window[ExposedFn.OnMessage](message);
});
});
Loading

1 comment on commit 84e592c

@mmuniz75
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to add an image to the group ?

Please sign in to comment.