|
| 1 | +# Client & Lifecycle |
| 2 | + |
| 3 | +The `Client` is the entry point. Constructing it **connects automatically** and emits lifecycle |
| 4 | +events; there is no separate `connect()` call. |
| 5 | + |
| 6 | +```typescript |
| 7 | +import { Client } from 'zaileys' |
| 8 | + |
| 9 | +const client = new Client({ sessionId: 'default' }) |
| 10 | +``` |
| 11 | + |
| 12 | +## Connection events |
| 13 | + |
| 14 | +```typescript |
| 15 | +client.on('qr', ({ qrString }) => {}) // QR string to render |
| 16 | +client.on('connect', ({ me }) => {}) // authenticated; `me` is your JID |
| 17 | +client.on('disconnect', ({ reason }) => {}) // dropped (auto-reconnect runs with backoff) |
| 18 | +``` |
| 19 | + |
| 20 | +Zaileys reconnects automatically with exponential backoff, and detects an invalid/corrupted |
| 21 | +session so you know when to delete the auth folder and re-scan. |
| 22 | + |
| 23 | +## Sending & mutating |
| 24 | + |
| 25 | +`client.send(jid)` opens a [builder](/sending-messages). Awaiting any send resolves to the new |
| 26 | +`WAMessageKey`, which the mutation helpers consume: |
| 27 | + |
| 28 | +```typescript |
| 29 | +const key = await client.send(jid).text('Original') |
| 30 | +await client.edit(key).text('Edited') |
| 31 | +await client.react(key, '👍') // empty string removes the reaction |
| 32 | +await client.delete(key, { forEveryone: true }) |
| 33 | +await client.forward(key, otherJid) |
| 34 | +``` |
| 35 | + |
| 36 | +## Domain namespaces |
| 37 | + |
| 38 | +Higher-level operations are grouped under namespaces (each throws `NOT_CONNECTED` until the client |
| 39 | +is connected): |
| 40 | + |
| 41 | +| Namespace | Purpose | |
| 42 | +| --------- | ------- | |
| 43 | +| `client.group.*` | metadata, participants, invite, subject/description | |
| 44 | +| `client.presence.*` | `typing()` / `recording()` / `online()` / `offline()` | |
| 45 | +| `client.privacy.*` | privacy settings | |
| 46 | +| `client.newsletter.*` | channel/newsletter operations | |
| 47 | +| `client.community.*` | community operations | |
| 48 | + |
| 49 | +```typescript |
| 50 | +await client.presence.typing(jid) // show the "typing…" indicator |
| 51 | +const meta = await client.group.metadata(groupJid) |
| 52 | +``` |
| 53 | + |
| 54 | +## Options |
| 55 | + |
| 56 | +| Option | Type | Default | Notes | |
| 57 | +| ------ | ---- | ------- | ----- | |
| 58 | +| `sessionId` | `string` | `'default'` | auth folder / namespace | |
| 59 | +| `authType` | `'qr' \| 'pairing'` | `'qr'` | login method | |
| 60 | +| `phoneNumber` | `string` | — | required for `pairing` | |
| 61 | +| `commandPrefix` | `string \| string[]` | — | enables the [command framework](/commands) | |
| 62 | +| `ignoreMe` | `boolean` | `true` | drop messages from the bot's own account | |
| 63 | +| `auth` | `AuthStoreBundle` | `FileAuthStore` | see [Storage](/storage) | |
| 64 | +| `store` | `MessageStore` | in-memory | see [Storage](/storage) | |
| 65 | +| `logger` | Pino-compatible | silent-ish | structural logger | |
0 commit comments