-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.ts
66 lines (59 loc) · 1.8 KB
/
send.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {
getPhoneNumberFromConfig,
getPrintFormat,
getRecipientPhoneNumberFromConfig,
initSmsServiceWithServicePlanId,
printFullResponse,
} from '../../config';
import { Sms } from '@sinch/sdk-core';
(async () => {
console.log('***********');
console.log('* SendSMS *');
console.log('***********');
const recipientPhoneNumber = getRecipientPhoneNumberFromConfig();
const senderPhoneNumber = getPhoneNumberFromConfig();
const oneDayLater = new Date();
oneDayLater.setDate(oneDayLater.getDate() +1);
const requestData: Sms.SendSMSRequestData= {
sendSMSRequestBody: {
type: 'mt_text',
to: [
recipientPhoneNumber,
],
from: senderPhoneNumber,
parameters: {
name: {
[recipientPhoneNumber]: 'John',
default: 'there',
},
},
body: 'Hi ${name}!',
// send_at: oneDayLater,
delivery_report: 'full',
},
};
const smsService = initSmsServiceWithServicePlanId();
let response;
try {
response = await smsService.batches.send(requestData);
} catch (error) {
console.error(`ERROR: The SMS could not be sent`);
throw error;
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(`The SMS has been sent successfully. Here is the batch id: ${response.id}`);
if(response.type === 'mt_text') {
console.log(`The message was: '${response.body}'`);
}
if(response.type === 'mt_binary') {
console.log(`The message (decoded) was: '${atob(response.body!)}'`);
}
if(response.type === 'mt_media') {
console.log(`The message was: '${response.body?.message}'`);
}
} else {
printFullResponse(response);
}
console.log(`You may want to update your .env file with the following value: BATCH_ID=${response.id}`);
})();