|
| 1 | +import { |
| 2 | + ProviderAdapter, |
| 3 | + ProviderSendOptions, |
| 4 | + SmsResponse, |
| 5 | + AutomasConfig, |
| 6 | +} from "../type"; |
| 7 | +import { readEnv } from "../utils/env"; |
| 8 | + |
| 9 | +const DEFAULT_API_BASE_URL = "https://api.automas.sms.com"; |
| 10 | + |
| 11 | +function resolveConfig(config?: AutomasConfig): AutomasConfig { |
| 12 | + return { |
| 13 | + apiKey: config?.apiKey ?? readEnv("AUTOMAS_API_KEY"), |
| 14 | + senderId: config?.senderId ?? readEnv("AUTOMAS_SENDER_ID"), |
| 15 | + apiBaseUrl: config?.apiBaseUrl ?? readEnv("AUTOMAS_API_BASE_URL") ?? DEFAULT_API_BASE_URL, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +export const automasAdapter: ProviderAdapter<AutomasConfig> = { |
| 20 | + id: "automas", |
| 21 | + async send(options: ProviderSendOptions<AutomasConfig>): Promise<SmsResponse> { |
| 22 | + const config = resolveConfig(options.config); |
| 23 | + |
| 24 | + if (!config.apiKey) { |
| 25 | + return { |
| 26 | + success: false, |
| 27 | + provider: "automas", |
| 28 | + error: "Automas apiKey is missing. Provide apiKey or set AUTOMAS_API_KEY.", |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + const recipients = options.message.to; |
| 33 | + if (!recipients || recipients.length === 0) { |
| 34 | + return { |
| 35 | + success: false, |
| 36 | + provider: "automas", |
| 37 | + error: "At least one recipient is required.", |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + // Automas accepts multiple recipients as comma-separated |
| 42 | + const toParam = recipients.join(","); |
| 43 | + |
| 44 | + const params = new URLSearchParams({ |
| 45 | + api_key: config.apiKey, |
| 46 | + to: toParam, |
| 47 | + message: options.message.message, |
| 48 | + } as Record<string, string>); |
| 49 | + |
| 50 | + if (options.message.senderId || config.senderId) { |
| 51 | + params.set("sender_id", options.message.senderId ?? config.senderId ?? ""); |
| 52 | + } |
| 53 | + |
| 54 | + // If message may include scheduling or campaign tracking, allow passing via message object extras |
| 55 | + // The library doesn't define extras on SmsMessage; users can pass schedule via message.senderId pattern or config. |
| 56 | + |
| 57 | + const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL; |
| 58 | + const url = `${baseUrl.replace(/\/$/, "")}/sms/send?${params.toString()}`; |
| 59 | + |
| 60 | + let res: Response; |
| 61 | + try { |
| 62 | + res = await fetch(url, { method: "GET" }); |
| 63 | + } catch (err) { |
| 64 | + return { |
| 65 | + success: false, |
| 66 | + provider: "automas", |
| 67 | + error: "Network error when contacting Automas.", |
| 68 | + data: err instanceof Error ? err.message : String(err), |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + let data: unknown; |
| 73 | + try { |
| 74 | + data = await res.json(); |
| 75 | + } catch { |
| 76 | + data = await res.text(); |
| 77 | + } |
| 78 | + |
| 79 | + // Automas example response contains `error: 0` when accepted |
| 80 | + const accepted = typeof data === "object" && data !== null && (data as any).error === 0; |
| 81 | + |
| 82 | + return { |
| 83 | + success: !!accepted && res.ok, |
| 84 | + provider: "automas", |
| 85 | + statusCode: res.status, |
| 86 | + data, |
| 87 | + error: accepted || res.ok ? undefined : "Automas request failed.", |
| 88 | + }; |
| 89 | + }, |
| 90 | +}; |
0 commit comments