Compare commits
2 Commits
8af4397803
...
176309131f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
176309131f | ||
|
|
c6200f3816 |
@@ -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,18 @@ 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 nestedContent =
|
||||
data.message && typeof data.message === 'object'
|
||||
? ((data.message as Record<string, unknown>).content as Record<string, unknown> | undefined)
|
||||
: undefined;
|
||||
const nestedDirectUrl = (nestedContent?.url as string) || '';
|
||||
const nestedImageUrl = (nestedContent?.image as Record<string, unknown> | undefined)?.url as string;
|
||||
const nestedVideoUrl = (nestedContent?.video as Record<string, unknown> | undefined)?.url as string;
|
||||
const nestedFileUrl = (nestedContent?.file as Record<string, unknown> | undefined)?.url as string;
|
||||
for (const url of [nestedDirectUrl, nestedImageUrl, nestedVideoUrl, nestedFileUrl]) {
|
||||
if (url) mediaUrls.push(url);
|
||||
}
|
||||
|
||||
const hasMedia = mediaUrls.length > 0 || channel === 'mms';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user