Skip to content

Commit 8e9056a

Browse files
author
allburov
committed
[core] Add /api/sessions/{session}/me endpoint
Fix #191
1 parent ea75b35 commit 8e9056a

File tree

15 files changed

+151
-25
lines changed

15 files changed

+151
-25
lines changed

docs/site/content/en/docs/how-to/sessions/index.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ In order to log out the session - call `POST /api/sessions/logout`
128128
}
129129
```
130130

131+
### Get me
132+
133+
Get information about the associated account for that session (if any).
134+
```bash
135+
GET /api/sessions/{session}/me
136+
```
137+
138+
**Authenticated and working** session's response:
139+
```json
140+
{
141+
142+
"pushName": "string"
143+
}
144+
```
145+
146+
**Stopped** or **not authenticated** session returns null:
147+
```json
148+
null
149+
```
150+
151+
152+
131153

132154
## Authentication
133155
### Get QR

docs/site/content/en/docs/overview/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ toc: true
1414
---
1515
## 2023.9
1616
September 2023
17+
- Add `GET /api/sessions/{session/me` endpoint so you [can get the phone number associated with the session](https://waha.devlike.pro/docs/how-to/sessions/#get-me)
1718
- Add [polls support in NOWEB engine](https://waha.devlike.pro/docs/how-to/polls)
1819
- Add dedicated [Get QR](https://waha.devlike.pro/docs/how-to/sessions/#get-qr) endpoint!
1920
- Support [pairing method (NOWEB)](https://waha.devlike.pro/docs/how-to/sessions/#get-pairing-code) - you can connect with a code instead of QR.

src/api/groups.controller.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ export class GroupsController {
5151

5252
@Put(':id/settings/security/info-admin-only')
5353
@SessionApiParam
54-
@ApiOperation({ summary: 'Updates the group settings to only allow admins to edit group info (title, description, photo).' })
55-
setGroupAdminOnly(@SessionParam session: WhatsappSession, @Param('id') id: string) {
54+
@ApiOperation({
55+
summary:
56+
'Updates the group settings to only allow admins to edit group info (title, description, photo).',
57+
})
58+
setGroupAdminOnly(
59+
@SessionParam session: WhatsappSession,
60+
@Param('id') id: string,
61+
) {
5662
return session.setInfoAdminsOnly(id, true);
5763
}
5864

src/api/sessions.controller.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import { SessionManager } from '../core/abc/manager.abc';
66
import { parseBool } from '../helpers';
77
import {
88
ListSessionsQuery,
9+
MeInfo,
910
SessionDTO,
11+
SessionInfo,
1012
SessionLogoutRequest,
1113
SessionStartRequest,
1214
SessionStopRequest,
1315
} from '../structures/sessions.dto';
16+
import { SessionApiParam, SessionParam } from './helpers';
17+
import { WhatsappSession } from '../core/abc/session.abc';
18+
import { WAHAChatPresences } from '../structures/presence.dto';
1419

1520
@ApiSecurity('api_key')
1621
@Controller('api/sessions')
1722
@ApiTags('sessions')
18-
export class SessionsController {
23+
class SessionsController {
1924
constructor(private manager: SessionManager) {}
2025

2126
@Post('/start/')
@@ -45,8 +50,24 @@ export class SessionsController {
4550
}
4651

4752
@Get('/')
48-
async list(@Query() query: ListSessionsQuery): Promise<SessionDTO[]> {
53+
async list(@Query() query: ListSessionsQuery): Promise<SessionInfo[]> {
4954
const all = parseBool(query.all);
5055
return this.manager.getSessions(all);
5156
}
5257
}
58+
59+
@ApiSecurity('api_key')
60+
@Controller('api/sessions/:session')
61+
@ApiTags('sessions')
62+
class SessionController {
63+
constructor(private manager: SessionManager) {}
64+
65+
@Get('me')
66+
@SessionApiParam
67+
@ApiOperation({ summary: 'Get information about the authenticated account' })
68+
getMe(@SessionParam session: WhatsappSession): Promise<MeInfo> {
69+
return session.getSessionMeInfo();
70+
}
71+
}
72+
73+
export { SessionController, SessionsController };

src/core/abc/manager.abc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { OnApplicationShutdown } from '@nestjs/common';
33
import { WAHAEngine } from '../../structures/enums.dto';
44
import {
55
SessionDTO,
6+
SessionInfo,
67
SessionLogoutRequest,
78
SessionStartRequest,
89
SessionStopRequest,
@@ -35,5 +36,5 @@ export abstract class SessionManager implements OnApplicationShutdown {
3536

3637
abstract getSession(name: string, error?: boolean): WhatsappSession;
3738

38-
abstract getSessions(all: boolean): Promise<SessionDTO[]>;
39+
abstract getSessions(all: boolean): Promise<SessionInfo[]>;
3940
}

src/core/abc/session.abc.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ import {
3131
ParticipantsRequest,
3232
} from '../../structures/groups.dto';
3333
import { WAHAChatPresences } from '../../structures/presence.dto';
34-
import { ProxyConfig, SessionConfig } from '../../structures/sessions.dto';
34+
import {
35+
MeInfo,
36+
ProxyConfig,
37+
SessionConfig,
38+
} from '../../structures/sessions.dto';
3539
import { NotImplementedByEngineError } from '../exceptions';
3640
import { LocalSessionStorage, MediaStorage } from './storage.abc';
3741
import { OTPRequest, RequestCodeRequest } from '../../structures/auth.dto';
@@ -168,6 +172,10 @@ export abstract class WhatsappSession {
168172

169173
abstract getScreenshot(): Promise<Buffer | string>;
170174

175+
public getSessionMeInfo(): Promise<MeInfo | null> {
176+
throw new NotImplementedByEngineError();
177+
}
178+
171179
/**
172180
* Other methods
173181
*/

src/core/app.module.core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { ContactsController } from '../api/contacts.controller';
88
import { GroupsController } from '../api/groups.controller';
99
import { PresenceController } from '../api/presence.controller';
1010
import { ScreenshotController } from '../api/screenshot.controller';
11-
import { SessionsController } from '../api/sessions.controller';
11+
import {
12+
SessionController,
13+
SessionsController,
14+
} from '../api/sessions.controller';
1215
import { VersionController } from '../api/version.controller';
1316
import { WhatsappConfigService } from '../config.service';
1417
import { SessionManager } from './abc/manager.abc';
@@ -39,6 +42,7 @@ export const IMPORTS = [
3942
export const CONTROLLERS = [
4043
AuthController,
4144
SessionsController,
45+
SessionController,
4246
ChattingController,
4347
ChatsController,
4448
StatusController,

src/core/manager.core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { WAHAEngine, WAHASessionStatus } from '../structures/enums.dto';
1010
import {
1111
ProxyConfig,
1212
SessionDTO,
13+
SessionInfo,
1314
SessionLogoutRequest,
1415
SessionStartRequest,
1516
SessionStopRequest,
@@ -191,7 +192,7 @@ export class SessionManagerCore extends SessionManager {
191192
return session;
192193
}
193194

194-
async getSessions(all: boolean): Promise<SessionDTO[]> {
195+
async getSessions(all: boolean): Promise<SessionInfo[]> {
195196
if (!this.session) {
196197
if (!all) {
197198
return [];
@@ -201,14 +202,17 @@ export class SessionManagerCore extends SessionManager {
201202
name: this.DEFAULT,
202203
status: WAHASessionStatus.STOPPED,
203204
config: undefined,
205+
me: null,
204206
},
205207
];
206208
}
209+
const me = await this.session.getSessionMeInfo().catch((err) => null);
207210
return [
208211
{
209212
name: this.session.name,
210213
status: this.session.status,
211214
config: this.session.sessionConfig,
215+
me: me,
212216
},
213217
];
214218
}

src/core/session.noweb.core.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import makeWASocket, {
44
getAggregateVotesInPollMessage,
55
getKeyAuthor,
66
isJidGroup,
7+
jidNormalizedUser,
78
makeInMemoryStore,
89
PresenceData,
910
proto,
@@ -76,6 +77,7 @@ import {
7677
} from './exceptions';
7778
import { createAgentProxy } from './helpers.proxy';
7879
import { QR } from './QR';
80+
import { MeInfo } from '../structures/sessions.dto';
7981

8082
// eslint-disable-next-line @typescript-eslint/no-var-requires
8183
const QRCode = require('qrcode');
@@ -240,6 +242,19 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
240242
return;
241243
}
242244

245+
async getSessionMeInfo(): Promise<MeInfo | null> {
246+
const me = this.sock.authState?.creds?.me;
247+
if (!me) {
248+
return null;
249+
}
250+
const meId = jidNormalizedUser(me.id);
251+
const meInfo: MeInfo = {
252+
id: toCusFormat(meId),
253+
pushName: me.name,
254+
};
255+
return meInfo;
256+
}
257+
243258
/**
244259
* START - Methods for API
245260
*/

src/core/session.webjs.core.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import {
5050
NotImplementedByEngineError,
5151
} from './exceptions';
5252
import { QR } from './QR';
53+
import { MeInfo } from '../structures/sessions.dto';
54+
import { jidNormalizedUser } from '@adiwajshing/baileys';
5355

5456
// eslint-disable-next-line @typescript-eslint/no-var-requires
5557
const QRCode = require('qrcode');
@@ -113,6 +115,19 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
113115
return this.whatsapp.destroy();
114116
}
115117

118+
async getSessionMeInfo(): Promise<MeInfo | null> {
119+
const clientInfo = this.whatsapp?.info;
120+
if (!clientInfo) {
121+
return null;
122+
}
123+
const wid = clientInfo.wid;
124+
const meInfo: MeInfo = {
125+
id: wid._serialized,
126+
pushName: clientInfo.pushname,
127+
};
128+
return meInfo;
129+
}
130+
116131
protected listenConnectionEvents() {
117132
this.whatsapp.on(Events.QR_RECEIVED, (qr) => {
118133
this.log.debug('QR received');

0 commit comments

Comments
 (0)