Update isOnWhatsApp

This commit is contained in:
Adhiraj Singh
2020-11-07 13:47:21 +05:30
parent 75aa1f9d81
commit 442c03a05e
2 changed files with 14 additions and 8 deletions

View File

@@ -243,7 +243,7 @@ const buffer = fs.readFileSync("Media/ma_gif.mp4") // load some gif
const options: MessageOptions = {mimetype: Mimetype.gif, caption: "hello!"} // some metadata & caption
conn.sendMessage(id, buffer, MessageType.video, options)
// send an audio file
const buffer = fs.readFileSync("Media/audio.mp3") // can send mp3, mp4, & ogg -- but for mp3 files the mimetype must be set to ogg
const buffer = fs.readFileSync("Media/audio.mp3") // can send mp3, mp4, & ogg
const options: MessageOptions = {mimetype: Mimetype.mp4Audio} // some metadata (can't have caption in audio)
conn.sendMessage(id, buffer, MessageType.audio, options)
```
@@ -371,9 +371,9 @@ await conn.deleteChat (jid) // will delete the chat (can be a group or broadcast
```
- To check if a given ID is on WhatsApp
``` ts
const id = 'xyz@s.whatsapp.net'
const id = '123456'
const exists = await conn.isOnWhatsApp (id)
console.log (`${id} ${exists ? " exists " : " does not exist"} on WhatsApp`)
if (exists) console.log (`${id} exists on WhatsApp, as jid: ${exists.jid}`)
```
- To query chat history on a group or with someone
``` ts

View File

@@ -1,20 +1,26 @@
import {WAConnection as Base} from './4.Events'
import { Presence, WABroadcastListInfo, WAProfilePictureChange, WAChat, ChatModification, WALoadChatOptions } from './Constants'
import { Presence, WABroadcastListInfo, WAProfilePictureChange, ChatModification, WALoadChatOptions } from './Constants'
import {
WAMessage,
WANode,
WAMetric,
WAFlag,
} from '../WAConnection/Constants'
import { generateProfilePicture, waChatKey, whatsappID, unixTimestampSeconds } from './Utils'
import { generateProfilePicture, whatsappID, unixTimestampSeconds } from './Utils'
import { Mutex } from './Mutex'
import { type } from 'os'
// All user related functions -- get profile picture, set status etc.
export class WAConnection extends Base {
/** Query whether a given number is registered on WhatsApp */
isOnWhatsApp = (jid: string) => this.query({json: ['query', 'exist', jid], requiresPhoneConnection: false}).then((m) => m.status === 200)
/**
* Query whether a given number is registered on WhatsApp
* @param str phone number/jid you want to check for
* @returns undefined if the number doesn't exists, otherwise the correctly formatted jid
*/
isOnWhatsApp = async (str: string) => {
const { status, jid } = await this.query({json: ['query', 'exist', str], requiresPhoneConnection: false})
if (status === 200) return { exists: true, jid: whatsappID(jid) }
}
/**
* Tell someone about your presence -- online, typing, offline etc.
* @param jid the ID of the person/group who you are updating