31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
export interface ParsedInboundSms {
|
|
messageId: string;
|
|
from: string;
|
|
to: string;
|
|
text: string;
|
|
hasMedia: boolean;
|
|
mediaUrls: string[];
|
|
}
|
|
|
|
export function parseTelnyxInboundMessage(body: unknown): ParsedInboundSms | null {
|
|
try {
|
|
const data = body as Record<string, unknown>;
|
|
const eventData = (data.data as Record<string, unknown>) || data;
|
|
const payload = (eventData.payload as Record<string, unknown>) || eventData;
|
|
|
|
const from = ((payload.from as Record<string, unknown>)?.phone_number as string) || (payload.from as string) || '';
|
|
const to = (Array.isArray(payload.to) ? (payload.to[0] as Record<string, unknown>)?.phone_number : (payload.to as Record<string, unknown>)?.phone_number) as string || '';
|
|
const text = (payload.text as string) || (payload.body as string) || '';
|
|
const messageId = (payload.id as string) || (eventData.id as string) || '';
|
|
|
|
const media = (payload.media as Array<{ url: string }>) || [];
|
|
const mediaUrls = media.map(m => m.url).filter(Boolean);
|
|
|
|
if (!from || !text) return null;
|
|
|
|
return { messageId, from, to, text: text.trim(), hasMedia: mediaUrls.length > 0, mediaUrls };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|