From 169f0bb77f8209c08649493719c46646c795ddb5 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Mon, 13 Jul 2020 17:00:19 +0530 Subject: [PATCH] Bug fix + Url query --- src/WAClient/Base.ts | 12 +++++++++++- src/WAClient/Constants.ts | 7 +++++++ src/WAClient/Tests.ts | 6 ++++++ src/WAConnection/Connect.ts | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/WAClient/Base.ts b/src/WAClient/Base.ts index e93766f..2db5269 100644 --- a/src/WAClient/Base.ts +++ b/src/WAClient/Base.ts @@ -1,5 +1,5 @@ import WAConnection from '../WAConnection/WAConnection' -import { MessageStatus, MessageStatusUpdate, PresenceUpdate, Presence, ChatModification, WABroadcastListInfo } from './Constants' +import { MessageStatus, MessageStatusUpdate, PresenceUpdate, Presence, ChatModification, WABroadcastListInfo, WAUrlInfo } from './Constants' import { WAMessage, WANode, @@ -7,6 +7,7 @@ import { WAFlag, MessageLogLevel, } from '../WAConnection/Constants' +import { proto } from '../../WAMessage/WAMessage' export default class WhatsAppWebBase extends WAConnection { /** Set the callback for message status updates (when a message is delivered, read etc.) */ @@ -186,4 +187,13 @@ export default class WhatsAppWebBase extends WAConnection { const json = ['action', {epoch: this.msgCount.toString(), type: 'set'}, nodes] return this.queryExpecting200(json, [WAMetric.group, WAFlag.ignore]) as Promise<{status: number}> } + /** Query a string to check if it has a url, if it does, return required info */ + async urlQuery (text: string) { + const query = ['query', {type: 'url', url: text, epoch: this.msgCount.toString()}, null] + const response = await this.queryExpecting200 (query, [26, WAFlag.ignore]) + if (response[1]) { + response[1].jpegThumbnail = response[2] + } + return response[1] as WAUrlInfo + } } diff --git a/src/WAClient/Constants.ts b/src/WAClient/Constants.ts index 43a73b1..9a3d1cb 100644 --- a/src/WAClient/Constants.ts +++ b/src/WAClient/Constants.ts @@ -80,6 +80,13 @@ export interface WABroadcastListInfo { name: string recipients?: {id: string}[] } +export interface WAUrlInfo { + 'canonical-url': string + 'matched-text': string + title: string + description: string + jpegThumbnail?: Buffer +} export interface MessageInfo { reads: {jid: string, t: string}[] deliveries: {jid: string, t: string}[] diff --git a/src/WAClient/Tests.ts b/src/WAClient/Tests.ts index 5635004..eb2e81c 100644 --- a/src/WAClient/Tests.ts +++ b/src/WAClient/Tests.ts @@ -110,6 +110,12 @@ WAClientTest('Misc', (client) => { it('should return the stories', async () => { await client.getStories() }) + it('should return a preview', async () => { + const info = await client.urlQuery ('fren have you seen https://www.github.com/adiwajshing/Baileys') + assert.equal (info["matched-text"], 'https://www.github.com/adiwajshing/Baileys') + + await assert.rejects (() => client.urlQuery('oh hello there')) + }) it('should return the profile picture', async () => { const response = await client.getProfilePicture(testJid) assert.ok(response) diff --git a/src/WAConnection/Connect.ts b/src/WAConnection/Connect.ts index b1bd874..bfd4971 100644 --- a/src/WAConnection/Connect.ts +++ b/src/WAConnection/Connect.ts @@ -42,7 +42,7 @@ export default class WAConnectionConnector extends WAConnectionValidator { .then(user => { this.startKeepAliveRequest() - this.conn.on ('error', null) + this.conn.removeAllListeners ('error') this.conn.on ('close', () => this.unexpectedDisconnect ('closed')) resolve(user)