Skip to content
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
1 change: 1 addition & 0 deletions packages/blocks/bubbles/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export enum BubbleBlockType {
VIDEO = "video",
EMBED = "embed",
AUDIO = "audio",
LOCATION = "location",
}
22 changes: 14 additions & 8 deletions packages/whatsapp/src/convertMessageToWhatsAppMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Props = {
message: ContinueChatResponse["messages"][number];
mediaCache?: UploadMediaCache;
};

export const convertMessageToWhatsAppMessage = async ({
message,
mediaCache,
Expand All @@ -39,13 +40,11 @@ export const convertMessageToWhatsAppMessage = async ({
case BubbleBlockType.IMAGE: {
if (!message.content.url || isImageUrlNotCompatible(message.content.url))
return null;

if (mediaCache) {
const mediaId = await getOrUploadMedia({
url: message.content.url,
cache: mediaCache,
});

if (mediaId) {
return {
type: "image",
Expand All @@ -55,7 +54,6 @@ export const convertMessageToWhatsAppMessage = async ({
};
}
}

return {
type: "image",
image: {
Expand All @@ -65,13 +63,11 @@ export const convertMessageToWhatsAppMessage = async ({
}
case BubbleBlockType.AUDIO:
if (!message.content.url) return null;

if (mediaCache) {
const mediaId = await getOrUploadMedia({
url: message.content.url,
cache: mediaCache,
});

if (mediaId) {
return {
type: "audio",
Expand All @@ -81,7 +77,6 @@ export const convertMessageToWhatsAppMessage = async ({
};
}
}

return {
type: "audio",
audio: {
Expand All @@ -97,7 +92,6 @@ export const convertMessageToWhatsAppMessage = async ({
url: message.content.url,
cache: mediaCache,
});

if (mediaId) {
return {
type: "video",
Expand All @@ -107,7 +101,6 @@ export const convertMessageToWhatsAppMessage = async ({
};
}
}

// Fall back to using the URL if pre-upload fails or credentials not provided
return {
type: "video",
Expand All @@ -129,6 +122,19 @@ export const convertMessageToWhatsAppMessage = async ({
},
};
return null;
case BubbleBlockType.LOCATION: {
// Handle location messages
if (!message.content.latitude || !message.content.longitude) return null;
return {
type: "location",
location: {
latitude: message.content.latitude,
longitude: message.content.longitude,
name: message.content.name,
address: message.content.address,
},
};
}
case BubbleBlockType.EMBED:
if (!message.content.url) return null;
return {
Expand Down
12 changes: 11 additions & 1 deletion packages/whatsapp/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ const sendingMessageSchema = z.discriminatedUnion("type", [
type: z.literal("video"),
video: mediaSchema,
}),
z.object({
type: z.literal("location"),
location: z.object({
latitude: z.number(),
longitude: z.number(),
name: z.string().optional(),
address: z.string().optional(),
}),
}),
z.object({
type: z.literal("interactive"),
interactive: interactiveSchema,
Expand All @@ -90,6 +99,7 @@ const incomingMessageReferral = z.object({
ctwa_clid: z.string().optional(),
source_id: z.string().optional(),
});

export type WhatsAppMessageReferral = z.infer<typeof incomingMessageReferral>;

const sharedIncomingMessageFieldsSchema = z.object({
Expand Down Expand Up @@ -274,7 +284,7 @@ const whatsAppComparisonSchema = z.object({
comparisonOperator: z.nativeEnum(ComparisonOperators).optional(),
value: z.string().optional(),
});
export type WhatsAppComparison = z.infer<typeof whatsAppComparisonSchema>;

export type WhatsAppComparison = z.infer<typeof whatsAppComparisonSchema>;
export type WhatsAppIncomingMessage = z.infer<typeof incomingMessageSchema>;
export type WhatsAppSendingMessage = z.infer<typeof sendingMessageSchema>;