Files
simstudioai--sim/apps/sim/lib/messaging/sms/service.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

186 lines
4.6 KiB
TypeScript

import { createLogger } from '@sim/logger'
import { getErrorMessage } from '@sim/utils/errors'
import { truncate } from '@sim/utils/string'
import { Twilio } from 'twilio'
import { env } from '@/lib/core/config/env'
const logger = createLogger('SMSService')
export interface SMSOptions {
to: string | string[]
body: string
from?: string
}
interface BatchSMSOptions {
messages: SMSOptions[]
}
interface SMSResponseData {
sid?: string
status?: string
to?: string
from?: string
id?: string
results?: SendSMSResult[]
count?: number
}
export interface SendSMSResult {
success: boolean
message: string
data?: SMSResponseData
}
interface BatchSendSMSResult {
success: boolean
message: string
results: SendSMSResult[]
data?: SMSResponseData
}
const twilioAccountSid = env.TWILIO_ACCOUNT_SID
const twilioAuthToken = env.TWILIO_AUTH_TOKEN
const twilioPhoneNumber = env.TWILIO_PHONE_NUMBER
const twilioClient =
twilioAccountSid &&
twilioAuthToken &&
twilioAccountSid.trim() !== '' &&
twilioAuthToken.trim() !== ''
? new Twilio(twilioAccountSid, twilioAuthToken)
: null
export async function sendSMS(options: SMSOptions): Promise<SendSMSResult> {
try {
const { to, body, from } = options
const fromNumber = from || twilioPhoneNumber
if (!fromNumber || fromNumber.trim() === '') {
logger.error('No Twilio phone number configured')
return {
success: false,
message: 'SMS sending failed: No from phone number configured',
}
}
if (!twilioClient) {
logger.error('SMS sending failed: Twilio not configured', {
to,
body: truncate(body, 50),
from: fromNumber,
})
return {
success: false,
message:
'SMS sending failed: Twilio credentials not configured. Please set TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER in your environment variables.',
}
}
if (typeof to === 'string') {
return await sendSingleSMS(to, body, fromNumber)
}
const results: SendSMSResult[] = []
for (const phoneNumber of to) {
try {
const result = await sendSingleSMS(phoneNumber, body, fromNumber)
results.push(result)
} catch (error) {
results.push({
success: false,
message: getErrorMessage(error, 'Failed to send SMS'),
})
}
}
const successCount = results.filter((r) => r.success).length
return {
success: successCount === results.length,
message:
successCount === results.length
? 'All SMS messages sent successfully'
: `${successCount}/${results.length} SMS messages sent successfully`,
data: { results, count: successCount },
}
} catch (error) {
logger.error('Error sending SMS:', error)
return {
success: false,
message: 'Failed to send SMS',
}
}
}
async function sendSingleSMS(to: string, body: string, from: string): Promise<SendSMSResult> {
if (!twilioClient) {
throw new Error('Twilio client not configured')
}
try {
const message = await twilioClient.messages.create({
body,
from,
to,
})
logger.info('SMS sent successfully:', {
to,
from,
messageSid: message.sid,
status: message.status,
})
return {
success: true,
message: 'SMS sent successfully via Twilio',
data: {
sid: message.sid,
status: message.status,
to: message.to,
from: message.from,
},
}
} catch (error) {
logger.error('Failed to send SMS via Twilio:', error)
throw error
}
}
async function sendBatchSMS(options: BatchSMSOptions): Promise<BatchSendSMSResult> {
try {
const results: SendSMSResult[] = []
logger.info('Sending batch SMS messages')
for (const smsOptions of options.messages) {
try {
const result = await sendSMS(smsOptions)
results.push(result)
} catch (error) {
results.push({
success: false,
message: getErrorMessage(error, 'Failed to send SMS'),
})
}
}
const successCount = results.filter((r) => r.success).length
return {
success: successCount === results.length,
message:
successCount === results.length
? 'All batch SMS messages sent successfully'
: `${successCount}/${results.length} SMS messages sent successfully`,
results,
data: { count: successCount },
}
} catch (error) {
logger.error('Error in batch SMS sending:', error)
return {
success: false,
message: 'Failed to send batch SMS messages',
results: [],
}
}
}