@phonghq/go-chat 1.0.34 → 1.0.36

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.
@@ -12,6 +12,7 @@ import IconClose from '../../../assets/icons/call/IconClose.vue';
12
12
  import { usePlivo } from '../../../composable/usePlivo';
13
13
  import { PLIVO_CALL_STATUS } from '../../../types/chat/call';
14
14
  import IconMic from '../../../assets/icons/call/IconMic.vue';
15
+ import { useError } from '../../../composable/useError';
15
16
  const props = withDefaults(defineProps(), {});
16
17
  const emit = defineEmits();
17
18
  // const {
@@ -44,8 +45,12 @@ const handlePlivoCallBack = (status, data) => {
44
45
  callStatus.value = status;
45
46
  startTimer();
46
47
  }
48
+ else if (status == PLIVO_CALL_STATUS.MEDIA_PERMISSION_FAIL) {
49
+ emit('error:mediaPermissionFail');
50
+ }
47
51
  };
48
52
  const { plivoLogin, plivoCallAnswer, plivoCall, plivoEndCall, plivoCallSwishMute } = usePlivo(handlePlivoCallBack);
53
+ const { showError } = useError();
49
54
  const STATUS_LABEL = computed(() => {
50
55
  return {
51
56
  [PLIVO_CALL_STATUS.CONNECTING]: 'Connecting...',
@@ -57,7 +62,8 @@ const STATUS_LABEL = computed(() => {
57
62
  [PLIVO_CALL_STATUS.NO_ANSWER]: 'No Answer',
58
63
  [PLIVO_CALL_STATUS.CANCEL]: '',
59
64
  [PLIVO_CALL_STATUS.TIME_OUT]: '',
60
- [PLIVO_CALL_STATUS.BUSY]: ''
65
+ [PLIVO_CALL_STATUS.BUSY]: '',
66
+ [PLIVO_CALL_STATUS.MEDIA_PERMISSION_FAIL]: ''
61
67
  };
62
68
  });
63
69
  const label = computed(() => STATUS_LABEL.value[callStatus.value] ?? '');
@@ -72,13 +78,20 @@ const userRemoter = ref(null);
72
78
  const isMute = ref(false);
73
79
  let timer = null;
74
80
  let timeOut = null;
81
+ let refreshTokenTimeOut = null;
82
+ let refreshTokenCount = null;
75
83
  let callType = 'call';
76
84
  let uuidEnd = '';
77
85
  onMounted(async () => {
78
- if (dataProfile.value?.tenant_phone) {
79
- const token = await getPlivoAccessToken();
80
- // const token = ''
81
- await plivoLogin(token);
86
+ try {
87
+ if (dataProfile.value?.tenant_phone) {
88
+ const token = await getPlivoAccessToken();
89
+ await plivoLogin({ token });
90
+ setTimeout(refreshToken, 1000);
91
+ }
92
+ }
93
+ catch {
94
+ showError('We’re having trouble connecting the call. Please try again in a moment');
82
95
  }
83
96
  });
84
97
  onUnmounted(() => {
@@ -117,6 +130,31 @@ function endCall(option) {
117
130
  emit('endCall', userRemoter.value, callType);
118
131
  callType = '';
119
132
  }
133
+ const refreshToken = async () => {
134
+ try {
135
+ console.log('refreshToken: ');
136
+ if (refreshTokenTimeOut) {
137
+ clearTimeout(refreshTokenTimeOut);
138
+ refreshTokenTimeOut = null;
139
+ }
140
+ const token = await getPlivoAccessToken();
141
+ await plivoLogin({ token, isRefresh: true });
142
+ setTimeout(() => refreshToken(), 15 * 60 * 1000);
143
+ refreshTokenCount = 0;
144
+ }
145
+ catch (e) {
146
+ console.log(e);
147
+ refreshTokenCount = refreshTokenCount + 1;
148
+ console.log(refreshTokenCount);
149
+ if (refreshTokenCount < 3) {
150
+ setTimeout(() => refreshToken(), 2 * 60 * 1000);
151
+ }
152
+ else {
153
+ showError('We’re having trouble connecting the call. Please try again in a moment');
154
+ }
155
+ console.log(e);
156
+ }
157
+ };
120
158
  const open = () => {
121
159
  drawerVisible.value = true;
122
160
  disable.value = true;
@@ -155,7 +193,7 @@ const answer = async () => {
155
193
  const getUserOffer = async (phone) => {
156
194
  let res = null;
157
195
  try {
158
- userRemoter.value = null;
196
+ userRemoter.value = { phone, username: '' };
159
197
  let data = {
160
198
  phone: formatPhone10number(phone, '1'),
161
199
  client_id: dataProfile.value?.tenant_id ?? ''
@@ -0,0 +1,10 @@
1
+ import { ref } from 'vue';
2
+ export const goChatError = ref('');
3
+ export function useError() {
4
+ const showError = (error) => {
5
+ goChatError.value = error;
6
+ };
7
+ return {
8
+ showError
9
+ };
10
+ }
@@ -51,7 +51,7 @@ export const useListConversations = (is_unknown) => {
51
51
  if ((data.is_unknown ?? 0) != is_unknown)
52
52
  return;
53
53
  getDataMqtt();
54
- if (topic === TOPIC_HOME[0] + dataProfile.value?.id) {
54
+ if (topic === TOPIC_HOME[0] + dataProfile.value?.id || topic === TOPIC_HOME[3] + dataProfile.value?.id) {
55
55
  const index = listConversations.value.findIndex((item) => item.id === data.id);
56
56
  const hasChatBox = listConversations.value.findIndex((item) => item.id === digibotId);
57
57
  if (index != -1) {
@@ -1,4 +1,6 @@
1
1
  import { PLIVO_CALL_STATUS } from '../types/chat/call';
2
+ import { createSipAccount } from '../utils/chat/call';
3
+ import { dataProfile } from '../utils/chat/auth';
2
4
  export function usePlivo(callback) {
3
5
  var options = {
4
6
  debug: 'ALL',
@@ -12,36 +14,41 @@ export function usePlivo(callback) {
12
14
  let CallUuid = '';
13
15
  let custom_resolve = null;
14
16
  let custom_reject = null;
15
- const plivoLogin = async (token) => {
17
+ const plivoLogin = async (data) => {
16
18
  // token =
17
19
  // 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6InBsaXZvO3Y9MSJ9.eyJqdGkiOiJ0ZXN0MSIsImlzcyI6IlNBTjJZMFpERTRNSkFUTVpZNU5DIiwic3ViIjoid2ViY2FsbDAwMzA3OTY3MzQ1NDg5MTgyNyIsIm5iZiI6MTc2MzY5MzUyNSwiZXhwIjoxNzYzNzc5OTI1LCJncmFudHMiOnsidm9pY2UiOnsiaW5jb21pbmdfYWxsb3ciOnRydWUsIm91dGdvaW5nX2FsbG93Ijp0cnVlfX19.9p4O_xTb4kNhKyDVfK3EemSKBQiHtbKNUZ5iwnSdX1I'
18
20
  try {
19
- if (!plivoBrowserSdk)
21
+ if (!plivoBrowserSdk) {
20
22
  plivoBrowserSdk = new window.Plivo(options);
21
- plivoBrowserSdk?.client?.on?.('onCallAnswered', (callInfo) => {
22
- handleCallAnswered(callInfo);
23
- });
24
- plivoBrowserSdk?.client?.on?.('onCallTerminated', (hangupInfo, callInfo) => handleCallTerminated(hangupInfo, callInfo));
25
- plivoBrowserSdk.client.on('onCallFailed', (data, callInfo) => {
26
- handleCallFailed(data, callInfo);
27
- });
28
- plivoBrowserSdk.client.on('onIncomingCallCanceled', (callInfo) => {
29
- handleIncomingCallCanceled(callInfo);
30
- });
31
- // plivoBrowserSdk.client.on('onCalling', (data: any) => console.log('onCallFailed', data))
32
- plivoBrowserSdk?.client?.on?.('onIncomingCall', (callerID, extraHeaders, callInfo) => {
33
- handleIncomingCall(callInfo);
34
- });
35
- plivoBrowserSdk.client.on('onCallRemoteRinging', (data) => handleCallRemoteRinging(data));
36
- plivoBrowserSdk.client.on('onMediaPermission', (e) => handleMediaPermission(e));
37
- plivoBrowserSdk?.client?.on?.('onLoginFailed', (e) => handleLoginFailed(e));
38
- // plivoBrowserSdk?.client?.on?.('remoteAudioStatus', () => console.log('remoteAudioStatus'))
39
- const payload = JSON.parse(atob(token.split('.')[1]));
40
- console.log(payload);
23
+ plivoBrowserSdk?.client?.on?.('onCallAnswered', (callInfo) => {
24
+ handleCallAnswered(callInfo);
25
+ });
26
+ plivoBrowserSdk?.client?.on?.('onCallTerminated', (hangupInfo, callInfo) => handleCallTerminated(hangupInfo, callInfo));
27
+ plivoBrowserSdk.client.on('onCallFailed', (data, callInfo) => {
28
+ handleCallFailed(data, callInfo);
29
+ });
30
+ plivoBrowserSdk.client.on('onIncomingCallCanceled', (callInfo) => {
31
+ handleIncomingCallCanceled(callInfo);
32
+ });
33
+ // plivoBrowserSdk.client.on('onCalling', (data: any) => console.log('onCallFailed', data))
34
+ plivoBrowserSdk?.client?.on?.('onIncomingCall', (callerID, extraHeaders, callInfo) => {
35
+ handleIncomingCall(callInfo);
36
+ });
37
+ plivoBrowserSdk.client.on('onCallRemoteRinging', (data) => handleCallRemoteRinging(data));
38
+ plivoBrowserSdk.client.on('onMediaPermission', (e) => handleMediaPermission(e));
39
+ plivoBrowserSdk?.client?.on?.('onLoginFailed', (e) => handleLoginFailed(e));
40
+ }
41
+ if (!data.isRefresh) {
42
+ const payload = JSON.parse(atob(data.token.split('.')[1]));
43
+ await createSipAccount({
44
+ sip_username: `${payload.sub}_${payload.iss}`,
45
+ did_number: dataProfile.value?.tenant_phone ?? ''
46
+ });
47
+ }
41
48
  // await plivoBrowserSdk?.client?.tokenLogin(payload.sub ?? '', token)
42
49
  // await plivoBrowserSdk?.client?.login('webcall003079673454891827', '123456abcA!')
43
50
  // await plivoBrowserSdk?.client?.login(payload.sub , null, token)
44
- await plivoBrowserSdk?.client?.loginWithAccessToken(token);
51
+ await plivoBrowserSdk?.client?.loginWithAccessToken(data.token);
45
52
  // await plivoBrowserSdk?.client?.on?.(payload)
46
53
  // console.log('Registered with token')
47
54
  }
@@ -146,6 +153,9 @@ export function usePlivo(callback) {
146
153
  };
147
154
  const handleMediaPermission = (e) => {
148
155
  console.log('MediaPermission', e);
156
+ if (e?.status == 'failure') {
157
+ callback(PLIVO_CALL_STATUS.MEDIA_PERMISSION_FAIL);
158
+ }
149
159
  };
150
160
  return {
151
161
  plivoLogin,
@@ -3,9 +3,10 @@ export const TOPIC_DETAIL_CALL = 'call/message/';
3
3
  export const TOPIC_HOME = [
4
4
  'chat/conversation/update/',
5
5
  'chat/conversation/read_message/',
6
- 'chat/conversation/update_list/'
6
+ 'chat/conversation/update_list/',
7
+ 'chat/conversation/update_list/sms-inbound'
7
8
  ];
8
- export const TOPIC_PLIVO_SMS = 'sms-outbound/';
9
+ export const TOPIC_PLIVO_SMS = 'sms-inbound/';
9
10
  export const TOPIC_STATUS_USER = 'user/status/';
10
11
  // export CALL_STATE = {
11
12
  // Offer: 'offer',
@@ -17,5 +17,6 @@ export const PLIVO_CALL_STATUS = {
17
17
  NO_ANSWER: 'no-answer',
18
18
  CANCEL: 'cancel',
19
19
  TIME_OUT: 'timeout',
20
- BUSY: 'busy'
20
+ BUSY: 'busy',
21
+ MEDIA_PERMISSION_FAIL: 'media-permission-fail',
21
22
  };
@@ -137,3 +137,19 @@ export const getPlivoAccessToken = async () => {
137
137
  const result = await response.json();
138
138
  return result?.accessToken;
139
139
  };
140
+ export const createSipAccount = async (data) => {
141
+ const url = BARE_WEBSOCKET_URL + '/ws/create-sip-account';
142
+ const response = await fetch(url, {
143
+ method: 'POST',
144
+ body: JSON.stringify(data),
145
+ headers: {
146
+ ['Content-Type']: 'application/json'
147
+ }
148
+ });
149
+ if (!response.ok) {
150
+ throw new Error(`Response status: ${response.status}`);
151
+ }
152
+ const result = await response.json();
153
+ console.log(result);
154
+ return result;
155
+ };
@@ -30,7 +30,7 @@ export const publicTopicConversationUpdate = async (data) => {
30
30
  const current = dayjs().tz(TIME_ZONE_UTC).format(DATE_FORMATS['DATE_FORMAT_FULL']);
31
31
  const dataMessageForReceiver = {
32
32
  id: data.infoUser?.conversation_id ?? 0,
33
- receiver_id: Number(dataProfile.value?.id ?? 0),
33
+ receiver_id: Number(data.infoUser?.id ?? 0),
34
34
  username: dataProfile.value?.username ?? '',
35
35
  phone: dataProfile.value?.phone ?? '',
36
36
  avatar: dataProfile.value?.avatar ?? '',
@@ -42,12 +42,12 @@ export const publicTopicConversationUpdate = async (data) => {
42
42
  status: 1,
43
43
  color: dataProfile.value?.color ?? '',
44
44
  is_call: dataProfile.value?.color ?? '',
45
- is_unknown: 0
45
+ is_unknown: 0,
46
46
  };
47
47
  publishMessage(`${TOPIC_HOME[0] + Number(data.infoUser?.id)}`, JSON.stringify(dataMessageForReceiver));
48
48
  const dataMessageMyself = {
49
49
  id: data.infoUser?.conversation_id ?? 0,
50
- receiver_id: Number(dataProfile.value?.id ?? 0),
50
+ receiver_id: Number(data.infoUser?.id ?? 0),
51
51
  username: data.infoUser?.username ?? '',
52
52
  phone: data.infoUser?.phone ?? '',
53
53
  avatar: data.infoUser?.avatar ?? '',
@@ -4,7 +4,7 @@ export const phoneNumberFormat = (phone, halfShowType = 1) => {
4
4
  number = number.substring(number.length - 10);
5
5
  }
6
6
  if (number.length == 10) {
7
- if (true) {
7
+ if (false) {
8
8
  if (halfShowType == 4) {
9
9
  number = (number || '').replace(/(\d{3})(\d{3})(\d{4})/, "*** $3");
10
10
  }
@@ -26,6 +26,7 @@ interface IResProfile {
26
26
  user_type: 'customer' | 'tenant';
27
27
  has_business_tenant_phone?: boolean;
28
28
  color: string;
29
+ is_sms_active: number;
29
30
  }
30
31
  interface IPramsLoginLink {
31
32
  tenant_id: string;
@@ -13,5 +13,6 @@ export declare const PLIVO_CALL_STATUS: {
13
13
  readonly CANCEL: "cancel";
14
14
  readonly TIME_OUT: "timeout";
15
15
  readonly BUSY: "busy";
16
+ readonly MEDIA_PERMISSION_FAIL: "media-permission-fail";
16
17
  };
17
18
  export type PlivoCallStatusType = (typeof PLIVO_CALL_STATUS)[keyof typeof PLIVO_CALL_STATUS];
@@ -20,6 +20,7 @@ export type IResMessage = {
20
20
  message_uuid?: string | null;
21
21
  is_call?: number | null;
22
22
  duration?: number | null;
23
+ is_sms: number;
23
24
  record_url?: string | null;
24
25
  call_status?: string | null;
25
26
  created_at: string;
@@ -20,6 +20,7 @@ export declare const dataProfile: import("vue").Ref<{
20
20
  user_type: "customer" | "tenant";
21
21
  has_business_tenant_phone?: boolean | undefined;
22
22
  color: string;
23
+ is_sms_active: number;
23
24
  } | null, IResProfile | {
24
25
  id: number;
25
26
  username: string;
@@ -31,6 +32,7 @@ export declare const dataProfile: import("vue").Ref<{
31
32
  user_type: "customer" | "tenant";
32
33
  has_business_tenant_phone?: boolean | undefined;
33
34
  color: string;
35
+ is_sms_active: number;
34
36
  } | null>;
35
37
  export declare const dataLoginLink: import("vue").Ref<{
36
38
  token: string;
@@ -9,3 +9,7 @@ export declare const plivoCall: (user: IResUser) => Promise<any>;
9
9
  export declare const plivoEndCall: (uuid: string) => Promise<void>;
10
10
  export declare const downloadRecord: (url_pub: string) => Promise<void>;
11
11
  export declare const getPlivoAccessToken: () => Promise<any>;
12
+ export declare const createSipAccount: (data: {
13
+ did_number: string;
14
+ sip_username: string;
15
+ }) => Promise<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phonghq/go-chat",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist"