Skip to content

Commit cc08706

Browse files
committed
provider based doc added
1 parent 1678a48 commit cc08706

2 files changed

Lines changed: 257 additions & 4 deletions

File tree

readme.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,149 @@ Current provider ids:
132132
- `smsnetbd`
133133
- `bulksmsbd`
134134

135+
## Provider Examples
136+
137+
```typescript
138+
import { sendSms } from "sms-kit";
139+
140+
// Twilio
141+
await sendSms({
142+
provider: "twilio",
143+
message: {
144+
to: ["+15005550006"],
145+
message: "Twilio test message",
146+
},
147+
config: {
148+
accountSid: "your_twilio_sid",
149+
authToken: "your_twilio_token",
150+
from: "+15005550006",
151+
},
152+
});
153+
```
154+
155+
```typescript
156+
import { sendSms } from "sms-kit";
157+
158+
// MessageBird
159+
await sendSms({
160+
provider: "messagebird",
161+
message: {
162+
to: ["+8801712345678"],
163+
message: "MessageBird test message",
164+
},
165+
config: {
166+
accessKey: "your_messagebird_key",
167+
originator: "MyBrand",
168+
},
169+
});
170+
```
171+
172+
```typescript
173+
import { sendSms } from "sms-kit";
174+
175+
// SMS.to
176+
await sendSms({
177+
provider: "smsto",
178+
message: {
179+
to: ["+8801712345678"],
180+
message: "SMS.to test message",
181+
},
182+
config: {
183+
apiKey: "your_smsto_key",
184+
senderId: "MyBrand",
185+
},
186+
});
187+
```
188+
189+
```typescript
190+
import { sendSms } from "sms-kit";
191+
192+
// Textlocal
193+
await sendSms({
194+
provider: "textlocal",
195+
message: {
196+
to: ["+447000000000"],
197+
message: "Textlocal test message",
198+
},
199+
config: {
200+
apiKey: "your_textlocal_key",
201+
sender: "MyBrand",
202+
},
203+
});
204+
```
205+
206+
```typescript
207+
import { sendSms } from "sms-kit";
208+
209+
// BulkSMS
210+
await sendSms({
211+
provider: "bulksms",
212+
message: {
213+
to: ["+447000000000"],
214+
message: "BulkSMS test message",
215+
},
216+
config: {
217+
tokenId: "your_bulksms_token_id",
218+
tokenSecret: "your_bulksms_token_secret",
219+
},
220+
});
221+
```
222+
223+
```typescript
224+
import { sendSms } from "sms-kit";
225+
226+
// MiMSMS
227+
await sendSms({
228+
provider: "mimsms",
229+
message: {
230+
to: ["88018XXXXXXXX"],
231+
message: "MiMSMS test message",
232+
senderId: "MiM SMS",
233+
},
234+
config: {
235+
username: "you@example.com",
236+
apiKey: "your_mimsms_key",
237+
transactionType: "T",
238+
campaignId: null,
239+
},
240+
});
241+
```
242+
243+
```typescript
244+
import { sendSms } from "sms-kit";
245+
246+
// sms.net.bd (Alpha SMS)
247+
await sendSms({
248+
provider: "smsnetbd",
249+
message: {
250+
to: ["8801800000000", "8801700000000"],
251+
message: "sms.net.bd test message",
252+
senderId: "MyBrand",
253+
},
254+
config: {
255+
apiKey: "your_smsnetbd_key",
256+
schedule: "2021-10-13 16:00:52",
257+
},
258+
});
259+
```
260+
261+
```typescript
262+
import { sendSms } from "sms-kit";
263+
264+
// BulkSMSBD
265+
await sendSms({
266+
provider: "bulksmsbd",
267+
message: {
268+
to: ["88017XXXXXXXX"],
269+
message: "BulkSMSBD test message",
270+
senderId: "8809617626719",
271+
},
272+
config: {
273+
apiKey: "your_bulksmsbd_key",
274+
},
275+
});
276+
```
277+
135278
## License
136279

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

src/providers/twilio.ts

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,58 @@ import {
66
} from "../type";
77
import { readEnv } from "../utils/env";
88

9+
const DEFAULT_API_BASE_URL = "https://api.twilio.com";
10+
911
function resolveConfig(config?: TwilioConfig): TwilioConfig {
1012
return {
1113
accountSid: config?.accountSid ?? readEnv("TWILIO_ACCOUNT_SID"),
1214
authToken: config?.authToken ?? readEnv("TWILIO_AUTH_TOKEN"),
1315
from: config?.from ?? readEnv("TWILIO_FROM"),
14-
apiBaseUrl: config?.apiBaseUrl ?? readEnv("TWILIO_API_BASE_URL"),
16+
apiBaseUrl:
17+
config?.apiBaseUrl ?? readEnv("TWILIO_API_BASE_URL") ?? DEFAULT_API_BASE_URL,
1518
};
1619
}
1720

21+
function buildAuthHeader(accountSid: string, authToken: string): string {
22+
return `Basic ${Buffer.from(`${accountSid}:${authToken}`).toString("base64")}`;
23+
}
24+
25+
async function sendSingleMessage(
26+
config: Required<Pick<TwilioConfig, "accountSid" | "authToken" | "from" | "apiBaseUrl">>,
27+
to: string,
28+
body: string
29+
): Promise<{ ok: boolean; status: number; data: unknown }>
30+
{
31+
const url = new URL(
32+
`/2010-04-01/Accounts/${config.accountSid}/Messages.json`,
33+
config.apiBaseUrl
34+
);
35+
36+
const form = new URLSearchParams({
37+
To: to,
38+
From: config.from,
39+
Body: body,
40+
});
41+
42+
const response = await fetch(url.toString(), {
43+
method: "POST",
44+
headers: {
45+
Authorization: buildAuthHeader(config.accountSid, config.authToken),
46+
"Content-Type": "application/x-www-form-urlencoded",
47+
},
48+
body: form.toString(),
49+
});
50+
51+
let data: unknown;
52+
try {
53+
data = await response.json();
54+
} catch {
55+
data = await response.text();
56+
}
57+
58+
return { ok: response.ok, status: response.status, data };
59+
}
60+
1861
export const twilioAdapter: ProviderAdapter<TwilioConfig> = {
1962
id: "twilio",
2063
async send(options: ProviderSendOptions<TwilioConfig>): Promise<SmsResponse> {
@@ -29,11 +72,78 @@ export const twilioAdapter: ProviderAdapter<TwilioConfig> = {
2972
};
3073
}
3174

75+
if (!config.from) {
76+
return {
77+
success: false,
78+
provider: "twilio",
79+
error:
80+
"Twilio sender is missing. Provide from or set TWILIO_FROM.",
81+
};
82+
}
83+
84+
const recipients = options.message.to;
85+
if (recipients.length === 0) {
86+
return {
87+
success: false,
88+
provider: "twilio",
89+
error: "At least one recipient is required.",
90+
};
91+
}
92+
93+
const resolvedConfig = {
94+
accountSid: config.accountSid,
95+
authToken: config.authToken,
96+
from: config.from,
97+
apiBaseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL,
98+
};
99+
100+
if (recipients.length === 1) {
101+
const result = await sendSingleMessage(
102+
resolvedConfig,
103+
recipients[0],
104+
options.message.message
105+
);
106+
107+
if (!result.ok) {
108+
return {
109+
success: false,
110+
provider: "twilio",
111+
statusCode: result.status,
112+
error: "Twilio request failed.",
113+
data: result.data,
114+
};
115+
}
116+
117+
return {
118+
success: true,
119+
provider: "twilio",
120+
statusCode: result.status,
121+
data: result.data,
122+
};
123+
}
124+
125+
const results = [] as Array<{ to: string; ok: boolean; status: number; data: unknown }>;
126+
let hasFailure = false;
127+
128+
for (const to of recipients) {
129+
const result = await sendSingleMessage(
130+
resolvedConfig,
131+
to,
132+
options.message.message
133+
);
134+
results.push({ to, ok: result.ok, status: result.status, data: result.data });
135+
if (!result.ok) {
136+
hasFailure = true;
137+
}
138+
}
139+
32140
return {
33-
success: false,
141+
success: !hasFailure,
34142
provider: "twilio",
35-
error:
36-
"Twilio adapter is scaffolded. Add request details in src/providers/twilio.ts.",
143+
statusCode: hasFailure ? 207 : 200,
144+
data: results,
145+
error: hasFailure ? "One or more Twilio requests failed." : undefined,
37146
};
147+
38148
},
39149
};

0 commit comments

Comments
 (0)