In order to use Stringee Live Chat API, you need to have a widget key which will be used to identify your portal. You can get that key by doing the following steps:
$ npm install stringee-react-native-v2 --save
Permissions
The Stringee Android SDK requires some permissions from your AndroidManifest
android/app/src/main/AndroidManifest.xml
<!--Internet-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Proguard
If your project uses ProGuard, you may have to add the following settings to the ProGuard configuration file to make sure Stringee builds correctly:
proguard-rules.pro
in your app/
dir and insert inside:#Flutter Wrapper
-dontwarn org.webrtc.**
-keep class org.webrtc.** { *; }
-keep class com.stringee.** { *; }
/app/buidl.gradle
:android {
buildTypes {
release {
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Add volley library
In your file build.gradle
add this line:
dependencies {
implementation 'com.android.volley:volley:1.2.1'
}
In you terminal, change into your ios directory, from the command line run following command:
pod install --repo-update
After run cocoapods command, open project file .xcworkspace
In the "Build Settings" tab -> "Other linker flags" add "$(inherited)" flag
In the "Build Settings" tab -> "Enable bitcode" select "NO"
Right-click the information property list file (Info.plist) and select Open As -> Source Code. Then insert the following XML snippet into the body of your file just before the final element:
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) uses Camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) uses Microphone</string>
In the "Build Settings" tab -> "Allow Non-modular includes in Framework Modules" select "YES"
Initialize StringeeClient
import {StringeeClient} from 'stringee-react-native-v2';
...
const stringeeClient: StringeeClient = new StringeeClient();
Register client events
// Listen for the StringeeClient event
const stringeeClientListener: StringeeClientListener = new StringeeClientListener();
// Invoked when the StringeeClient is connected
stringeeClientListener.onConnect = (stringeeClient, userId) => {
console.log('onConnect: ', userId);
};
// Invoked when the StringeeClient is disconnected
stringeeClientListener.onDisConnect = (stringeeClient) => {
console.log('onDisConnect');
};
// Invoked when StringeeClient connect false
stringeeClientListener.onFailWithError = (stringeeClient, code, message) => {
console.log('onFailWithError: ', message);
};
// Invoked when your token is expired
stringeeClientListener.onRequestAccessToken = (stringeeClient) => {
console.log('onRequestAccessToken');
};
// Invoked when conversation or message update
stringeeClientListener.onObjectChange = (stringeeClient, objectType, objectChanges, changeType) => {
console.log('onObjectChange: ', objectType, objectChanges, changeType);
};
// Invoked when a chat request timed out and no agent in queue accept
stringeeClientListener.onTimeoutInQueue = (stringeeClient, convId, customerId, customerName) => {
console.log('onTimeoutInQueue: ', convId, customerId, customerName);
};
// Invoked when the conversation is ended
stringeeClientListener.onConversationEnded = (stringeeClient, convId, endedby) => {
console.log('onConversationEnded: ', convId, endedby);
};
stringeeClient.setListener(stringeeClientListener);
Chat Profile is an object which will let you know:
stringeeClient.getChatProfile('YOUR_WIDGET_KEY').then(chatProfile => {
console.log('getChatProfile', JSON.stringify(chatProfile));
}).catch(console.log);
In order to chat with your agents, your customers have to connect to Stringee server first. But they are not users in your system, they can be anybody, so you need to generate a token for them. In order to do that, call the following function:
stringeeClient.getLiveChatToken('YOUR_WIDGET_KEY', 'YOUR_CUSTOMER_NAME', 'YOUR_CUSTOMER_EMAIL').then(token => {
console.log('getLiveChatToken', token);
// After get token then can connect
stringeeClient.connect(token);
}).catch(console.log);
In which:
Before starting a live chat conversation, you can update the customer information to Stringee Server, so your agent can know more about your customer (where is the customer from? what type of cell phone the customer using?...). In order to do that, call the following function:
stringeeClient.updateUserInfo("USER_NAME", "USER_EMAIL", "USER_AVATAR").then(() => {
console.log('updateUserInfo success');
}).catch(console.log);
Then start a live chat conversation by calling:
stringeeClient.createLiveChatConversation(queueId).then(conversation => {
console.log('createLiveChatConversation', JSON.stringify(conversation));
}).catch(console.log);
In which:
Follow this instruction Messages
In order to end the live chat conversation, you call the following method:
conversation.endChat().then(() => {
console.log('endChat success');
}).catch(console.log);
Next, we will connect to Stringee Server. You must do this before you can start a live chat conversation.
Initialize StringeeClient
import {StringeeClient} from 'stringee-react-native-v2';
...
const stringeeClient: StringeeClient = new StringeeClient();
Register client events
// Listen for the StringeeClient event
const stringeeClientListener: StringeeClientListener = new StringeeClientListener();
// Invoked when the StringeeClient is connected
stringeeClientListener.onConnect = (stringeeClient, userId) => {
console.log('onConnect: ', userId);
};
// Invoked when the StringeeClient is disconnected
stringeeClientListener.onDisConnect = (stringeeClient) => {
console.log('onDisConnect');
};
// Invoked when StringeeClient connect false
stringeeClientListener.onFailWithError = (stringeeClient, code, message) => {
console.log('onFailWithError: ', message);
};
// Invoked when your token is expired
stringeeClientListener.onRequestAccessToken = (stringeeClient) => {
console.log('onRequestAccessToken');
};
// Invoked when conversation or message update
stringeeClientListener.onObjectChange = (stringeeClient, objectType, objectChanges, changeType) => {
console.log('onObjectChange: ', objectType, objectChanges, changeType);
};
// Invoked when the client receives chat request
stringeeClientListener.onReceiveChatRequest = (stringeeClient, request) => {
console.log('onReceiveChatRequest', JSON.stringify(request));
};
// Invoked when the client receives transfer chat request
stringeeClientListener.onReceiveTransferChatRequest = (stringeeClient, request) => {
console.log('onReceiveTransferChatRequest', JSON.stringify(request));
};
// Invoked when a chat request time out for answer and route to next agent
stringeeClientListener.onTimeoutAnswerChat = (stringeeClient, request) => {
console.log('onTimeoutAnswerChat', JSON.stringify(request));
};
// Invoked when the conversation is ended
stringeeClientListener.onConversationEnded = (stringeeClient, convId, endedby) => {
console.log('onConversationEnded: ', convId, endedby);
};
stringeeClient.setListener(stringeeClientListener);
Connect to Stringee
const token: string = 'PUT YOUR TOKEN HERE'
...
stringeeClient.connect(token);
After receive a chat request, agent can accept or reject this chat request.
For accept chat request, call the following function:
chatRequest.acceptChatRequest().then(() => {
console.log('acceptChatRequest success');
// if accept success, you can get conversation to start chatting
stringeeClient.getConversationById(chatRequest.convId).then(conversation => {
console.log('getConversationById', JSON.stringify(conversation));
}).catch(console.log);
}).catch(console.log);
After accept chat request success, you can get conversation by conversationId from chat request
For reject chat request, call the following function:
chatRequest.rejectChatRequest().then(() => {
console.log('acceptChatRequest success');
// if accept success, you can get conversation to start chatting
stringeeClient.getConversationById(chatRequest.convId).then(conversation => {
console.log('getConversationById', JSON.stringify(conversation));
}).catch(console.log);
}).catch(console.log);
Follow this instruction Messages
In order to end the live chat conversation, you call the following method:
conversation.endChat().then(() => {
console.log('endChat success');
}).catch(console.log);
For agent want to create a ticket for a missed live chat conversation, you call:
const liveChatTicketParam: LiveChatTicketParam = new LiveChatTicketParam({
name: 'CUSTOMER_NAME',
email: 'CUSTOMER_EMAIL',
phone: 'CUSTOMER_PHONE',
note: 'TICKET_DESCRIPTION',
});
stringeeClient.createLiveChatTicket('YOUR_WIDGET_KEY', liveChatTicketParam).then(() => {
console.log('createLiveChatTicket success');
}).catch(console.log);
If you want to send chat's content to an email at any time, you can use the following function:
conversation.sendChatTranscript('EMAIL', 'DOMAIN').then(() => {
console.log('sendChatTranscript success');
}).catch(console.log);
You can view a completed version of this sample app on GitHub: https://github.com/stringeecom/react-native-samples/tree/master/LiveChatSample