Assuming flutter application is available
Add below snippet into flutter app pubspec.yaml “path” is where plugin is copied after that need to run “flutter pub get”
dependencies:
flutter:
sdk: flutter
korebotplugin:
# the parent directory to use the current plugin's version.
path: ../
Create a “Method channel” with channel name as below
static const platform = MethodChannel('kore.botsdk/chatbot');
Create a method which invokes the chat window as below here the method name is
“_callNativemethod” can be changed as per requirement.
Future<void> _callNativemethod() async {
platform.setMethodCallHandler((handler) async {
if (handler.method == 'Callbacks') {
// Do your logic here.
debugPrint("Event from native ${handler.arguments}");
}
});
try {
final String result = await platform.invokeMethod('getChatWindow');
} on PlatformException catch (e) {}
}
On button press the above mentioned method can be called to open the chat window as below
children: [
ElevatedButton(
onPressed: _callNativemethod,
child: const Text('Bot Connect'),
),
],
All the callbacks from native to the flutter application happens in the below snippet. Users can implement their own logics as per requirement.
platform.setMethodCallHandler((handler) async {
if (handler.method == 'Callbacks') {
// Do your logic here.
debugPrint("Event from native ${handler.arguments}");
}
});
Callbacks received are in below json format which can be consumed by the clients and implemented as per requirement.
When fails in fetching jwt token
{"eventCode":"Error_STS","eventMessage":"STS call failed"}
When fails in Socket(Bot) Connection
{"eventCode":"Error_Socket","eventMessage":"Socket connection failed"}
When Bot connected successfully
{"eventCode":"BotConnected","eventMessage":"Bot connected successfully"}
When User clicks the back button on the chat window in IOS or hardware back button in android.
{"eventCode":"BotClosed","eventMessage":"Bot closed by the user"}
Add below lines in AppDelegate.swift
![image](https://private-user-images.githubusercontent.com/64408292/259973351-fb33b51c-1795-48af-933b-cae0bf0bbe69.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg4ODc2NTMsIm5iZiI6MTcxODg4NzM1MywicGF0aCI6Ii82NDQwODI5Mi8yNTk5NzMzNTEtZmIzM2I1MWMtMTc5NS00OGFmLTkzM2ItY2FlMGJmMGJiZTY5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjIwVDEyNDIzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTliMjI0NDEzZmRmNmQ1YTIwNzgzZmJjOWJkNGFkZTVlNGU0YjVlOTUzMTJlODQyYjgwNjQ2MjgxYjgwMDE3MWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.4DiCfCxmPZT7NU5M5ZiJqL9QIbquc7RbCXjXZk49MTk)
//Callbacks from chatbotVC
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "CallbacksNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.callbacksMethod), name:
NSNotification.Name(rawValue: "CallbacksNotification"), object: nil)
@objc func callbacksMethod(notification:Notification) {
let dataString: String = notification.object as! String
if let eventDic = convertStringToDictionary(text: dataString){
if flutterMethodChannel != nil{
flutterMethodChannel?.invokeMethod("Callbacks", arguments: eventDic)
}
}
}
func convertStringToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
![image](https://private-user-images.githubusercontent.com/64408292/259973069-7a6b82c6-c0f3-4d1c-af1f-e7fbedbbb6d4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg4ODc2NTMsIm5iZiI6MTcxODg4NzM1MywicGF0aCI6Ii82NDQwODI5Mi8yNTk5NzMwNjktN2E2YjgyYzYtYzBmMy00ZDFjLWFmMWYtZTdmYmVkYmJiNmQ0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjIwVDEyNDIzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTEzNTRiNjJhNGIwODFhNzE2Y2IwYjUwYTI4ZjhlZmE0MzUxZmU2NjNiYjcyZjIwMDhiOWI0NjYzYTU1YmZjODAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.UrhyOC7DXOi11x9DqEGpkzyp5ykVCJzRoCrnDmLlC3o)