post_enrollment_app/lib/pages/postEnrollment/botman_widget.dart
2025-07-11 18:00:55 +05:30

132 lines
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class BotmanWidget extends StatefulWidget {
const BotmanWidget({super.key});
@override
State<BotmanWidget> createState() => _BotmanWidgetState();
}
class _BotmanWidgetState extends State<BotmanWidget> {
late final WebViewController _controller;
@override
void initState() {
super.initState();
_controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadHtmlString(_htmlWithBotman);
}
void openBotman() {
_controller.runJavaScript("openBotmanFromFlutter();");
}
final String _htmlWithBotman = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
#botmanWidgetRoot {
display: none !important;
}
body.show-botman #botmanWidgetRoot {
display: block !important;
}
#botmanWidgetRoot > div {
bottom: 80px !important;
right: 10px !important;
z-index: 9999 !important;
min-width: 80px !important;
min-height: 100px !important;
}
.content {
width: 10%;
height: 10vh;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
</style>
<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
</head>
<body></body>
<script>
var emp_name = 'Kumar';
var uid = Math.floor(100000 + Math.random() * 900000);
console.log('CURRENT_USER: ' + uid);
const APIENDPOINT = 'https://venbait.in/nhance/dev';
const APIAPIENDPOINT = 'https://venbait.in/nhance/dev';
window.botmanWidget = {
chatServer: APIENDPOINT + '/chat',
frameEndpoint: APIAPIENDPOINT + '/widget',
title: "Ask ILA",
introMessage: "",
bubbleBackground: "#007bff",
mainColor: "#007bff",
placeholderText: "Type your message here...",
aboutText: "Insurance Assistant ILA",
enableAttachments: false,
parameters: {
employee_id: '12288',
session_id: "XYZ789",
origin: "mobile",
emp_code: "HTL-007",
client_id: 159,
client_branch_id: 125
}
};
// ✅ Function that Flutter will call
function openBotmanFromFlutter() {
let tryCount = 0;
const maxTries = 10;
const interval = setInterval(() => {
if (typeof botmanChatWidget !== "undefined" && botmanChatWidget.open) {
botmanChatWidget.open();
setTimeout(() => {
botmanChatWidget.sayAsBot('Hi ' + emp_name + ', This is ILA your Insurance Assistant. Please choose the following options.');
botmanChatWidget.whisper('Hi');
}, 2000);
clearInterval(interval);
} else {
tryCount++;
if (tryCount >= maxTries) {
console.error("❌ Botman widget not available after waiting.");
clearInterval(interval);
}
}
}, 500); // retry every 500ms
}
</script>
</html>
''';
@override
Widget build(BuildContext context) {
return Stack(
children: [
WebViewWidget(controller: _controller),
Positioned(
right: 0,
bottom: 0,
child: FloatingActionButton(
onPressed: openBotman,
backgroundColor: Colors.blue,
child: const Icon(Icons.chat),
),
)
],
);
}
}