Update body format

This commit is contained in:
khalid@traclabs.com
2026-05-05 12:57:03 -05:00
parent 8af4397803
commit c6200f3816

View File

@@ -26,12 +26,35 @@ export function parseVonageInboundMessage(body: unknown): ParsedInboundSms | nul
try {
const data = body as Record<string, unknown>;
const from = (data.from as string) || '';
const to = (data.to as string) || '';
const text = (data.text as string) || '';
// Vonage may send either a flat shape (from/to/text) or a nested one:
// {
// from: { type: 'sms', number: '1999...' },
// to: { type: 'sms', number: '1999...' },
// message: { content: { type: 'text', text: '...' } }
// }
const from =
(typeof data.from === 'string' ? data.from : '') ||
(data.from && typeof data.from === 'object'
? (((data.from as Record<string, unknown>).number as string) || '')
: '');
const to =
(typeof data.to === 'string' ? data.to : '') ||
(data.to && typeof data.to === 'object'
? (((data.to as Record<string, unknown>).number as string) || '')
: '');
const nestedText =
data.message && typeof data.message === 'object'
? (((data.message as Record<string, unknown>).content as Record<string, unknown> | undefined)?.text as string) || ''
: '';
const text = (data.text as string) || nestedText || '';
const messageId = (data.message_uuid as string) || '';
const messageType = (data.message_type as string) || '';
const channel = (data.channel as string) || 'sms';
const nestedMessageType =
data.message && typeof data.message === 'object'
? (((data.message as Record<string, unknown>).content as Record<string, unknown> | undefined)?.type as string) || ''
: '';
const messageType = (data.message_type as string) || nestedMessageType || '';
const channel = (data.channel as string) || ((data.to as Record<string, unknown> | undefined)?.type as string) || 'sms';
// Collect media URLs from MMS messages
const mediaUrls: string[] = [];
@@ -47,6 +70,12 @@ export function parseVonageInboundMessage(body: unknown): ParsedInboundSms | nul
const fileUrl = (data.file as Record<string, unknown>)?.url as string;
if (fileUrl) mediaUrls.push(fileUrl);
}
// Nested media shapes (best-effort)
const nestedMediaUrl =
data.message && typeof data.message === 'object'
? (((data.message as Record<string, unknown>).content as Record<string, unknown> | undefined)?.url as string) || ''
: '';
if (nestedMediaUrl) mediaUrls.push(nestedMediaUrl);
const hasMedia = mediaUrls.length > 0 || channel === 'mms';