Skip to content

Commit

Permalink
[core] Support MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
allburov committed Feb 18, 2024
1 parent 074063b commit fe42460
Show file tree
Hide file tree
Showing 26 changed files with 587 additions and 218 deletions.
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ services:
- WHATSAPP_SWAGGER_PASSWORD=123
- WHATSAPP_API_HOSTNAME=localhost
restart: always

mongodb:
image: mongo
container_name: mongodb
ports:
- '27017:27017/tcp'
volumes:
- mongodb_data:/data/db
restart: always

volumes:
mongodb_data: {}
13 changes: 11 additions & 2 deletions docs/site/content/en/docs/how-to/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ The following environment variables can be used to configure the WAHA.
response.
- `WHATSAPP_START_SESSION=session1,session2`: This variable can be used to start sessions with the specified names right
after launching the API. Separate session names with a comma.
- Also read more about [Sessions ->]({{< relref "/docs/how-to/sessions" >}})
- Also read more about [Session Storages on the dedicated page ->]({{< relref "/docs/how-to/storages#sessions" >}})

### Swagger
- `WHATSAPP_SWAGGER_CONFIG_ADVANCED=true` - enables advanced configuration options for Swagger documentation - you can customize host, port and base URL for the requests.
Expand Down Expand Up @@ -66,14 +68,18 @@ Read more about it on [**Session page** ->]({{< relref "/docs/how-to/sessions#co
Keep in mind that session's proxy configuration takes precedence over proxy configuration set by environment variables!


### Security ![](/images/versions/plus.png)
### Security
<b>Security is available in [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) only.</b>

- `WHATSAPP_API_KEY=mysecret`: If you set this variable, you must include the `X-Api-Key: mysecret` header in all
requests to the API. This will protect the API with a secret code.
- `WHATSAPP_SWAGGER_USERNAME=admin` and `WHATSAPP_SWAGGER_PASSWORD=admin`: These variables can be used to protect the
Swagger panel with `admin / admin` credentials. This does not affect API access.

Read more about security settings for Swagger and API on [**Security page** ->]({{< relref "/docs/how-to/security" >}}).

### Files ![](/images/versions/plus.png)
### Files
<b>Files configuration is available in [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) only.</b>

The following environment variables can be used to configure the file storage options for the WAHA:

Expand All @@ -85,8 +91,11 @@ The following environment variables can be used to configure the file storage op
- Under the hood, it sets `WHATSAPP_FILES_MIMETYPES=mimetype/ignore-all-media` to ignore all media files.
- `WHATSAPP_FILES_LIFETIME`: This variable can be used to set the time (in seconds) after which files will be removed to
free up space. The default value is `180`.
- Set this variable to `0` to disable the file lifetime.
- `WHATSAPP_FILES_FOLDER`: This variable can be used to set the folder where files from chats (images, voice messages)
will be stored. The default value is `/tmp/whatsapp-files`.
- The folder must be mounted to the host machine to keep the files between container restarts. [ Read more about how to persist files ->]({{< relref "/docs/how-to/storages#media" >}})
- Also read more about [Media Storages on the dedicated page ->]({{< relref "/docs/how-to/storages#media" >}})

💡 Even if WAHA doesn't process the message media because of `WHATSAPP_FILES_MIMETYPES` or `WHATSAPP_DOWNLOAD_MEDIA`
you'll get a webhook event with `hasMedia: True` field, but with no `media.url`.
Expand Down
24 changes: 2 additions & 22 deletions docs/site/content/en/docs/how-to/sessions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,28 +298,8 @@ configure autostart options so when the docker container restarts - it restores

### Session persistent

#### File storage
If you want to save your session and do not scan QR code everytime when you launch WAHA - connect a local file storage
to the container. WAHA stores authentication information in the directory and reuses it after restart.

[Attach volume](https://docs.docker.com/storage/volumes/) part to the command:

```bash
-v `pwd`/.sessions:/app/.sessions
```

The full command would be:

```bash
docker run -d -v `pwd`/.sessions:/app/.sessions -p 3000:3000/tcp --name whatsapp-http-api devlikeapro/whatsapp-http-api-plus
```

#### Remote storage ![](/images/versions/soon.png)

If you're interested in using some "remote" storage (like Redis or other Databases) to save sessions - please create an
issue on GitHub.

For instances, it may be useful if you run WAHA in a cluster of servers and do not have shared file storage
If you want to save your session and do not scan QR code everytime when you launch WAHA -
[connect the session storage to the container ->]({{< relref "/docs/how-to/storages#sessions" >}})

### Autostart
If you don't want to call `POST /api/sessions/start` for every session each time when the container restart -
Expand Down
139 changes: 139 additions & 0 deletions docs/site/content/en/docs/how-to/storages/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
title: "Storages"
description: "Storages"
lead: ""
date: 2020-10-06T08:48:45+00:00
lastmod: 2020-10-06T08:48:45+00:00
draft: false
images: [ ]
weight: 700
---
There are several storages that are used by the WAHA, below you can find the list of them and the way to configure them.
1. [Sessions](#sessions) - used to store the session data, such as authentication information, configuration, and other data that is required to keep the session alive and connected to WhatsApp.
2. [Media](#media) - used to store the media files, such as images, videos, and other files that are received from the WhatsApp instance.

<b>Storages are available in [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) only.</b>

## Sessions
The "Sessions" storage is used to store the session data, such as authentication information, configuration,
and other data that is required to keep the session alive and connected to WhatsApp.

If you want to save your session and do not scan QR code everytime when you launch WAHA -
connect the session storage to the container.

For the session storage, you can use the following options:
1. [Local (files)](#local-files) - the default option, stores the session data in the local storage using files.
2. [MongoDB](#mongodb) - stores the session data in the MongoDB database.

The following table shows the compatibility of the session storage with the engines:

| | WEBJS | NOWEB | VENOM |
|---------------|:-----:|:-----:|:-----:|
| Local (files) | ✔️ | ✔️ | ✔️ |
| MongoDB | ✔️ | ✔️ ||

## Sessions - Local (files)
By default, the WAHA uses the **local storage (files)** to store the session data.

{{< alert icon="💡" text="It's <b>well tested solution</b> even for production" />}}

In order to use the local storage and save the session data between the container restarts,
you need to mount the volume to the `/app/.sessions` directory using the `-v` option in `docker run` command:

```bash
-v /path/to/on/host/.sessions:/app/.sessions
```

The full command to run the WAHA with the local storage and save the session data
in the current directory and `.sessions` directory:
```bash
docker run -v `pwd`/.sessions:/app/.sessions -p 3000:3000/tcp devlikeapro/whatsapp-http-api-plus
```
This is the only action you need to do to use the local storage - all session data will be available between the container restarts.

### How it works
In the host machine, the session data will be stored in the current directory in the `.sessions` directory.

Under the hood, the WAHA stores the session data in the following directory structure:
```
.sessions/{engine}/{sessionName}/...
```
when you "logout" the session using [POST /api/sessions/logout]({{< relref "/docs/how-to/sessions#logout">}}) or providing `logout: True` in [POST /api/sessions/stop]({{< relref "/docs/how-to/sessions#stop">}})
it removes the directory with the session data.


## Sessions - MongoDB
If you want to use the MongoDB to store the session data, you need to:
1. Start the MongoDB server (using docker or any other way). You can either start your own MongoDB server or use the one of cloud providers, like [MongoDB Atlas](https://www.mongodb.com/atlas/database).
2. Set `WHATSAPP_SESSIONS_MONGO_URL=mongodb://user:password@host:port/` environment variable to connect to the MongoDB server.


**We recommend using your own MongoDB server as close as possible to WAHA server** for the best performance and security reasons.

### Example
First, you need to start MongoDB server:
```bash
docker run -d -p 27017:27017 --name mongodb mongo
```

Then, you need to run the WAHA with the `WHATSAPP_SESSIONS_MONGO_URL` environment variable:
```bash
docker run -e WHATSAPP_SESSIONS_MONGO_URL=mongodb://localhost:27017/ -p 3000:3000/tcp devlikeapro/whatsapp-http-api-plus
```

This is the only action you need to do to use the MongoDB storage -
all session authentication data will be stored in the MongoDB database.

### How it works
When you start a session, it stores the session data in the MongoDB database in two databases:
1. `waha_{engine}` - it saves the session configuration in `sessions` collection with `name: {sessionName}` field.
2. `waha_{engine}_{sessionName}` - it saves the WhatsApp authentication data and other session data. Each engine saves different data in this database.

When you "logout" the session using [POST /api/sessions/logout]({{< relref "/docs/how-to/sessions#logout">}}) or providing `logout: True` in [POST /api/sessions/stop]({{< relref "/docs/how-to/sessions#stop">}}) it removes those databases or elements from them.

For dealing and troubleshooting with the MongoDB, we recommend using [MongoDB Compass](https://www.mongodb.com/products/tools/compass).

![](waha-mongodb.png)

### MongoDB Atlas
If you use the [MongoDB Atlas](https://www.mongodb.com/atlas/database) you must grant
`Atlas Admin` role in **Security => Database Access** before you connect to the database.

For production please consider running the MongoDB server close to the WAHA server for the best performance and security reasons.

## Media
When your WhatsApp instance receives media files, it stores them in the media storage.

You can use the following options to store the media files:
1. [Local](#local) - the default option, stores the media files in the local storage using files.
2. [S3](#s3) - stores the media files in the S3 storage.

Read more about [available configuration options ->]({{<relref "/docs/how-to/config#files">}}).

The following table shows the compatibility of the storage with the engines:

| | WEBJS | NOWEB | VENOM |
|-------|:-----:|:-----:|:-----:|
| Local | ✔️ | ✔️ | ✔️ |
| S3 | 🕒 | 🕒 ||

## Media - Local
By default, the WAHA uses the **local file storage** to store the media files and those files has a short lifetime (180 seconds).
So it's your app responsibility to download and store them in a safe persistent place during this time.

### Save media files between the container restarts
If you want to use the local storage and **save the media files between the container restarts for a long time** - you need to:
1. Specify a dedicated folder to store the media files using `WHATSAPP_FILES_FOLDER=/app/.media` environment variable
2. Disable automatic media files cleanup using `WHATSAPP_FILES_LIFETIME=0` environment variable
3. Connect the volume to the specified folder using the `-v /path/to/files/on/host:/app/.media` option in `docker run` command to the folder specified in `WHATSAPP_FILES_FOLDER` environment variable.

Read more about [available configuration options ->]({{<relref "/docs/how-to/config#files">}}).

Here's all the steps in one command:
```bash
docker run -v /path/to/on/host/.media:/app/.media -e WHATSAPP_FILES_FOLDER=/app/.media -e WHATSAPP_FILES_LIFETIME=0 -p 3000:3000/tcp devlikeapro/whatsapp-http-api-plus
```

## Media - S3
If you're interested in using the S3 storage or any other cloud storage (like [self-hosted S3 - Minio](https://min.io/)),
please create an issue or vote for the S3 issue in [the GitHub repository](https://github.com/devlikeapro/whatsapp-http-api/issues?q=is%3Aissue+is%3Aopen+S3).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions docs/site/content/en/docs/overview/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ If you wish to get and read Release Notes - read them on one of our platforms, *

You even can **subscribe to get new updates** there!

## 2024.2
- Add support for [MongoDB as storage for Session data]({{< relref "/docs/how-to/storages" >}})
- Support persistent file storage for media files - [now you can save media files between container restarts]({{< relref "/docs/how-to/storages#media" >}})
- If you set `WHATSAPP_FILES_LIFETIME=0` environment variable - media files will be never deleted.

## 2024.1
- Implement [Patron Portal](https://portal.devlike.pro/) where you can get your personal API key and manage your perks.
- Read more on [Patreon ->](https://www.patreon.com/posts/waha-patron-97637416)
- Read more on [Boosty ->](https://boosty.to/wa-http-api/posts/8319079f-dac1-4179-b954-fcc559097c76)

## 2024.2
- Listen for browser disconnected and page close events in **WEBJS** engine [#262](https://github.com/devlikeapro/whatsapp-http-api/issues/262)

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"link-preview-js": "^3.0.4",
"lodash": "^4.17.21",
"mime-types": "^2.1.27",
"mongodb": "^6.3.0",
"passport": "^0.6.0",
"passport-headerapikey": "^1.2.2",
"qrcode": "^1.5.1",
Expand All @@ -53,7 +54,7 @@
"rxjs": "^7.1.0",
"swagger-ui-express": "^4.1.4",
"venom-bot": "5.0.1",
"whatsapp-web.js": "https://github.com/pedroslopez/whatsapp-web.js"
"whatsapp-web.js": "https://github.com/devlikeapro/whatsapp-web.js#main-fork"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/api/sessions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SessionsController {
@Post('/start/')
async start(@Body() request: SessionStartRequest): Promise<SessionDTO> {
const result = await this.manager.start(request);
await this.manager.sessionStorage.configRepository.save(
await this.manager.sessionConfigRepository.save(
request.name,
request.config || null,
);
Expand Down
4 changes: 4 additions & 0 deletions src/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export class WhatsappConfigService {
return value ? value.split(',') : [];
}

getSessionMongoUrl(): string | undefined {
return this.configService.get('WHATSAPP_SESSIONS_MONGO_URL', undefined);
}

getDefaultEngineName(): WAHAEngine {
const value = getEngineName();
if (value in WAHAEngine) {
Expand Down
1 change: 1 addition & 0 deletions src/core/abc/DataStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export abstract class DataStore {}
7 changes: 5 additions & 2 deletions src/core/abc/manager.abc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
SessionStartRequest,
SessionStopRequest,
} from '../../structures/sessions.dto';
import { ISessionAuthRepository } from '../storage/ISessionAuthRepository';
import { ISessionConfigRepository } from '../storage/ISessionConfigRepository';
import { WhatsappSession } from './session.abc';
import { LocalSessionStorage } from './storage.abc';
import { WebhookConductor } from './webhooks.abc';

export abstract class SessionManager implements OnApplicationShutdown {
public sessionStorage: LocalSessionStorage;
public store: any;
public sessionAuthRepository: ISessionAuthRepository;
public sessionConfigRepository: ISessionConfigRepository;

protected abstract getEngine(engine: WAHAEngine): typeof WhatsappSession;

Expand Down
11 changes: 5 additions & 6 deletions src/core/abc/session.abc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { EventEmitter } from 'events';
import * as fs from 'fs';
import { MessageId } from 'whatsapp-web.js';

import { OTPRequest, RequestCodeRequest } from '../../structures/auth.dto';
import {
ChatRequest,
CheckNumberStatusQuery,
Expand Down Expand Up @@ -47,8 +46,8 @@ import {
import { WASessionStatusBody } from '../../structures/webhooks.dto';
import { NotImplementedByEngineError } from '../exceptions';
import { QR } from '../QR';
import { DataStore } from './DataStore';
import { MediaManager } from './media.abc';
import { LocalSessionStorage } from './storage.abc';

const CHROME_PATH = '/usr/bin/google-chrome-stable';
const CHROMIUM_PATH = '/usr/bin/chromium';
Expand Down Expand Up @@ -77,7 +76,7 @@ export interface SessionParams {
name: string;
mediaManager: MediaManager;
log: ConsoleLogger;
sessionStorage: LocalSessionStorage;
sessionStore: DataStore;
proxyConfig?: ProxyConfig;
sessionConfig?: SessionConfig;
}
Expand All @@ -89,7 +88,7 @@ export abstract class WhatsappSession {
public name: string;
protected mediaManager: MediaManager;
protected log: ConsoleLogger;
protected sessionStorage: LocalSessionStorage;
protected sessionStore: DataStore;
protected proxyConfig?: ProxyConfig;
public sessionConfig?: SessionConfig;

Expand All @@ -98,7 +97,7 @@ export abstract class WhatsappSession {
public constructor({
name,
log,
sessionStorage,
sessionStore,
proxyConfig,
mediaManager,
sessionConfig,
Expand All @@ -107,7 +106,7 @@ export abstract class WhatsappSession {
this.name = name;
this.proxyConfig = proxyConfig;
this.log = log;
this.sessionStorage = sessionStorage;
this.sessionStore = sessionStore;
this.mediaManager = mediaManager;
this.sessionConfig = sessionConfig;
}
Expand Down
35 changes: 0 additions & 35 deletions src/core/abc/storage.abc.ts

This file was deleted.

Loading

0 comments on commit fe42460

Please sign in to comment.