Skip to content

Commit

Permalink
Fix mention function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Apr 4, 2020
1 parent d886b2d commit aa70b4e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sulla",
"version": "2.0.1",
"version": "2.1.0",
"description": "Javascript whatsapp framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion src/api/layers/controls.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class ControlsLayer extends GroupLayer {
* @param chatId The chat id from which to delete the message.
* @param messageId The specific message id of the message to be deleted
* @param onlyLocal If it should only delete locally (message remains on the other recipienct's phone). Defaults to false.
* @returns nothing
*/
public async deleteMessage(
chatId: string,
Expand Down
16 changes: 0 additions & 16 deletions src/api/layers/profile.layer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Page } from 'puppeteer';
import { ChatState } from '../model/enum';
import { HostLayer } from './host.layer';

declare module WAPI {
const setMyStatus: (to: string) => void;
const setMyName: (name: string) => void;
const setChatState: (chatState: string, chatId: string) => void;
}

export class ProfileLayer extends HostLayer {
Expand Down Expand Up @@ -38,18 +36,4 @@ export class ProfileLayer extends HostLayer {
{ name }
);
}

/**
* Sets the chat state
* @param chatState
* @param chatId
*/
public async setChatState(chatState: ChatState, chatId: string) {
return await this.page.evaluate(
({ chatState, chatId }) => {
WAPI.setChatState(chatState, chatId);
},
{ chatState: chatState, chatId }
);
}
}
65 changes: 31 additions & 34 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Page } from 'puppeteer';
import * as sharp from 'sharp';
import { base64MimeType, fileToBase64 } from '../helpers';
import { Message } from '../model';
import { ChatState } from '../model/enum';
import { ListenerLayer } from './listener.layer';

declare module WAPI {
Expand All @@ -15,7 +16,6 @@ declare module WAPI {
filename: string,
caption: string
) => any;
const sendMessageWithTags: (to: string, content: string) => string;
const sendMessageWithThumb: (
thumb: string,
url: string,
Expand Down Expand Up @@ -59,6 +59,7 @@ declare module WAPI {
) => void;
const sendMessageMentioned: (...args: any) => any;
const sendMessageToID: (id: string, message: string) => any;
const setChatState: (chatState: string, chatId: string) => void;
}

export class SenderLayer extends ListenerLayer {
Expand Down Expand Up @@ -118,24 +119,6 @@ export class SenderLayer extends ListenerLayer {
{ to, data, filename, caption }
);
}
/**
* Sends text message with @tags mentions
*
* Example:
* "Hello @8114285934 from sulla!"
* @param to chat id
* @param content message body
* @returns message id
*/
public sendTextWithTags(to: string, content: string): Promise<string> {
return this.page.evaluate(
({ to, content }) => {
WAPI.sendSeen(to);
return WAPI.sendMessageWithTags(to, content);
},
{ to, content }
);
}

/**
* Sends message with thumbnail
Expand Down Expand Up @@ -299,12 +282,12 @@ export class SenderLayer extends ListenerLayer {
}

/**
* This function takes an image and sends it as a sticker to the recipient. This is helpful for sending semi-ephemeral things like QR codes.
* The advantage is that it will not show up in the recipients gallery. This function automatiicaly converts images to the required webp format.
* @param b64: This is the base64 string formatted with data URI. You can also send a plain base64 string but it may result in an error as the function will not be able to determine the filetype before sending.
* @param to: The recipient id.
* Generates sticker from given image and sends it
* @param path image path
* @param to
*/
public async sendImageAsSticker(b64: string, to: string) {
public async sendImageAsSticker(to: string, path: string) {
const b64 = await fileToBase64(path);
const buff = Buffer.from(
b64.replace(/^data:image\/(png|gif|jpeg);base64,/, ''),
'base64'
Expand Down Expand Up @@ -383,15 +366,29 @@ export class SenderLayer extends ListenerLayer {
}

/**
* New version for sendign @tagged messages
* Sends text with tags
*
*/
// public async sendMentioned(to: string, message: string, mentioned: string) {
// console.log(message, mentioned);
// return await this.page.evaluate(
// ({ to, message, mentioned }) => {
// WAPI.sendMessageMentioned(to, message, mentioned);
// },
// { to, message, mentioned }
// );
// }
public async sendMentioned(to: string, message: string, mentioned: string[]) {
return await this.page.evaluate(
({ to, message, mentioned }) => {
WAPI.sendMessageMentioned(to, message, mentioned);
},
{ to, message, mentioned }
);
}

/**
* Sets the chat state
* @param chatState
* @param chatId
*/
public async setChatState(chatId: string, chatState: ChatState) {
return await this.page.evaluate(
({ chatState, chatId }) => {
WAPI.setChatState(chatState, chatId);
},
{ chatState: chatState, chatId }
);
}
}
14 changes: 9 additions & 5 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,18 @@ addOnAddedToGroup();
* New version of @tag message
*/
window.WAPI.sendMessageMentioned = async function (chatId, message, mentioned) {
var chat = WAPI.getChat(chatId);
const user = await Store.Contact.serialize().find(
(x) => x.id.user === mentioned
if (!Array.isArray(mentioned)) {
mentioned = [mentioned];
}

const chat = WAPI.getChat(chatId);
const users = await Store.Contact.serialize().filter((x) =>
mentioned.includes(x.id.user)
);
console.log(user);

chat.sendMessage(message, {
linkPreview: null,
mentionedJidList: [user.id],
mentionedJidList: users.map((u) => u.id),
quotedMsg: null,
quotedMsgAdminGroupJid: null,
});
Expand Down

0 comments on commit aa70b4e

Please sign in to comment.