react-native-emitra-chatbot 0.0.0-stage → 1.0.0
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.
- package/README.md +17 -2
- package/package.json +31 -4
- package/src/index.d.ts +5 -0
- package/src/index.js +230 -0
package/README.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-native-emitra-chatbot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Prototype wrapper for the hosted BSES eMitra widget. Not affiliated with BSES. Obtain authorization before distributing. No vendor script is bundled. The hosted service must be available.
|
|
4
|
+
|
|
5
|
+
## Install locally
|
|
6
|
+
`npm install ./react-native-emitra-chatbot-1.0.0.tgz react-native-webview`
|
|
7
|
+
|
|
8
|
+
For bare iOS run `npx pod-install` in the consuming app. For Expo use `npx expo install react-native-webview`.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
```tsx
|
|
12
|
+
import { EMitraChatbot } from 'react-native-emitra-chatbot';
|
|
13
|
+
export default function App() { return <EMitraChatbot onError={console.warn} />; }
|
|
14
|
+
```
|
|
15
|
+
Mount in a flex:1 screen. The existing widget controls its internal UI. The WebView occupies its container; it is not a touch-through overlay. `stylesheetUrl` is optional and must be a trusted HTTPS URL. `onReady` means the shadow root was detected, not that the backend is authenticated. External navigation is blocked by default. Device testing is required for authentication, CORS, uploads, keyboard, links and widget behavior. Do not use with untrusted scripts. No vendor functionality has been verified.
|
|
16
|
+
|
|
17
|
+
## Publish
|
|
18
|
+
Confirm permission, package name availability and licensing. Run `npm pack --dry-run`, `npm login`, then `npm publish --access public` after testing. This package is marked UNLICENSED pending authorization.
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
|
+
|
|
1
2
|
{
|
|
2
3
|
"name": "react-native-emitra-chatbot",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Reusable eMitra chatbot SDK for React Native applications supporting Android and iOS.",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"types": "src/index.d.ts",
|
|
8
|
+
"react-native": "src/index.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"react-native",
|
|
15
|
+
"react-native-chatbot",
|
|
16
|
+
"emitra",
|
|
17
|
+
"chatbot",
|
|
18
|
+
"android",
|
|
19
|
+
"ios",
|
|
20
|
+
"webview",
|
|
21
|
+
"chatbot-sdk"
|
|
22
|
+
],
|
|
23
|
+
"author": "Krishna Kant Sharma",
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": ">=18",
|
|
26
|
+
"react-native": ">=0.72",
|
|
27
|
+
"react-native-webview": ">=13"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"license": "UNLICENSED"
|
|
33
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type {ComponentType} from 'react';
|
|
2
|
+
import type {StyleProp, ViewStyle} from 'react-native';
|
|
3
|
+
export interface EMitraChatbotProps { stylesheetUrl?: string; style?: StyleProp<ViewStyle>; onReady?: () => void; onError?: (message: string) => void; }
|
|
4
|
+
export const EMitraChatbot: ComponentType<EMitraChatbotProps>;
|
|
5
|
+
export default EMitraChatbot;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
|
|
2
|
+
import React, {useMemo} from 'react';
|
|
3
|
+
import {View, StyleSheet} from 'react-native';
|
|
4
|
+
import {WebView} from 'react-native-webview';
|
|
5
|
+
|
|
6
|
+
const ORIGIN = 'https://byplcbot.bsesdelhi.com';
|
|
7
|
+
|
|
8
|
+
export function EMitraChatbot({
|
|
9
|
+
stylesheetUrl,
|
|
10
|
+
style,
|
|
11
|
+
onReady,
|
|
12
|
+
onError,
|
|
13
|
+
} = {}) {
|
|
14
|
+
const source = useMemo(() => {
|
|
15
|
+
const css = JSON.stringify(stylesheetUrl || '')
|
|
16
|
+
.replace(/</g, '\\u003c');
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
baseUrl: ORIGIN + '/',
|
|
20
|
+
html: `
|
|
21
|
+
<!doctype html>
|
|
22
|
+
<html>
|
|
23
|
+
<head>
|
|
24
|
+
<meta name="viewport"
|
|
25
|
+
content="width=device-width, initial-scale=1">
|
|
26
|
+
|
|
27
|
+
<style>
|
|
28
|
+
html, body {
|
|
29
|
+
margin: 0;
|
|
30
|
+
width: 100%;
|
|
31
|
+
height: 100%;
|
|
32
|
+
background: transparent;
|
|
33
|
+
}
|
|
34
|
+
</style>
|
|
35
|
+
</head>
|
|
36
|
+
|
|
37
|
+
<body>
|
|
38
|
+
<script>
|
|
39
|
+
function emit(type, data) {
|
|
40
|
+
window.ReactNativeWebView.postMessage(
|
|
41
|
+
JSON.stringify({type, data})
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// NEW: Capture detailed console errors
|
|
46
|
+
const originalError = console.error;
|
|
47
|
+
|
|
48
|
+
console.error = function (...args) {
|
|
49
|
+
originalError.apply(console, args);
|
|
50
|
+
|
|
51
|
+
const message = args.map(arg => {
|
|
52
|
+
if (arg instanceof Error) {
|
|
53
|
+
return arg.stack || arg.message;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (typeof arg === 'object') {
|
|
57
|
+
try {
|
|
58
|
+
return JSON.stringify(arg);
|
|
59
|
+
} catch {
|
|
60
|
+
return String(arg);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return String(arg);
|
|
65
|
+
}).join(' ');
|
|
66
|
+
|
|
67
|
+
emit('debug', message);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// NEW: Capture unhandled promise errors
|
|
71
|
+
window.addEventListener(
|
|
72
|
+
'unhandledrejection',
|
|
73
|
+
function(event) {
|
|
74
|
+
console.error(
|
|
75
|
+
'Unhandled promise rejection:',
|
|
76
|
+
event.reason
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
function waitForWidget() {
|
|
82
|
+
let attempts = 0;
|
|
83
|
+
|
|
84
|
+
const timer = setInterval(() => {
|
|
85
|
+
const host = document.getElementById(
|
|
86
|
+
'emitra-widget-host'
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
if (host && host.shadowRoot) {
|
|
90
|
+
clearInterval(timer);
|
|
91
|
+
|
|
92
|
+
const url = ${css};
|
|
93
|
+
|
|
94
|
+
if (url) {
|
|
95
|
+
try {
|
|
96
|
+
const parsed = new URL(
|
|
97
|
+
url,
|
|
98
|
+
location.href
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
if (parsed.protocol !== 'https:') {
|
|
102
|
+
throw Error(
|
|
103
|
+
'HTTPS stylesheet required'
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const link =
|
|
108
|
+
document.createElement('link');
|
|
109
|
+
|
|
110
|
+
link.rel = 'stylesheet';
|
|
111
|
+
link.href = parsed.href;
|
|
112
|
+
|
|
113
|
+
link.onerror = () => emit(
|
|
114
|
+
'error',
|
|
115
|
+
'Stylesheet loading failed'
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
host.shadowRoot.appendChild(link);
|
|
119
|
+
|
|
120
|
+
} catch (e) {
|
|
121
|
+
emit('error', e.message);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
emit('ready');
|
|
126
|
+
|
|
127
|
+
} else if (++attempts >= 100) {
|
|
128
|
+
clearInterval(timer);
|
|
129
|
+
|
|
130
|
+
emit(
|
|
131
|
+
'error',
|
|
132
|
+
'Widget shadow root not found'
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}, 200);
|
|
136
|
+
}
|
|
137
|
+
</script>
|
|
138
|
+
|
|
139
|
+
<script
|
|
140
|
+
src="${ORIGIN}/emitra-widget.iife.js"
|
|
141
|
+
onload="waitForWidget()"
|
|
142
|
+
onerror="emit('error','Chatbot script loading failed')">
|
|
143
|
+
</script>
|
|
144
|
+
|
|
145
|
+
</body>
|
|
146
|
+
</html>
|
|
147
|
+
`,
|
|
148
|
+
};
|
|
149
|
+
}, [stylesheetUrl]);
|
|
150
|
+
|
|
151
|
+
return React.createElement(
|
|
152
|
+
View,
|
|
153
|
+
{
|
|
154
|
+
style: [styles.container, style],
|
|
155
|
+
},
|
|
156
|
+
React.createElement(WebView, {
|
|
157
|
+
source,
|
|
158
|
+
style: styles.webview,
|
|
159
|
+
|
|
160
|
+
javaScriptEnabled: true,
|
|
161
|
+
domStorageEnabled: true,
|
|
162
|
+
|
|
163
|
+
// NEW: Cookie and session configuration
|
|
164
|
+
sharedCookiesEnabled: true,
|
|
165
|
+
thirdPartyCookiesEnabled: true,
|
|
166
|
+
cacheEnabled: true,
|
|
167
|
+
incognito: false,
|
|
168
|
+
|
|
169
|
+
// NEW: Enable Chrome debugging in development
|
|
170
|
+
webviewDebuggingEnabled: __DEV__,
|
|
171
|
+
|
|
172
|
+
originWhitelist: ['*'],
|
|
173
|
+
|
|
174
|
+
onShouldStartLoadWithRequest: r =>
|
|
175
|
+
r.url === 'about:blank' ||
|
|
176
|
+
r.url === ORIGIN ||
|
|
177
|
+
r.url.startsWith(ORIGIN + '/'),
|
|
178
|
+
|
|
179
|
+
onError: e => {
|
|
180
|
+
onError?.(e.nativeEvent.description);
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
onHttpError: e => {
|
|
184
|
+
if (__DEV__) {
|
|
185
|
+
console.log(
|
|
186
|
+
'CHATBOT HTTP ERROR:',
|
|
187
|
+
e.nativeEvent.statusCode,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
onMessage: e => {
|
|
193
|
+
let event;
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
event = JSON.parse(e.nativeEvent.data);
|
|
197
|
+
} catch {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (event.type === 'ready') {
|
|
202
|
+
onReady?.();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (event.type === 'error') {
|
|
206
|
+
onError?.(String(event.data));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (event.type === 'debug' && __DEV__) {
|
|
210
|
+
console.log(
|
|
211
|
+
'CHATBOT DEBUG:',
|
|
212
|
+
event.data,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
}),
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const styles = StyleSheet.create({
|
|
221
|
+
container: {
|
|
222
|
+
flex: 1,
|
|
223
|
+
},
|
|
224
|
+
webview: {
|
|
225
|
+
flex: 1,
|
|
226
|
+
backgroundColor: 'transparent',
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
export default EMitraChatbot;
|