@xenterprises/fastify-xconfig 1.1.8 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,146 +0,0 @@
1
- import Twilio from "twilio";
2
- export async function setupTwilio(fastify, options) {
3
- if (options.active !== false) {
4
- // Return if missing Twilio credentials
5
- if (
6
- !options.accountSid ||
7
- !options.authToken ||
8
- !options.phoneNumber
9
- )
10
- throw new Error(
11
- "Twilio accountSid, authToken, and phoneNumber must be provided."
12
- );
13
-
14
- const twilioClient = Twilio(
15
- options.accountSid,
16
- options.authToken
17
- );
18
-
19
- // Decorator to group Twilio-related methods under fastify.twilio
20
- fastify.decorate('twilio', {
21
- sendSMS: async (to, body) => {
22
- try {
23
- const message = await twilioClient.messages.create({
24
- body,
25
- to,
26
- from: options.phoneNumber, // Use the Twilio phone number from options
27
- });
28
- return message;
29
- } catch (error) {
30
- fastify.log.error("Twilio send SMS failed:", error);
31
- throw new Error("Failed to send SMS.");
32
- }
33
- },
34
- sendMMS: async (to, body, mediaUrl) => {
35
- try {
36
- const message = await twilioClient.messages.create({
37
- body,
38
- to,
39
- from: options.phoneNumber,
40
- mediaUrl: [mediaUrl], // Array of media URLs
41
- });
42
- return message;
43
- } catch (error) {
44
- fastify.log.error("Twilio send MMS failed:", error);
45
- throw new Error("Failed to send MMS.");
46
- }
47
- },
48
- makeVoiceCall: async (to, twimlUrl) => {
49
- try {
50
- const call = await twilioClient.calls.create({
51
- to,
52
- from: options.phoneNumber,
53
- url: twimlUrl, // URL that provides TwiML instructions
54
- });
55
- return call;
56
- } catch (error) {
57
- fastify.log.error("Twilio make voice call failed:", error);
58
- throw new Error("Failed to make voice call.");
59
- }
60
- },
61
- sendVerificationCode: async (to, channel = 'sms') => {
62
- try {
63
- const verification = await twilioClient.verify.services(options.verifyServiceSid)
64
- .verifications
65
- .create({ to, channel }); // channel can be 'sms' or 'call'
66
- return verification;
67
- } catch (error) {
68
- fastify.log.error("Twilio send verification code failed:", error);
69
- throw new Error("Failed to send verification code.");
70
- }
71
- },
72
- verifyCode: async (to, code) => {
73
- try {
74
- const verificationCheck = await twilioClient.verify.services(options.verifyServiceSid)
75
- .verificationChecks
76
- .create({ to, code });
77
- return verificationCheck;
78
- } catch (error) {
79
- fastify.log.error("Twilio verify code failed:", error);
80
- throw new Error("Failed to verify code.");
81
- }
82
- },
83
- fetchSMSHistory: async (fromDate, toDate) => {
84
- try {
85
- const messages = await twilioClient.messages.list({
86
- dateSentAfter: fromDate,
87
- dateSentBefore: toDate,
88
- limit: 100,
89
- });
90
- return messages;
91
- } catch (error) {
92
- fastify.log.error("Twilio fetch SMS history failed:", error);
93
- throw new Error("Failed to fetch SMS history.");
94
- }
95
- },
96
- handleIncomingSMS: (req, reply) => {
97
- const { Body, From } = req.body;
98
-
99
- // Process incoming message
100
- console.log(`Message received from ${From}: ${Body}`);
101
-
102
- reply.type('text/xml');
103
- reply.send('<Response><Message>Thanks for your message!</Message></Response>');
104
- },
105
- checkCallStatus: async (callSid) => {
106
- try {
107
- const call = await twilioClient.calls(callSid).fetch();
108
- return call;
109
- } catch (error) {
110
- fastify.log.error("Twilio check call status failed:", error);
111
- throw new Error("Failed to check call status.");
112
- }
113
- },
114
- validatePhoneNumber: async (phoneNumber) => {
115
- try {
116
- // Perform phone number validation with Twilio
117
- const validation = await twilioClient.lookups.v2
118
- .phoneNumbers(phoneNumber)
119
- .fetch();
120
-
121
- // Check if the phone number is valid (Twilio may return "valid": true/false)
122
- const isValid = validation.valid !== undefined ? validation.valid : true;
123
-
124
- // Return the required structure
125
- return {
126
- sms: phoneNumber, // The phone number
127
- smsValidate: validation, // The full validation payload from Twilio
128
- isSmsValidated: isValid // Set true/false based on the validation
129
- };
130
- } catch (error) {
131
- fastify.log.error("Twilio phone number validation failed:", error);
132
-
133
- // Return the structure with failed validation
134
- return {
135
- sms: phoneNumber, // The phone number
136
- smsValidate: {}, // Empty object as validation failed
137
- isSmsValidated: false // Validation failed
138
- };
139
- }
140
- }
141
- });
142
-
143
- console.info(" ✅ Twilio Enabled");
144
- }
145
-
146
- }