Added offline check for isOnWhatsApp

This commit is contained in:
Adhiraj Singh
2020-11-20 12:50:14 +05:30
parent ba8b8c693a
commit 9f8f244b66
5 changed files with 78 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ import WS from 'ws'
import * as Utils from './Utils'
import Encoder from '../Binary/Encoder'
import Decoder from '../Binary/Decoder'
import fetch from 'node-fetch'
import fetch, { RequestRedirect } from 'node-fetch'
import {
AuthenticationCredentials,
WAUser,
@@ -31,7 +31,7 @@ const logger = pino({ prettyPrint: { levelFirst: true, ignore: 'hostname', trans
export class WAConnection extends EventEmitter {
/** The version of WhatsApp Web we're telling the servers we are */
version: [number, number, number] = [2, 2045, 19]
version: [number, number, number] = [2, 2047, 10]
/** The Browser we're telling the WhatsApp Web servers we are */
browserDescription: [string, string, string] = Utils.Browsers.baileys ('Chrome')
/** Metadata like WhatsApp id, name set on WhatsApp etc. */
@@ -406,10 +406,11 @@ export class WAConnection extends EventEmitter {
/**
* Does a fetch request with the configuration of the connection
*/
protected fetchRequest = (endpoint: string, method: string = 'GET', body?: any, agent?: Agent, headers?: {[k: string]: string}) => (
protected fetchRequest = (endpoint: string, method: string = 'GET', body?: any, agent?: Agent, headers?: {[k: string]: string}, redirect: RequestRedirect = 'follow') => (
fetch(endpoint, {
method,
body,
redirect,
headers: { Origin: DEFAULT_ORIGIN, ...(headers || {}) },
agent: agent || this.connectOptions.fetchAgent
})

View File

@@ -8,6 +8,7 @@ import {
} from '../WAConnection/Constants'
import { generateProfilePicture, whatsappID } from './Utils'
import { Mutex } from './Mutex'
import { URL } from 'url'
// All user related functions -- get profile picture, set status etc.
@@ -18,9 +19,33 @@ export class WAConnection extends Base {
* @returns undefined if the number doesn't exists, otherwise the correctly formatted jid
*/
isOnWhatsApp = async (str: string) => {
if (this.state !== 'open') {
return this.isOnWhatsAppNoConn(str)
}
const { status, jid } = await this.query({json: ['query', 'exist', str], requiresPhoneConnection: false})
if (status === 200) return { exists: true, jid: whatsappID(jid) }
}
}
/**
* Query whether a given number is registered on WhatsApp, without needing to open a WS connection
* @param str phone number/jid you want to check for
* @returns undefined if the number doesn't exists, otherwise the correctly formatted jid
*/
isOnWhatsAppNoConn = async (str: string) => {
let phone = str.split('@')[0]
const url = `https://wa.me/${phone}`
const response = await this.fetchRequest(url, 'GET', undefined, undefined, undefined, 'manual')
const loc = response.headers.get('Location')
if (!loc) {
this.logger.warn({ url, status: response.status }, 'did not get location from request')
return
}
const locUrl = new URL('', loc)
if (!locUrl.pathname.endsWith('send/')) {
return
}
phone = locUrl.searchParams.get('phone')
return { exists: true, jid: `${phone}@s.whatsapp.net` }
}
/**
* Tell someone about your presence -- online, typing, offline etc.
* @param jid the ID of the person/group who you are updating