@vgroup/dialbox 0.0.25 → 0.0.26

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.
@@ -114,59 +114,44 @@ class TwilioService {
114
114
  this.isAvailableNumber = new BehaviorSubject(false);
115
115
  this.callerIdList = new BehaviorSubject([]);
116
116
  this.triggerSMSReload = new BehaviorSubject(false);
117
- this.initializeTwilioDevice();
118
117
  }
119
- // Initialize Twilio device and set up event listeners
120
- initializeTwilioDevice() {
121
- // Get the Twilio token from localStorage
122
- const token = localStorage.getItem('twilio_token');
123
- if (!token) {
124
- console.error('Twilio token not found in localStorage');
125
- return;
118
+ // Update the Twilio token
119
+ updateToken(token) {
120
+ if (this.device) {
121
+ this.device.updateToken(token);
122
+ localStorage.setItem('twilio_token', token);
123
+ console.log('Twilio token updated');
126
124
  }
127
- // Initialize the Twilio Device
128
- // @ts-ignore - We'll handle the device setup
129
- this.device = new Device(token, {
130
- codecPreferences: ['opus', 'pcmu'],
131
- debug: true
132
- });
133
- // Set up device event listeners
134
- this.device.on('ready', (device) => {
135
- console.log('Twilio Device Ready');
136
- });
137
- this.device.on('error', (error) => {
138
- console.error('Twilio Device Error:', error);
139
- this.currentCallState.next('error');
140
- });
141
- // Handle incoming calls
142
- this.device.on('incoming', (call) => {
143
- console.log('Incoming call:', call);
144
- this.currentCall.next(call);
145
- this.currentCallState.next('incoming');
146
- this.callType.next('INCOMING');
147
- // Set up call event handlers
148
- call.on('accept', () => {
149
- console.log('Call accepted');
150
- this.currentCallState.next('in-progress');
151
- this.isIncomingCallPicked.next(true);
125
+ else {
126
+ console.warn('Cannot update token: Twilio device not initialized');
127
+ }
128
+ }
129
+ // Initialize Twilio device with token
130
+ initializeTwilioDevice(token) {
131
+ try {
132
+ if (!token) {
133
+ console.error('No Twilio token provided');
134
+ return;
135
+ }
136
+ // Store the token in localStorage
137
+ localStorage.setItem('twilio_token', token);
138
+ // Initialize Twilio device
139
+ // @ts-ignore - Device is loaded from Twilio client SDK
140
+ this.device = new Device(token, {
141
+ codecPreferences: ['opus', 'pcmu'],
142
+ debug: true
152
143
  });
153
- call.on('disconnect', () => {
154
- console.log('Call disconnected');
155
- this.currentCall.next(null);
156
- this.currentCallState.next('none');
157
- this.isIncomingCallPicked.next(false);
144
+ // Set up device event listeners
145
+ this.device.on('ready', (device) => {
146
+ console.log('Twilio Device Ready');
158
147
  });
159
- call.on('reject', () => {
160
- console.log('Call rejected');
161
- this.currentCall.next(null);
162
- this.currentCallState.next('none');
148
+ this.device.on('error', (error) => {
149
+ console.error('Twilio Device Error:', error);
150
+ this.currentCallState.next('error');
163
151
  });
164
- });
165
- }
166
- // Update Twilio token when needed
167
- updateToken(token) {
168
- if (this.device) {
169
- this.device.updateToken(token);
152
+ }
153
+ catch (error) {
154
+ console.error('Error initializing Twilio device:', error);
170
155
  }
171
156
  }
172
157
  saveContact(payload) {
@@ -3150,6 +3135,68 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
3150
3135
  }]
3151
3136
  }] });
3152
3137
 
3138
+ class TwilioInitService {
3139
+ constructor(twilioService, extensionService) {
3140
+ this.twilioService = twilioService;
3141
+ this.extensionService = extensionService;
3142
+ }
3143
+ // Call this method when your application starts (e.g., in app.component.ts)
3144
+ async initializeTwilio() {
3145
+ try {
3146
+ // 1. Get a token from your backend
3147
+ const token = await this.getTwilioToken();
3148
+ if (!token) {
3149
+ console.error('Failed to get Twilio token');
3150
+ return;
3151
+ }
3152
+ // 2. Initialize the Twilio device with the token
3153
+ this.twilioService.initializeTwilioDevice(token);
3154
+ // 3. Set up token refresh (optional, but recommended)
3155
+ this.setupTokenRefresh();
3156
+ }
3157
+ catch (error) {
3158
+ console.error('Error initializing Twilio:', error);
3159
+ }
3160
+ }
3161
+ async getTwilioToken() {
3162
+ // Replace this with your actual token retrieval logic
3163
+ // Example:
3164
+ // return this.extensionService.getTwilioToken().toPromise();
3165
+ // For now, we'll get it from localStorage if it exists
3166
+ const token = localStorage.getItem('twilio_token');
3167
+ if (!token) {
3168
+ throw new Error('Twilio token not found');
3169
+ }
3170
+ return token;
3171
+ }
3172
+ setupTokenRefresh() {
3173
+ // Refresh token every 30 minutes (Twilio tokens typically expire after 1 hour)
3174
+ this.tokenRefreshInterval = setInterval(async () => {
3175
+ try {
3176
+ const newToken = await this.getTwilioToken();
3177
+ this.twilioService.updateToken(newToken);
3178
+ }
3179
+ catch (error) {
3180
+ console.error('Error refreshing Twilio token:', error);
3181
+ }
3182
+ }, 30 * 60 * 1000); // 30 minutes
3183
+ }
3184
+ ngOnDestroy() {
3185
+ // Clean up the interval when the service is destroyed
3186
+ if (this.tokenRefreshInterval) {
3187
+ clearInterval(this.tokenRefreshInterval);
3188
+ }
3189
+ }
3190
+ }
3191
+ TwilioInitService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: TwilioInitService, deps: [{ token: TwilioService }, { token: ExtensionService }], target: i0.ɵɵFactoryTarget.Injectable });
3192
+ TwilioInitService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: TwilioInitService, providedIn: 'root' });
3193
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: TwilioInitService, decorators: [{
3194
+ type: Injectable,
3195
+ args: [{
3196
+ providedIn: 'root'
3197
+ }]
3198
+ }], ctorParameters: function () { return [{ type: TwilioService }, { type: ExtensionService }]; } });
3199
+
3153
3200
  /*
3154
3201
  * Public API Surface of dialbox
3155
3202
  */
@@ -3158,5 +3205,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
3158
3205
  * Generated bundle index. Do not edit.
3159
3206
  */
3160
3207
 
3161
- export { CallProgressComponent, CallerIdDialogComponent, DialboxComponent, DialboxModule, IncomingCallComponent };
3208
+ export { CallProgressComponent, CallerIdDialogComponent, DialboxComponent, DialboxModule, IncomingCallComponent, TwilioInitService };
3162
3209
  //# sourceMappingURL=vgroup-dialbox.mjs.map