Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/twilio integration #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ RAZORPAY_SECRET=""
RAZORPAY_WEBHOOK_SECRET=""

UPLOADTHING_TOKEN=""

TWILIO_ACCOUNT_SID=""
TWILIO_AUTH_TOKEN=""
TWILIO_WHATSAPP_NUMBER=""
65 changes: 65 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"node-html-to-image": "^5.0.0",
"nodemailer": "^6.9.16",
"razorpay": "^2.9.5",
"twilio": "^5.4.0",
"uploadthing": "^7.3.0",
"uuid": "^11.0.3",
"zod": "^3.23.8"
Expand Down
6 changes: 6 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const env = createEnv({
RAZORPAY_SECRET: z.string(),
RAZORPAY_WEBHOOK_SECRET: z.string(),
UPLOADTHING_TOKEN: z.string(),
TWILIO_ACCOUNT_SID: z.string(),
TWILIO_AUTH_TOKEN: z.string(),
TWILIO_WHATSAPP_NUMBER: z.string(),
},

/**
Expand Down Expand Up @@ -73,6 +76,9 @@ export const env = createEnv({
RAZORPAY_SECRET: process.env.RAZORPAY_SECRET,
RAZORPAY_WEBHOOK_SECRET: process.env.RAZORPAY_WEBHOOK_SECRET,
UPLOADTHING_TOKEN: process.env.UPLOADTHING_TOKEN,
TWILIO_ACCOUNT_SID: process.env.TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN: process.env.TWILIO_AUTH_TOKEN,
TWILIO_WHATSAPP_NUMBER: process.env.TWILIO_WHATSAPP_NUMBER,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
128 changes: 128 additions & 0 deletions src/graphql/models/Twilio/mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { builder } from "~/graphql/builder";
import { sendWhatsAppMessage } from "~/services/twilio.service";

builder.mutationField("sendEventRegistrationReminder", (t) =>
t.field({
type: "Boolean",
args: {
eventId: t.arg.id({ required: true }),
},
resolve: async (root, args, ctx) => {
try {
const user = await ctx.user;
if (!user) {
throw new Error("Not authenticated");
}
if (user.role !== "ORGANIZER") {
throw new Error("Not authorized to send WhatsApp notifications");
}

const event = await ctx.prisma.event.findUnique({
where: { id: Number(args.eventId) },
include: {
Teams: {
include: {
TeamMembers: {
include: {
User: true,
},
},
},
},
},
});

if (!event) {
throw new Error("Event not found");
}

for (const team of event.Teams) {
for (const member of team.TeamMembers) {
const contentVariables = JSON.stringify({
"1": member.User.name,
"2": event.name,
});
await sendWhatsAppMessage(
member.User.phoneNumber!,
"HX154ff0082b3bd18082e22c5301927562",
contentVariables,
);
}
}
return true;
} catch (error) {
console.error(
"Error in sendEventRegistrationReminder mutation:",
error,
);
throw new Error(`Unexpected error: ${(error as Error).message}`);
}
},
}),
);

builder.mutationField("sendWinnerWhatsAppNotification", (t) =>
t.field({
type: "Boolean",
args: {
eventId: t.arg.id({ required: true }),
},
resolve: async (root, args, ctx) => {
try {
const user = await ctx.user;
if (!user) {
throw new Error("Not authenticated");
}
if (user.role !== "ORGANIZER") {
throw new Error("Not authorized to send WhatsApp notifications");
}

const event = await ctx.prisma.event.findUnique({
where: { id: Number(args.eventId) },
include: {
Winner: {
include: {
Team: {
include: {
TeamMembers: {
include: {
User: true,
},
},
},
},
},
},
},
});

if (!event) {
throw new Error("Event not found");
}

for (const winner of event.Winner) {
for (const member of winner.Team.TeamMembers) {
const contentVariables = JSON.stringify({
"1": member.User.name,
"2": winner.Team.name,
"3": winner.type,
"4": event.name,
});
await sendWhatsAppMessage(
member.User.phoneNumber!,
"HX902d67cec04bbd3d3f9f0eb0c64a752a",
contentVariables,
);
}
}
return true;
} catch (error) {
console.error(
"Error in sendWinnerWhatsAppNotification mutation:",
error,
);
throw new Error(`Unexpected error: ${(error as Error).message}`);
}
},
}),
);
1 change: 1 addition & 0 deletions src/graphql/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ import "~/graphql/models/User";
import "~/graphql/models/UserInHotel";
import "~/graphql/models/Winner";
import "~/graphql/models/XP";
import "~/graphql/models/Twilio/mutation";
23 changes: 23 additions & 0 deletions src/services/twilio.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Twilio from "twilio";
import { env } from "~/env";

const client = Twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN);

export const sendWhatsAppMessage = async (
to: string,
contentSid: string,
contentVariables: string,
) => {
try {
const message = await client.messages.create({
from: env.TWILIO_WHATSAPP_NUMBER,
contentSid,
contentVariables,
to: `whatsapp:${to}`,
});
return message;
} catch (error) {
console.error("Error sending WhatsApp message:", error);
throw new Error("Failed to send WhatsApp message");
}
};