Skip to content

Commit c80143c

Browse files
committed
whatsapp verify token issue fix
1 parent b671bfa commit c80143c

File tree

6 files changed

+160
-117
lines changed

6 files changed

+160
-117
lines changed

app/ui/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "app",
33
"private": true,
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dialoqbase",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Create chatbots with ease",
55
"scripts": {
66
"ui:dev": "pnpm run --filter ui dev",

server/src/routes/api/v1/bot/integration/handlers/post.handler.ts

-90
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ import { FastifyReply, FastifyRequest } from "fastify";
22
import {
33
createIntergationType,
44
PauseIntergationType,
5-
WhatsAppIntergationBodyType,
6-
WhatsAppIntergationType,
75
} from "./type";
86
import { geProviderRequiredFields } from "../../../../../../utils/intergation";
97

108
import TelegramBot from "../../../../../../integration/telegram";
119
import DiscordBot from "../../../../../../integration/discord";
1210
import WhatsappBot from "../../../../../../integration/whatsapp";
1311

14-
import * as PubSub from "pubsub-js";
1512
import SlackBot from "../../../../../../integration/slack";
1613
// import { writeFile } from "fs";
1714

@@ -432,90 +429,3 @@ export const pauseOrResumeIntergationHandler = async (
432429
});
433430
}
434431
};
435-
436-
export const whatsappIntergationHandler = async (
437-
request: FastifyRequest<WhatsAppIntergationType>,
438-
reply: FastifyReply
439-
) => {
440-
// req.query['hub.mode'] == 'subscribe' &&
441-
// req.query['hub.verify_token'] == 1234
442-
if (
443-
request.query["hub.mode"] == "subscribe" &&
444-
request.query["hub.verify_token"] == "1234"
445-
) {
446-
return reply.status(200).send(request.query["hub.challenge"]);
447-
}
448-
449-
console.log(JSON.stringify(request.body, null, 2));
450-
return reply.status(200).send({
451-
message: "Integration created",
452-
});
453-
};
454-
455-
export const whatsappIntergationHandlerPost = async (
456-
req: FastifyRequest<WhatsAppIntergationBodyType>,
457-
reply: FastifyReply
458-
) => {
459-
try {
460-
const signature = req.headers["x-hub-signature"];
461-
462-
if (!signature) {
463-
return reply.status(400).send({
464-
message: "Invalid request",
465-
});
466-
}
467-
468-
const prisma = req.server.prisma;
469-
470-
const isBot = await prisma.botIntegration.findFirst({
471-
where: {
472-
bot_id: req.params.id,
473-
provider: "whatsapp",
474-
},
475-
});
476-
477-
if (!isBot) {
478-
return reply.status(404).send({
479-
message: "Bot not found",
480-
});
481-
}
482-
483-
if (!req.body.object || !req.body.entry?.[0]?.changes?.[0]?.value) {
484-
return reply.status(400).send({
485-
message: "Invalid request",
486-
});
487-
}
488-
489-
if (req.body?.entry?.[0]?.changes?.[0]?.value?.statuses) {
490-
return reply.status(202).send();
491-
}
492-
493-
const { from, id, timestamp, type, ...rest } =
494-
req.body.entry[0].changes[0].value.messages[0];
495-
const identifer =
496-
req.body.entry[0].changes[0].value.metadata.phone_number_id;
497-
498-
let event: string | undefined = undefined;
499-
let data: any | undefined = undefined;
500-
501-
switch (type) {
502-
case "text":
503-
event = "message";
504-
data = { text: rest.text?.body, identifer: identifer, from, id };
505-
break;
506-
default:
507-
break;
508-
}
509-
510-
if (event && data) {
511-
// console.log("publishing to", req.params.id, { event, data });
512-
PubSub.publish(req.params.id, data);
513-
}
514-
515-
// console.log(JSON.stringify(req.body, null, 2));
516-
return reply.status(200).send();
517-
} catch (error) {
518-
console.log(error);
519-
return reply.status(200).send();
520-
}
521-
};

server/src/routes/api/v1/bot/integration/handlers/type.ts

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export type WhatsAppIntergationType = {
6565
"hub.verify_token": string;
6666
"hub.mode": string;
6767
"hub.challenge": string;
68+
},
69+
Params: {
70+
id: string;
6871
}
6972
}
7073

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { FastifyReply, FastifyRequest } from "fastify";
2+
import { WhatsAppIntergationBodyType, WhatsAppIntergationType } from "./type";
3+
import * as PubSub from "pubsub-js";
4+
5+
export const whatsappIntergationHandler = async (
6+
request: FastifyRequest<WhatsAppIntergationType>,
7+
reply: FastifyReply
8+
) => {
9+
const prisma = request.server.prisma;
10+
11+
const bot = await prisma.botIntegration.findFirst({
12+
where: {
13+
bot_id: request.params.id,
14+
provider: "whatsapp",
15+
},
16+
});
17+
18+
if (!bot) {
19+
return reply.status(404).send({
20+
message: "Bot not found",
21+
});
22+
}
23+
24+
if (
25+
request.query["hub.mode"] == "subscribe" &&
26+
request.query["hub.verify_token"] == bot.whatsapp_verify_token
27+
) {
28+
return reply.status(200).send(request.query["hub.challenge"]);
29+
}
30+
31+
console.log(JSON.stringify(request.body, null, 2));
32+
return reply.status(200).send({
33+
message: "Integration created",
34+
});
35+
};
36+
37+
export const whatsappIntergationHandlerPost = async (
38+
req: FastifyRequest<WhatsAppIntergationBodyType>,
39+
reply: FastifyReply
40+
) => {
41+
try {
42+
const signature = req.headers["x-hub-signature"];
43+
44+
if (!signature) {
45+
return reply.status(400).send({
46+
message: "Invalid request",
47+
});
48+
}
49+
50+
const prisma = req.server.prisma;
51+
52+
const isBot = await prisma.botIntegration.findFirst({
53+
where: {
54+
bot_id: req.params.id,
55+
provider: "whatsapp",
56+
},
57+
});
58+
59+
if (!isBot) {
60+
return reply.status(404).send({
61+
message: "Bot not found",
62+
});
63+
}
64+
65+
if (!req.body.object || !req.body.entry?.[0]?.changes?.[0]?.value) {
66+
return reply.status(400).send({
67+
message: "Invalid request",
68+
});
69+
}
70+
71+
if (req.body?.entry?.[0]?.changes?.[0]?.value?.statuses) {
72+
return reply.status(202).send();
73+
}
74+
75+
const { from, id, timestamp, type, ...rest } =
76+
req.body.entry[0].changes[0].value.messages[0];
77+
const identifer =
78+
req.body.entry[0].changes[0].value.metadata.phone_number_id;
79+
80+
let event: string | undefined = undefined;
81+
let data: any | undefined = undefined;
82+
83+
switch (type) {
84+
case "text":
85+
event = "message";
86+
data = { text: rest.text?.body, identifer: identifer, from, id };
87+
break;
88+
default:
89+
break;
90+
}
91+
92+
if (event && data) {
93+
// console.log("publishing to", req.params.id, { event, data });
94+
PubSub.publish(req.params.id, data);
95+
}
96+
97+
// console.log(JSON.stringify(req.body, null, 2));
98+
return reply.status(200).send();
99+
} catch (error) {
100+
console.log(error);
101+
return reply.status(200).send();
102+
}
103+
};

server/src/routes/api/v1/bot/integration/index.ts

+52-25
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { FastifyPluginAsync } from "fastify";
22
import {
33
createIntergationHandler,
44
pauseOrResumeIntergationHandler,
5-
whatsappIntergationHandler,
6-
whatsappIntergationHandlerPost,
75
} from "./handlers/post.handler";
86
import {
97
createIntergationSchema,
@@ -13,6 +11,11 @@ import {
1311
regenerateAPIKeySchema,
1412
} from "./schema";
1513

14+
import {
15+
whatsappIntergationHandler,
16+
whatsappIntergationHandlerPost,
17+
} from "./handlers/whatsapp.handler";
18+
1619
import {
1720
generateAPIKeyHandler,
1821
getAPIIntegrationHandler,
@@ -22,42 +25,66 @@ import { getChannelsByProvider } from "./handlers/get.handler";
2225

2326
const root: FastifyPluginAsync = async (fastify, _): Promise<void> => {
2427
// create integration for channel
25-
fastify.post("/:id", {
26-
schema: createIntergationSchema,
27-
onRequest: [fastify.authenticate],
28-
}, createIntergationHandler);
28+
fastify.post(
29+
"/:id",
30+
{
31+
schema: createIntergationSchema,
32+
onRequest: [fastify.authenticate],
33+
},
34+
createIntergationHandler
35+
);
2936
// pause or resume integration
30-
fastify.post("/:id/toggle", {
31-
schema: pauseOrResumeIntergationSchema,
32-
onRequest: [fastify.authenticate],
33-
}, pauseOrResumeIntergationHandler);
37+
fastify.post(
38+
"/:id/toggle",
39+
{
40+
schema: pauseOrResumeIntergationSchema,
41+
onRequest: [fastify.authenticate],
42+
},
43+
pauseOrResumeIntergationHandler
44+
);
3445

3546
// return all bot channels
36-
fastify.get("/:id", {
37-
onRequest: [fastify.authenticate],
38-
}, getChannelsByProvider);
47+
fastify.get(
48+
"/:id",
49+
{
50+
onRequest: [fastify.authenticate],
51+
},
52+
getChannelsByProvider
53+
);
3954

4055
// whatsapp integration
4156
fastify.get("/:id/whatsapp", {}, whatsappIntergationHandler);
4257
fastify.post("/:id/whatsapp", {}, whatsappIntergationHandlerPost);
4358

4459
// api key integration
45-
fastify.get("/:id/api", {
46-
schema: getAPIIntegrationSchema,
47-
onRequest: [fastify.authenticate],
48-
}, getAPIIntegrationHandler);
60+
fastify.get(
61+
"/:id/api",
62+
{
63+
schema: getAPIIntegrationSchema,
64+
onRequest: [fastify.authenticate],
65+
},
66+
getAPIIntegrationHandler
67+
);
4968

5069
// generate api key
51-
fastify.post("/:id/api", {
52-
schema: generateAPIKeySchema,
53-
onRequest: [fastify.authenticate],
54-
}, generateAPIKeyHandler);
70+
fastify.post(
71+
"/:id/api",
72+
{
73+
schema: generateAPIKeySchema,
74+
onRequest: [fastify.authenticate],
75+
},
76+
generateAPIKeyHandler
77+
);
5578

5679
// regenerate api key
57-
fastify.put("/:id/api", {
58-
schema: regenerateAPIKeySchema,
59-
onRequest: [fastify.authenticate],
60-
}, regenerateAPIKeyHandler);
80+
fastify.put(
81+
"/:id/api",
82+
{
83+
schema: regenerateAPIKeySchema,
84+
onRequest: [fastify.authenticate],
85+
},
86+
regenerateAPIKeyHandler
87+
);
6188
};
6289

6390
export default root;

0 commit comments

Comments
 (0)