Skip to content

ScerIO/usedesk.dart

Repository files navigation

usedesk

Pure dart SDK implementation of usedesk.com service Now added only UsedeskChat sdk

pub package

Getting Started

In Example dir fully flutter chat integration with package flutter_chat_ui

Integrate anywhere by steps:

  1. 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');
  }
}
  1. 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',
    ),
);
  1. Identify client / pass additional data
usedeskChat
  ..identify = IdentifyConfiguration(
      /* All params optional */
      name: 'Serge Shkurko',
      email: 'mySuper@email.com',
      phoneNumber: 88005553535,
      additionalId: 'uuid_in_my_system',
  )
  ..additionalFields = { 
    '99999': 'v$appVersion:$appBuildNumber' 
  };
  1. 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);
    }
});

  1. Control connection
usedeskChat.connect(); // for receiving usedesk messages from socket

usedeskChat.disconnect(); // dismiss connection 
  1. Send message
// Text
usedeskChat.sendText('My cool message');

// File
usedeskChat.sendFile('My_cool_image.jpg', fileBytes);

Contributing

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

About

Pure dart SDK implementation of usedesk.ru

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages