Skip to content

Commit b51be16

Browse files
committed
automas added
1 parent c6ba2b2 commit b51be16

4 files changed

Lines changed: 124 additions & 2 deletions

File tree

readme.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ SMSNETBD_CONTENT_ID=
9191
9292
BULKSMSBD_API_KEY=your_bulksmsbd_key
9393
BULKSMSBD_SENDER_ID=MyBrand
94+
95+
AUTOMAS_API_KEY=your_automas_key
96+
AUTOMAS_SENDER_ID=AUTOMAS
97+
AUTOMAS_API_BASE_URL=https://api.automas.sms.com
9498
```
9599

96100
## Error Handling
@@ -100,7 +104,7 @@ The `sendSms` function will return an object with the following structure:
100104
```typescript
101105
{
102106
success: boolean;
103-
provider: "twilio" | "messagebird" | "smsto" | "textlocal" | "bulksms" | "mimsms" | "smsnetbd" | "bulksmsbd" | "unknown";
107+
provider: "twilio" | "messagebird" | "smsto" | "textlocal" | "bulksms" | "mimsms" | "smsnetbd" | "bulksmsbd" | "automas" | "unknown";
104108
data?: unknown;
105109
error?: string;
106110
statusCode?: number;
@@ -131,6 +135,7 @@ Current provider ids:
131135
- `mimsms`
132136
- `smsnetbd`
133137
- `bulksmsbd`
138+
- `automas`
134139

135140
## Provider Examples
136141

@@ -275,6 +280,23 @@ await sendSms({
275280
});
276281
```
277282

283+
```typescript
284+
import { sendSms } from "sms-kit";
285+
286+
// Automas
287+
await sendSms({
288+
provider: "automas",
289+
message: {
290+
to: ["8801700000000", "8801800000000"],
291+
message: "Hello from Automas",
292+
senderId: "AUTOMAS",
293+
},
294+
config: {
295+
apiKey: "your_automas_key",
296+
},
297+
});
298+
```
299+
278300
## License
279301

280302
This package is open-source and available under the MIT License.

src/providers/automas.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
};

src/providers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { smsNetBdAdapter } from "./smsnetbd";
1111
import { smsToAdapter } from "./smsto";
1212
import { textlocalAdapter } from "./textlocal";
1313
import { twilioAdapter } from "./twilio";
14+
import { automasAdapter } from "./automas";
1415

1516
const providers: {
1617
[K in SmsProviderId]: ProviderAdapter<ProviderConfigMap[K]>;
@@ -23,6 +24,7 @@ const providers: {
2324
mimsms: mimSmsAdapter,
2425
smsnetbd: smsNetBdAdapter,
2526
bulksmsbd: bulksmsbdAdapter,
27+
automas: automasAdapter,
2628
};
2729

2830
export function getProviderAdapter<TProvider extends SmsProviderId>(

src/type.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export type SmsProviderId =
66
| "bulksms"
77
| "mimsms"
88
| "smsnetbd"
9-
| "bulksmsbd";
9+
| "bulksmsbd"
10+
| "automas";
1011

1112
export type SmsResponseProvider = SmsProviderId | "unknown";
1213

@@ -73,6 +74,12 @@ export type BulkSmsBdConfig = {
7374
apiBaseUrl?: string;
7475
};
7576

77+
export type AutomasConfig = {
78+
apiKey?: string;
79+
senderId?: string;
80+
apiBaseUrl?: string;
81+
};
82+
7683
export type ProviderConfigMap = {
7784
twilio: TwilioConfig;
7885
messagebird: MessageBirdConfig;
@@ -82,6 +89,7 @@ export type ProviderConfigMap = {
8289
mimsms: MimSmsConfig;
8390
smsnetbd: SmsNetBdConfig;
8491
bulksmsbd: BulkSmsBdConfig;
92+
automas: AutomasConfig;
8593
};
8694

8795
export type SendSmsOptions<TProvider extends SmsProviderId = SmsProviderId> = {

0 commit comments

Comments
 (0)