Skip to content

Commit 4165304

Browse files
authored
🤖 Merge PR DefinitelyTyped#74993 africastalking: rewrite against SDK 0.8.0 by @shottah
1 parent bd15be7 commit 4165304

3 files changed

Lines changed: 954 additions & 70 deletions

File tree

Lines changed: 342 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,354 @@
11
import AfricasTalking = require("africastalking");
22

3-
const africastalking = AfricasTalking({
3+
// =========================================================================
4+
// Client construction
5+
// =========================================================================
6+
7+
const client = AfricasTalking({
8+
apiKey: "apiKey",
9+
username: "username",
10+
});
11+
12+
const clientXml = AfricasTalking({
413
apiKey: "apiKey",
514
username: "username",
15+
format: "xml",
616
});
717

8-
const SMS = async () => {
9-
const token = africastalking.TOKEN;
10-
const sms = africastalking.SMS;
18+
const clientHandle: AfricasTalking.AfricasTalking = client;
19+
20+
// =========================================================================
21+
// SMS (sms.d.ts)
22+
// =========================================================================
23+
24+
async function smsSurface() {
25+
const sms: AfricasTalking.SMS = client.SMS;
26+
27+
const single: AfricasTalking.SMSResponse = await sms.send({
28+
to: "+254700000000",
29+
message: "hello",
30+
});
31+
const message: string = single.SMSMessageData.Message;
32+
const recipients: AfricasTalking.SMSRecipient[] = single.SMSMessageData.Recipients;
33+
const status: AfricasTalking.SMSRecipientStatus = recipients[0].status;
34+
const messageId: string = recipients[0].messageId;
35+
void message;
36+
void status;
37+
void messageId;
38+
39+
await sms.send({
40+
to: ["+254700000000", "+254700000001"],
41+
message: "hi",
42+
from: "AFRICASTKNG",
43+
senderId: "AFRICASTKNG",
44+
enqueue: true,
45+
});
46+
47+
const batch: AfricasTalking.SMSResponse[] = await sms.send([
48+
{ to: "+254700000000", message: "hi" },
49+
{ to: "+254700000001", message: "hi" },
50+
]);
51+
void batch;
52+
53+
const bulk: AfricasTalking.SMSResponse = await sms.sendBulk({
54+
to: "+254700000000",
55+
message: "bulk",
56+
});
57+
const bulkArray: AfricasTalking.SMSResponse[] = await sms.sendBulk([
58+
{ to: "+254700000000", message: "bulk" },
59+
]);
60+
void bulk;
61+
void bulkArray;
62+
63+
const premium: AfricasTalking.SMSResponse = await sms.sendPremium({
64+
to: "+254700000000",
65+
message: "premium",
66+
keyword: "premium",
67+
linkId: "linkId",
68+
retryDurationInHours: 1,
69+
});
70+
void premium;
71+
72+
const inbox: AfricasTalking.SMSFetchMessagesResponse = await sms.fetchMessages({
73+
lastReceivedId: 0,
74+
});
75+
const inboxMessages: AfricasTalking.SMSInboundMessage[] = inbox.SMSMessageData.Messages;
76+
void inboxMessages;
77+
78+
const createSub: AfricasTalking.SMSSubscriptionResponse = await sms.createSubscription({
79+
shortCode: "12345",
80+
keyword: "PREMIUM",
81+
phoneNumber: "+254700000000",
82+
});
83+
void createSub;
84+
85+
const fetchSub: AfricasTalking.SMSFetchSubscriptionResponse = await sms.fetchSubscription({
86+
shortCode: "12345",
87+
keyword: "PREMIUM",
88+
lastReceivedId: 0,
89+
});
90+
const subscribers: AfricasTalking.SMSSubscriber[] = fetchSub.responses;
91+
void subscribers;
92+
93+
const deleteSub: AfricasTalking.SMSDeleteSubscriptionResponse = await sms.deleteSubscription({
94+
shortCode: "12345",
95+
keyword: "PREMIUM",
96+
phoneNumber: "+254700000000",
97+
});
98+
void deleteSub;
99+
}
100+
101+
// =========================================================================
102+
// Token (token.d.ts)
103+
// =========================================================================
104+
105+
async function tokenSurface() {
106+
const token: AfricasTalking.TOKEN = client.TOKEN;
107+
const auth: AfricasTalking.AuthTokenResponse = await token.generateAuthToken();
108+
const t: string = auth.token;
109+
const lifetime: number = auth.lifetimeInSeconds;
110+
void t;
111+
void lifetime;
112+
}
113+
114+
// =========================================================================
115+
// Voice (voice.d.ts + actionbuilder.d.ts)
116+
// =========================================================================
117+
118+
async function voiceSurface() {
119+
const voice: AfricasTalking.VOICE = client.VOICE;
120+
121+
const call: AfricasTalking.VoiceCallResponse = await voice.call({
122+
callFrom: "+254700000000",
123+
callTo: ["+254700000001"],
124+
clientRequestId: "req_1",
125+
});
126+
const callEntries: AfricasTalking.VoiceCallEntry[] = call.entries;
127+
void callEntries;
128+
129+
const queued: AfricasTalking.VoiceQueuedCallsResponse = await voice.getNumQueuedCalls({
130+
phoneNumbers: ["+254700000000"],
131+
});
132+
void queued;
11133

12-
const sendSmsResult = await africastalking.SMS.send({
13-
to: "to",
14-
message: "message",
15-
from: "from",
134+
const upload: AfricasTalking.VoiceUploadMediaResponse = await voice.uploadMediaFile({
135+
url: "https://example.com/audio.wav",
136+
phoneNumber: "+254700000000",
16137
});
138+
void upload;
17139

18-
const checkoutTokenResponse = await token.createCheckoutToken("phoneNumber");
140+
const builder: AfricasTalking.ActionBuilder = new voice.ActionBuilder();
141+
const xml: string = builder
142+
.say("Welcome", { voice: "woman", playBeep: true })
143+
.play("https://example.com/jingle.mp3")
144+
.getDigits(
145+
{ say: { text: "Enter your pin", attributes: { voice: "woman" } } },
146+
{ finishOnKey: "#", numDigits: 4, callbackUrl: "https://example.com/cb" },
147+
)
148+
.dial("+254700000001", { record: true, sequential: true })
149+
.record(
150+
{ play: { url: "https://example.com/beep.mp3" } },
151+
{ finishOnKey: "#", maxLength: 30 },
152+
)
153+
.enqueue({ name: "support" })
154+
.dequeue("+254700000001", { name: "support" })
155+
.redirect("https://example.com/next")
156+
.conference()
157+
.reject()
158+
.build();
159+
void xml;
160+
}
19161

20-
const options = {
21-
// Set your premium product shortCode and keyword
22-
shortCode: "shortCode",
23-
keyword: "keyword",
24-
phoneNumber: "+phoneNumber",
25-
checkoutToken: checkoutTokenResponse.token,
162+
// =========================================================================
163+
// Airtime (airtime.d.ts)
164+
// =========================================================================
165+
166+
async function airtimeSurface() {
167+
const airtime: AfricasTalking.AIRTIME = client.AIRTIME;
168+
169+
const sendResult: AfricasTalking.AirtimeSendResponse = await airtime.send({
170+
idempotencyKey: "abc",
171+
maxNumRetry: 3,
172+
recipients: [
173+
{ phoneNumber: "+254700000000", currencyCode: "KES", amount: 50 },
174+
],
175+
});
176+
const responses: AfricasTalking.AirtimeResponseEntry[] = sendResult.responses;
177+
void responses;
178+
179+
const status: AfricasTalking.AirtimeTransactionStatusResponse = await airtime.findTransactionStatus(
180+
"tx_id",
181+
);
182+
const data: AfricasTalking.AirtimeTransactionData | undefined = status.data;
183+
void data;
184+
}
185+
186+
// =========================================================================
187+
// Application (application.d.ts)
188+
// =========================================================================
189+
190+
async function applicationSurface() {
191+
const app: AfricasTalking.APPLICATION = client.APPLICATION;
192+
const data: AfricasTalking.ApplicationDataResponse = await app.fetchApplicationData();
193+
const balance: string = data.UserData.balance;
194+
void balance;
195+
196+
const accountSurface: AfricasTalking.APPLICATION = client.ACCOUNT;
197+
const accountData: AfricasTalking.ApplicationDataResponse = await accountSurface.fetchAccount();
198+
void accountData;
199+
}
200+
201+
// =========================================================================
202+
// Insights (insights.d.ts)
203+
// =========================================================================
204+
205+
async function insightsSurface() {
206+
const insights: AfricasTalking.INSIGHTS = client.INSIGHTS;
207+
const oneNumber: AfricasTalking.InsightsSimSwapResponse = await insights.checkSimSwapState(
208+
"+254700000000",
209+
);
210+
const manyNumbers: AfricasTalking.InsightsSimSwapResponse = await insights.checkSimSwapState([
211+
"+254700000000",
212+
"+254700000001",
213+
]);
214+
const entries: AfricasTalking.InsightsSimSwapEntry[] = oneNumber.responses;
215+
void manyNumbers;
216+
void entries;
217+
}
218+
219+
// =========================================================================
220+
// Mobile data (mobileData.d.ts)
221+
// =========================================================================
222+
223+
async function mobileDataSurface() {
224+
const md: AfricasTalking.MOBILE_DATA = client.MOBILE_DATA;
225+
await md.send({
226+
productName: "DataBundle",
227+
idempotencyKey: "abc",
228+
recipients: [
229+
{
230+
phoneNumber: "+254700000000",
231+
quantity: 1,
232+
unit: "GB",
233+
validity: "Month",
234+
metadata: { userId: "user_1" },
235+
},
236+
],
237+
});
238+
await md.findTransaction({ transactionId: "tx_id" });
239+
await md.fetchWalletBalance();
240+
}
241+
242+
// =========================================================================
243+
// WhatsApp (whatsapp.d.ts)
244+
// =========================================================================
245+
246+
async function whatsappSurface() {
247+
const wa: AfricasTalking.WHATSAPP = client.WHATSAPP;
248+
249+
await wa.sendMessage({
250+
waNumber: "wa_number",
251+
phoneNumber: "+254700000000",
252+
body: { message: "hello" },
253+
});
254+
255+
await wa.sendMessage({
256+
waNumber: "wa_number",
257+
phoneNumber: "+254700000000",
258+
body: {
259+
templateId: "welcome_v1",
260+
headerValue: "Welcome",
261+
bodyValues: ["Alice"],
262+
},
263+
});
264+
265+
await wa.sendMessage({
266+
waNumber: "wa_number",
267+
phoneNumber: "+254700000000",
268+
body: {
269+
mediaType: "Image",
270+
url: "https://example.com/img.png",
271+
caption: "see attached",
272+
},
273+
});
274+
275+
await wa.sendMessage({
276+
waNumber: "wa_number",
277+
phoneNumber: "+254700000000",
278+
body: {
279+
action: {
280+
button: "Pick one",
281+
sections: [
282+
{
283+
title: "Options",
284+
rows: [{ id: "1", title: "First", description: "..." }],
285+
},
286+
],
287+
},
288+
body: { text: "Select an option" },
289+
},
290+
});
291+
292+
await wa.sendMessage({
293+
waNumber: "wa_number",
294+
phoneNumber: "+254700000000",
295+
body: {
296+
action: {
297+
buttons: [
298+
{ id: "yes", title: "Yes" },
299+
{ id: "no", title: "No" },
300+
],
301+
},
302+
body: { text: "Confirm?" },
303+
},
304+
});
305+
306+
await wa.createTemplate({
307+
waNumber: "wa_number",
308+
name: "promo_template",
309+
language: "en",
310+
category: "MARKETING",
311+
components: {
312+
body: { type: "BODY", text: "Hi {{1}}" },
313+
},
314+
});
315+
}
316+
317+
// =========================================================================
318+
// USSD (ussd.d.ts)
319+
// =========================================================================
320+
321+
function ussdSurface() {
322+
const ussd: AfricasTalking.USSDMiddlewareFactory = client.USSD;
323+
324+
const handler: AfricasTalking.USSDHandlerFn = (payload, respond) => {
325+
const session: string = payload.sessionId;
326+
const phone: string = payload.phoneNumber;
327+
const text: string = payload.text;
328+
const code: string = payload.serviceCode;
329+
void session;
330+
void phone;
331+
void code;
332+
333+
if (text === "") {
334+
respond({ response: "Welcome", endSession: false });
335+
} else {
336+
respond({ response: "Goodbye", endSession: true });
337+
}
26338
};
27339

28-
const res = await sms.createSubscription(options);
29-
};
340+
const middleware: AfricasTalking.USSDMiddleware[] = ussd(handler);
341+
void middleware;
342+
}
343+
344+
void clientHandle;
345+
void clientXml;
346+
void smsSurface;
347+
void tokenSurface;
348+
void voiceSurface;
349+
void airtimeSurface;
350+
void applicationSurface;
351+
void insightsSurface;
352+
void mobileDataSurface;
353+
void whatsappSurface;
354+
void ussdSurface;

0 commit comments

Comments
 (0)