@yepai/chatbot-sdk-core
v1.0.4
Published
A powerful and flexible chatbot SDK core library that provides essential functionality for building chatbot applications.
Readme
@yepai/chatbot-sdk-core
A powerful and flexible chatbot SDK core library that provides essential functionality for building chatbot applications.
Features
- Type-safe message handling
- Real-time communication with Socket.IO
- Schema validation with Zod
- Easy integration with any framework
- Automatic reconnection with configurable delay
- Heartbeat mechanism
- Environment-specific configuration
- Message encryption support
- Room-based chat
- Comprehensive event system
Installation
npm install @yepai/chatbot-sdk-coreUsage with Angular
1. Create a Chat Service
// chat.service.ts
import { Injectable } from '@angular/core';
import { MessageHubSdk, Message, SdkConfig } from '@yepai/chatbot-sdk-core';
@Injectable({
providedIn: 'root'
})
export class ChatService {
private sdk: MessageHubSdk;
constructor() {
const config: SdkConfig = {
url: 'ws://your-server',
apiKey: 'your-api-key',
virtualUserId: 'angular-user',
environment: 'browser',
autoReconnect: true,
reconnectDelay: 5000,
debug: false,
metadata: {
platform: 'web',
appVersion: '1.0.0'
},
heartbeat: {
interval: 15000,
timeout: 3000,
maxFailures: 2
}
};
this.sdk = new MessageHubSdk(config);
}
// Connect to the chat server
connect() {
return this.sdk.connect();
}
// Send a message
sendMessage(content: string) {
return this.sdk.sendMessage({
content,
type: 'text'
});
}
// Join a room
joinRoom(roomId: string) {
return this.sdk.joinRoom(roomId);
}
// Leave a room
leaveRoom(roomId: string) {
return this.sdk.leaveRoom(roomId);
}
// Listen for incoming messages
onMessage(callback: (message: Message) => void) {
return this.sdk.on('message', (event) => {
callback(event.message);
});
}
// Listen for connection status changes
onConnectionStatus(callback: (status: string) => void) {
return this.sdk.on('connection_status', (event) => {
callback(event.status);
});
}
// Disconnect from the server
disconnect() {
return this.sdk.disconnect();
}
}2. Create a Chat Component
// chat.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ChatService } from './chat.service';
import { Message } from '@yepai/chatbot-sdk-core';
@Component({
selector: 'app-chat',
template: `
<div class="chat-container">
<div class="connection-status">
Status: {{ connectionStatus }}
</div>
<div class="messages">
<div *ngFor="let message of messages" [class]="message.type">
{{ message.content }}
</div>
</div>
<div class="input-area">
<input [(ngModel)]="inputMessage" (keyup.enter)="sendMessage()" />
<button (click)="sendMessage()">Send</button>
</div>
</div>
`,
styles: [`
.chat-container {
display: flex;
flex-direction: column;
height: 100%;
}
.connection-status {
padding: 10px;
background: #f0f0f0;
}
.messages {
flex: 1;
overflow-y: auto;
}
.input-area {
padding: 10px;
display: flex;
gap: 10px;
}
`]
})
export class ChatComponent implements OnInit, OnDestroy {
messages: Message[] = [];
inputMessage = '';
connectionStatus = 'disconnected';
private unsubscribeMessage: () => void;
private unsubscribeStatus: () => void;
constructor(private chatService: ChatService) {}
ngOnInit() {
this.chatService.connect();
this.unsubscribeMessage = this.chatService.onMessage(message => {
this.messages.push(message);
});
this.unsubscribeStatus = this.chatService.onConnectionStatus(status => {
this.connectionStatus = status;
});
}
sendMessage() {
if (this.inputMessage.trim()) {
this.chatService.sendMessage(this.inputMessage);
this.inputMessage = '';
}
}
ngOnDestroy() {
this.unsubscribeMessage();
this.unsubscribeStatus();
this.chatService.disconnect();
}
}3. Add to App Module
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { ChatComponent } from './chat/chat.component';
@NgModule({
declarations: [
AppComponent,
ChatComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }API Reference
MessageHubSdk
The main class for interacting with the chat server.
const sdk = new MessageHubSdk(config: SdkConfig);Configuration
interface SdkConfig {
url: string;
apiKey: string;
virtualUserId: string;
environment: 'browser' | 'bot' | 'mobile' | 'desktop';
autoReconnect?: boolean;
reconnectDelay?: number;
debug?: boolean;
encryption?: {
enabled: boolean;
key: string;
};
metadata?: {
platform?: string;
osVersion?: string;
deviceId?: string;
appVersion?: string;
};
heartbeat?: {
interval: number;
timeout: number;
maxFailures: number;
};
}Methods
connect(): Promise<void>- Connect to the chat serverdisconnect(): Promise<void>- Disconnect from the chat serversendMessage(message: Message): Promise<void>- Send a messagejoinRoom(roomId: string): Promise<void>- Join a chat roomleaveRoom(roomId: string): Promise<void>- Leave a chat roomon<T extends EventType>(event: T, listener: EventListener<T>): void- Listen for eventsoff<T extends EventType>(event: T, listener: EventListener<T>): void- Remove event listenergetConnectionStatus(): ConnectionStatus- Get current connection statusisEncryptionEnabled(): boolean- Check if encryption is enabledgetApiKey(): string | undefined- Get the API key
Message
interface Message {
id?: string;
type: 'text' | 'image' | 'file' | 'system' | 'notification';
content: string;
encrypted?: boolean;
encryptedContent?: string;
roomId?: string;
metadata?: Record<string, unknown>;
timestamp?: Date;
}Events
enum EventType {
CONNECTION_STATUS = 'connection_status',
MESSAGE = 'message',
ERROR = 'error',
ROOM_JOINED = 'room_joined',
ROOM_LEFT = 'room_left',
ENCRYPTION_ERROR = 'encryption_error',
HEARTBEAT = 'heartbeat'
}License
MIT
