Amba-Agent-App/lib/services/twilioService.ts
2026-01-16 00:22:35 +03:00

66 lines
2.5 KiB
TypeScript

/**
* Twilio Service for Client-side Usage
* Provides easy-to-use functions for SMS and WhatsApp messaging
* Uses Firebase abstraction layer for cross-platform support
*/
import { httpsCallable } from '../firebase';
// SMS Functions
export const sendSMS = async (to: string, message: string, from?: string) => {
const sendSMSFunction = httpsCallable('sendSMS');
return await sendSMSFunction({ to, message, from });
};
export const sendBulkSMS = async (recipients: string[], message: string, from?: string) => {
const sendBulkSMSFunction = httpsCallable('sendBulkSMS');
return await sendBulkSMSFunction({ recipients, message, from });
};
// WhatsApp Functions
export const sendWhatsApp = async (to: string, message: string, from?: string) => {
const sendWhatsAppFunction = httpsCallable('sendWhatsApp');
return await sendWhatsAppFunction({ to, message, from });
};
export const sendBulkWhatsApp = async (recipients: string[], message: string, from?: string) => {
const sendBulkWhatsAppFunction = httpsCallable('sendBulkWhatsApp');
return await sendBulkWhatsAppFunction({ recipients, message, from });
};
// Utility Functions
export const getMessageStatus = async (messageSid: string) => {
const getMessageStatusFunction = httpsCallable('getMessageStatus');
return await getMessageStatusFunction({ messageSid });
};
export const getMessageHistory = async (from?: string, to?: string, limit?: number) => {
const getMessageHistoryFunction = httpsCallable('getMessageHistory');
return await getMessageHistoryFunction({ from, to, limit });
};
export const validatePhoneNumber = async (phoneNumber: string, countryCode?: string) => {
const validatePhoneNumberFunction = httpsCallable('validatePhoneNumber');
return await validatePhoneNumberFunction({ phoneNumber, countryCode });
};
// Helper function to format phone numbers for WhatsApp
export const formatWhatsAppNumber = (phoneNumber: string): string => {
if (phoneNumber.startsWith("whatsapp:")) {
return phoneNumber;
}
return `whatsapp:${phoneNumber}`;
};
// Helper function to format phone numbers for SMS
export const formatSMSNumber = (phoneNumber: string): string => {
const cleaned = phoneNumber.replace(/\D/g, "");
if (cleaned.length === 10) {
return `+1${cleaned}`;
} else if (cleaned.length === 11 && cleaned.startsWith("1")) {
return `+${cleaned}`;
} else if (cleaned.length > 10) {
return `+${cleaned}`;
}
return phoneNumber;
};