react-native-genesys-chat
v1.0.0
Published
RN wrapper of genesys cloud SDK
Downloads
9
Maintainers
Readme
react-native-genesys-chat
A React Native plugin to integrate Genesys Chat SDK into your mobile applications, allowing you to manage chat sessions, send messages, and listen to events such as incoming messages, typing notifications, and more.
Features
- Initialize chat sessions with Genesys.
- Send messages to the Genesys chat service.
- Listen for events like incoming messages and typing notifications.
- Clean up and disconnect chat sessions.
Installation
To install the plugin, run:
npm install react-native-genesys-chatLinking
If you are using React Native 0.60 or above, the package should be automatically linked. For older versions, you may need to link the package manually:
react-native link react-native-genesys-chatInstallation
For iOS, make sure to run:
- Add these sources to your Podfile:
source 'https://github.com/MyPureCloud/gc-sdk-specs'
source 'https://github.com/CocoaPods/Specs'- Then, run:
cd ios/
pod installThis ensures that you have the correct sources configured for the genesys pod installation.
Usage
import React, { useEffect, useState } from "react";
import {
setupMessengerTransport,
genesysChatEventEmitter,
sendMessage,
disconnectAndCleanup,
} from "react-native-genesys-chat";
import { View } from "react-native";
const ChatComponent = () => {
const [chats, setChats] = useState([]);
const [isAgentCompanion, setIsAgentCompanion] = useState(false);
const attributes = {};
useEffect(() => {
// Initialize the SDK with your deployment and domain details
setupMessengerTransport(
"PROD", // Deployment environment, e.g., 'PROD'
"usw2.pure.cloud" // Domain
);
// Start chat session
sendMessage("init chat", attributes); // 'attributes' is a additional options
// Event listeners
const genMsgSubscription = genesysChatEventEmitter.addListener(
"onGenesysMessage",
(message) => {
if (!isAgentCompanion && message.from === "human") {
setIsAgentCompanion(true);
}
setChats((prevChats) => [...prevChats, message]);
}
);
const genEventsSubscription = genesysChatEventEmitter.addListener(
"onTyping",
(events) => {
console.log("Typing Event:", events);
}
);
return () => {
disconnectAndCleanup();
genMsgSubscription.remove();
genEventsSubscription.remove();
};
}, []);
return <View>{/* Render chats or other UI components here */}</View>;
};
export default ChatComponent;API
setupMessengerTransport(deploymentId: string, domain: string, attributes?: { [key: string]: string }, logging: boolean = false)
Sets up the messaging transport for the chat SDK.
Parameters:
deploymentId: The deployment environment (e.g., 'PROD').domain: The domain for your Genesys environment (e.g., 'usw2.pure.cloud').attributes: Optional attributes to send with the session.logging: Optional boolean for enabling logging (default isfalse).
sendMessage(query: string, attributes?: { [key: string]: string })
Sends a message to the Genesys chat service.
Parameters:
query: The message you want to send.attributes: Optional custom attributes to attach to the message.
disconnectAndCleanup()
Disconnects and cleans up the chat session when finished.
Parameters: None.
Events
The package exposes several events that you can listen to using the genesysChatEventEmitter.
onGenesysMessage
- Description: Triggered when a new message is sent or received from the chat service.
- Callback Signature:
(message: Message) => voidmessage: The message object containing details likefrom,text, etc.
onTyping
- Description: Triggered when a user is typing a message in the chat.
- Callback Signature:
(state: TypingState) => voidstate: The typing state object containing:isTyping: A boolean indicating whether the user is typing.durationInMilliseconds: (optional) The duration in milliseconds that the user has been typing.
onMessengerState
- Description: Triggered when the messenger state changes (e.g., started, ended).
- Callback Signature:
(state: MessengerState) => voidstate: The messenger state object containing:state: The current state of the messenger, which can be'started'or'ended'.
onMessengerError
- Description: Triggered when an error occurs within the messenger.
- Callback Signature:
(error: unknown) => voiderror: The error object containing the details of the issue.
Contributing
Contributions are welcome! Please feel free to fork this repository, create a branch, and submit pull requests. For any issues or feature requests, please open an issue in the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
