Skip to content

Commit 60b4be5

Browse files
authored
Merge pull request #16 from Dialogue-Bot/DIAL-24-Add-receive-message-from-channels
DIAL-24-Add-receive-message-from-channels
2 parents 47d4693 + 60f508a commit 60b4be5

File tree

7 files changed

+80
-38
lines changed

7 files changed

+80
-38
lines changed

server/src/channels/base.channel.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ export class BaseChannel {
77
contactName: string;
88
channelType: string;
99

10-
constructor(id: string, contactId: string, contactName: string, channelType: string,) {
10+
constructor(id: string, contactId: string, contactName: string, channelType: string) {
1111
this.id = id;
1212
this.contactId = contactId;
1313
this.contactName = contactName;
1414
this.channelType = channelType;
15-
16-
if (channelType && contactName && contactId) {
17-
console.log(`Init channel ${channelType} - ${contactName} ${contactId}`);
18-
}
1915
}
2016

2117
public async postMessageToBot({ userId, data }) {

server/src/channels/messenger.channel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ export class MessengerChannel extends BaseChannel {
3939
return null;
4040
}
4141
}
42+
43+
public async prepareMessage(req: Request, res: Response) {
44+
45+
}
4246
}

server/src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const ENDPOINTS = {
3535
WEBHOOK: {
3636
INDEX: '/webhook',
3737
VERIFY: '/webhook/:contactId',
38+
INCOMING_MSG: '/webhook/:contactId',
3839
}
3940
};
4041

server/src/controllers/webhook.controller.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ export class WebhookController {
1515

1616
res.status(StatusCodes.OK).send(data);
1717
});
18+
19+
public handleIncomingMessage = catchAsync(async (req, res) => {
20+
res.status(StatusCodes.OK);
21+
22+
return await this.webhookService.handleIncomingMessage(req.params.contactId, req, res);
23+
});
1824
}

server/src/routes/webhook.route.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ export class WebhookRoute implements Routes {
1414
ENDPOINTS.WEBHOOK.VERIFY,
1515
this.controller.verifyWebhook
1616
);
17+
18+
this.router.post(
19+
ENDPOINTS.WEBHOOK.INCOMING_MSG,
20+
this.controller.handleIncomingMessage
21+
);
1722
}
1823
}

server/src/services/channels.service.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,25 @@ export class ChannelService {
5858
return newChannel;
5959
}
6060

61-
public async findOneById(id: string) {
62-
const channel = await db.query.channels.findFirst({
63-
where: eq(channels.id, id),
64-
});
61+
public async findOneByContactId(contactId: string) {
62+
const expectedChannel = await db.select({
63+
id: channels.id,
64+
contactId: channels.contactId,
65+
contactName: channels.contactName,
66+
channelType: channelTypes.name,
67+
credentials: channels.credentials,
68+
})
69+
.from(channels)
70+
.where(
71+
and(eq(channels.contactId, contactId),
72+
eq(channels.deleted, false)
73+
)
74+
)
75+
.innerJoin(channelTypes, eq(channels.channelTypeId, channelTypes.id));
6576

66-
return channel;
77+
if (!expectedChannel) return
78+
79+
return expectedChannel[0];
6780
}
6881

6982
public async updateById(id: string, fields: TUpdateChannel) {
@@ -78,9 +91,7 @@ export class ChannelService {
7891
);
7992
}
8093

81-
const contactIdExisted = await db.query.channels.findFirst({
82-
where: eq(channels.contactId, fields.contactId),
83-
});
94+
const contactIdExisted = this.channels.find(c => c.contactId == fields.contactId);
8495

8596
if (contactIdExisted && fields.contactId !== channelExisted.contactId) {
8697
throw new HttpException(
@@ -193,12 +204,15 @@ export class ChannelService {
193204
}
194205

195206
initChannel(channel: ChannelInfo) {
196-
switch (channel.channelType) {
207+
const { id, contactId, contactName, channelType, credentials } = channel;
208+
209+
console.log(`Init channel: ${channelType} - ${contactName} ${contactId}`);
210+
211+
switch (channelType) {
197212
case 'MSG':
198-
const { id, contactId, contactName, channelType, credentials } = channel;
199213
return new MessengerChannel(id, contactId, contactName, channelType, credentials);
200214
default:
201-
console.log(`Does not support channel type ${channel.channelType}`);
215+
console.log(`Init channel: Does not support channel type ${channel.channelType}`);
202216
break;
203217
}
204218

@@ -215,7 +229,12 @@ export class ChannelService {
215229
credentials: channels.credentials,
216230
})
217231
.from(channels)
218-
.where(eq(channels.deleted, false))
232+
.where(
233+
and(
234+
eq(channels.deleted, false),
235+
eq(channels.active, true)
236+
)
237+
)
219238
.innerJoin(channelTypes, eq(channels.channelTypeId, channelTypes.id))
220239

221240
contacts.forEach((c) => {
Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
import { MessengerChannel } from "@/channels/messenger.channel";
22
import { LOCALE_KEY } from "@/constants";
3-
import { db } from "@/database/db";
4-
import { channelTypes, channels } from "@/database/schema";
53
import { HttpException } from "@/exceptions/http-exception";
64
import { LocaleService } from "@/i18n/ctx";
7-
import { and, eq } from "drizzle-orm";
85
import { Request, Response } from "express";
96
import { StatusCodes } from "http-status-codes";
107
import { Inject, Service } from "typedi";
8+
import { ChannelService } from "./channels.service";
119

1210
@Service()
1311
export class WebhookService {
1412
constructor(
15-
@Inject(LOCALE_KEY) private readonly localeService: LocaleService
13+
@Inject(LOCALE_KEY) private readonly localeService: LocaleService,
14+
private readonly chanelService: ChannelService
1615
) { }
1716

1817
public async verifyWebhook(contactId: string, req: Request, res: Response) {
19-
const expectedChannel = await db.select({
20-
id: channels.id,
21-
contactId: channels.contactId,
22-
contactName: channels.contactName,
23-
channelType: channelTypes.name,
24-
credentials: channels.credentials,
25-
})
26-
.from(channels)
27-
.where(
28-
and(eq(channels.contactId, contactId),
29-
eq(channels.deleted, false)
30-
)
31-
)
32-
.innerJoin(channelTypes, eq(channels.channelTypeId, channelTypes.id));
18+
const expectedChannel = await this.chanelService.findOneByContactId(contactId);
3319

3420
if (!expectedChannel) {
3521
throw new HttpException(
@@ -40,9 +26,9 @@ export class WebhookService {
4026

4127
let verifyResult = null;
4228

43-
if (expectedChannel[0]) {
44-
const { id, contactId, contactName, channelType, credentials } = expectedChannel[0];
45-
switch (expectedChannel[0].channelType) {
29+
if (expectedChannel) {
30+
const { id, contactId, contactName, channelType, credentials } = expectedChannel;
31+
switch (channelType) {
4632
case 'MSG':
4733
const messengerChannel = new MessengerChannel(id, contactId, contactName, channelType, credentials);
4834
verifyResult = messengerChannel.verifyWebhook(req, res);
@@ -58,4 +44,29 @@ export class WebhookService {
5844

5945
return verifyResult;
6046
}
47+
48+
public async handleIncomingMessage(contactId: string, req: Request, res: Response) {
49+
const expectedChannel = await this.chanelService.findOneByContactId(contactId);
50+
51+
if (!expectedChannel) {
52+
console.log('Incoming message: Can not find channel with id ', req.params.id);
53+
return;
54+
}
55+
56+
const { id, contactName, channelType, credentials } = expectedChannel;
57+
58+
let prepareMessage = null;
59+
60+
switch (channelType) {
61+
case 'MSG':
62+
const messengerChannel = new MessengerChannel(id, contactId, contactName, channelType, credentials);
63+
prepareMessage = await messengerChannel.prepareMessage(req, res);
64+
break;
65+
default:
66+
console.log(`Incoming message: Does not support channel type ${channelType}`);
67+
break;
68+
}
69+
70+
return prepareMessage;
71+
}
6172
}

0 commit comments

Comments
 (0)