Pure dart SDK implementation of usedesk.com service
Now added only UsedeskChat
sdk
In Example dir fully flutter chat integration with package flutter_chat_ui
Integrate anywhere by steps:
- Need implement token storage, example with shared_preferences:
import 'package:shared_preferences/shared_preferences.dart';
import 'package:usedesk/usedesk.dart';
class SharedPreferencesUsedeskChatStorage extends UsedeskChatStorageProvider {
SharedPreferencesUsedeskChatStorage(this.prefs);
final SharedPreferences prefs;
@override
Future<String?> getToken() async {
return prefs.getString('token');
}
@override
Future<void> setToken(String token) {
return prefs.setString('token', token);
}
@override
Future<void> clearToken() {
return prefs.remove('token');
}
}
- Initialize chat
final usedeskChat = await UsedeskChat.init(
/* Required */
storage: SharedPreferencesUsedeskChatStorage(prefs),
companyId: companyId,
/* Optional */
channelId: channelId,
apiConfig: const ChatApiConfiguration(
urlChat: 'https://pubsubsec.usedesk.ru',
urlOfflineForm: 'https://secure.usedesk.ru/',
urlToSendFile: 'https://secure.usedesk.ru/uapi/v1/send_file',
),
);
- Identify client / pass additional data
usedeskChat
..identify = IdentifyConfiguration(
/* All params optional */
name: 'Serge Shkurko',
email: '[email protected]',
phoneNumber: 88005553535,
additionalId: 'uuid_in_my_system',
)
..additionalFields = {
'99999': 'v$appVersion:$appBuildNumber'
};
- Subscribe on messages stream
usedeskChat.messagesStream.listen((List<MessageBase> message) {
MessageBase message = message.first;
final isMyMessage = message is MessageFromClient;
// Work with different messages
if (message is MessageTextBase) {
print(message.text);
} else if (message is MessageImageBase) {
print(message.file);
} else if (message is MessageFileBase) {
print(message.file);
}
});
- Control connection
usedeskChat.connect(); // for receiving usedesk messages from socket
usedeskChat.disconnect(); // dismiss connection
- Send message
// Text
usedeskChat.sendText('My cool message');
// File
usedeskChat.sendFile('My_cool_image.jpg', fileBytes);
Rebuild code generation tools
# One time run
dart pub run build_runner build --delete-conflicting-outputs
# Watching of file changes
dart pub run build_runner watch --delete-conflicting-outputs