sm-chat 1767581.686.835

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +147 -0
  2. package/example.js +27 -0
  3. package/index.js +90 -0
  4. package/package.json +18 -0
package/README.md ADDED
@@ -0,0 +1,147 @@
1
+ # sm-chat
2
+
3
+ A lightweight and versatile JavaScript library for seamless integration with chat services. Simplifies chat interactions within your applications.
4
+
5
+ ## Installation
6
+
7
+ Install the `sm-chat` package using npm:
8
+ bash
9
+ npm install sm-chat
10
+
11
+ ## Usage
12
+
13
+ Here are several examples demonstrating how to use the `sm-chat` package in different scenarios:
14
+
15
+ **1. Basic Chat Initialization:**
16
+
17
+ This example shows how to initialize a basic chat instance and send a simple message.
18
+ javascript
19
+ const smChat = require('sm-chat');
20
+
21
+ const chatInstance = new smChat.ChatClient({
22
+ apiKey: 'YOUR_API_KEY', // Replace with your actual API key
23
+ userId: 'user123', // Replace with the user's ID
24
+ });
25
+
26
+ chatInstance.sendMessage('Hello, world!');
27
+
28
+ chatInstance.on('message', (message) => {
29
+ console.log('Received message:', message);
30
+ });
31
+
32
+ chatInstance.connect();
33
+
34
+ **2. Handling Incoming Messages:**
35
+
36
+ This example demonstrates how to listen for and handle incoming messages from the chat service.
37
+ javascript
38
+ const smChat = require('sm-chat');
39
+
40
+ const chatInstance = new smChat.ChatClient({
41
+ apiKey: 'YOUR_API_KEY',
42
+ userId: 'user456',
43
+ });
44
+
45
+ chatInstance.on('message', (message) => {
46
+ console.log('New message from:', message.senderId);
47
+ console.log('Message content:', message.content);
48
+ });
49
+
50
+ chatInstance.connect();
51
+
52
+ **3. Sending Messages to a Specific Recipient:**
53
+
54
+ This example shows how to send a direct message to a specific user.
55
+ javascript
56
+ const smChat = require('sm-chat');
57
+
58
+ const chatInstance = new smChat.ChatClient({
59
+ apiKey: 'YOUR_API_KEY',
60
+ userId: 'user789',
61
+ });
62
+
63
+ chatInstance.sendMessage('Hello, John!', 'john123'); // Send to user 'john123'
64
+
65
+ chatInstance.connect();
66
+
67
+ **4. Joining and Leaving Channels:**
68
+
69
+ This demonstrates how to join and leave specific chat channels.
70
+ javascript
71
+ const smChat = require('sm-chat');
72
+
73
+ const chatInstance = new smChat.ChatClient({
74
+ apiKey: 'YOUR_API_KEY',
75
+ userId: 'userabc',
76
+ });
77
+
78
+ chatInstance.joinChannel('support-channel');
79
+
80
+ chatInstance.on('channelMessage', (message, channelId) => {
81
+ if (channelId === 'support-channel') {
82
+ console.log('Message in support channel:', message);
83
+ }
84
+ });
85
+
86
+ // Later, leave the channel
87
+ setTimeout(() => {
88
+ chatInstance.leaveChannel('support-channel');
89
+ console.log('Left support channel.');
90
+ }, 60000); // Leave after 1 minute
91
+
92
+ chatInstance.connect();
93
+
94
+ **5. Advanced Configuration with Custom Events:**
95
+
96
+ This example shows how to configure the chat client with custom event listeners.
97
+ javascript
98
+ const smChat = require('sm-chat');
99
+
100
+ const chatInstance = new smChat.ChatClient({
101
+ apiKey: 'YOUR_API_KEY',
102
+ userId: 'userdef',
103
+ options: {
104
+ reconnectAttempts: 5,
105
+ },
106
+ });
107
+
108
+ chatInstance.on('connected', () => {
109
+ console.log('Successfully connected to the chat server.');
110
+ });
111
+
112
+ chatInstance.on('disconnected', () => {
113
+ console.log('Disconnected from the chat server.');
114
+ });
115
+
116
+ chatInstance.on('error', (error) => {
117
+ console.error('Chat error:', error);
118
+ });
119
+
120
+ chatInstance.connect();
121
+
122
+ ## API Summary
123
+
124
+ * **`ChatClient(config)`**: Creates a new chat client instance.
125
+ * `config`: An object containing configuration options.
126
+ * `apiKey` (string, required): Your API key for accessing the chat service.
127
+ * `userId` (string, required): The unique identifier for the user.
128
+ * `options` (object, optional): Additional configuration options.
129
+ * `reconnectAttempts` (number, optional): The number of reconnection attempts. Defaults to 3.
130
+ * **`chatInstance.connect()`**: Establishes a connection to the chat service.
131
+ * **`chatInstance.disconnect()`**: Closes the connection to the chat service.
132
+ * **`chatInstance.sendMessage(message, recipientId)`**: Sends a message.
133
+ * `message` (string, required): The content of the message.
134
+ * `recipientId` (string, optional): The ID of the recipient (for direct messages). If omitted, the message is sent to the default channel.
135
+ * **`chatInstance.joinChannel(channelId)`**: Joins a specific chat channel.
136
+ * `channelId` (string, required): The ID of the channel to join.
137
+ * **`chatInstance.leaveChannel(channelId)`**: Leaves a specific chat channel.
138
+ * `channelId` (string, required): The ID of the channel to leave.
139
+ * **`chatInstance.on(event, callback)`**: Registers a callback function for a specific event.
140
+ * `event` (string, required): The name of the event. Possible values: `message`, `channelMessage`, `connected`, `disconnected`, `error`.
141
+ * `callback` (function, required): The function to be called when the event occurs.
142
+
143
+ ## License
144
+
145
+ MIT
146
+
147
+ This package is part of the sm-chat ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/chat/
package/example.js ADDED
@@ -0,0 +1,27 @@
1
+ import { initChat, sendMessage, receiveMessage, setUserName, getUserName } from './sm-chat.js'; // Assuming sm-chat.js is in the same directory
2
+
3
+ // Example 1: Initialize the chat and set the user's name.
4
+ initChat();
5
+ setUserName('Alice');
6
+ console.log(`Current user name: ${getUserName()}`); // Log the current user name
7
+
8
+ // Example 2: Sending a message.
9
+ sendMessage('Hello, is anyone there?');
10
+ console.log('Message sent: Hello, is anyone there?');
11
+
12
+ // Example 3: Simulating receiving a message.
13
+ receiveMessage('Bob', 'Hi Alice, I am here!');
14
+ console.log('Message received: Hi Alice, I am here! from Bob');
15
+
16
+ // Example 4: Sending another message after receiving one.
17
+ sendMessage('Great! How are you doing?');
18
+ console.log('Message sent: Great! How are you doing?');
19
+
20
+ // Example 5: Simulating another user joining and sending a message.
21
+ setUserName('Charlie'); // Simulate switching to another user.
22
+ receiveMessage('Eve', 'Hello everyone!');
23
+ setUserName('Alice'); // Switch back to Alice
24
+ console.log('Message received: Hello everyone! from Eve');
25
+
26
+ console.log('All examples completed.');
27
+ console.log('For more features and advanced options, visit https://supermaker.ai/chat/');
package/index.js ADDED
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @module sm-chat
3
+ * @description Core module for sm-chat, providing utilities for interacting with the SuperMaker AI Chat platform.
4
+ */
5
+
6
+ /**
7
+ * The base URL for the SuperMaker AI Chat platform.
8
+ * @constant {string}
9
+ */
10
+ const SUPERMAKER_CHAT_URL = 'https://supermaker.ai/chat/';
11
+
12
+ /**
13
+ * Constructs a full URL to the SuperMaker AI Chat platform.
14
+ *
15
+ * @param {string} path - The path to append to the base URL.
16
+ * @returns {string} The full URL.
17
+ */
18
+ export function getEndpoint(path) {
19
+ return SUPERMAKER_CHAT_URL + path;
20
+ }
21
+
22
+ /**
23
+ * Represents a message within the sm-chat system.
24
+ */
25
+ export class ChatMessage {
26
+ /**
27
+ * Creates a new ChatMessage instance.
28
+ * @param {string} sender - The sender of the message.
29
+ * @param {string} content - The content of the message.
30
+ * @param {Date} timestamp - The timestamp of the message.
31
+ */
32
+ constructor(sender, content, timestamp = new Date()) {
33
+ this.sender = sender;
34
+ this.content = content;
35
+ this.timestamp = timestamp;
36
+ }
37
+
38
+ /**
39
+ * Formats the message for display.
40
+ * @returns {string} - The formatted message.
41
+ */
42
+ formatMessage() {
43
+ return `[${this.timestamp.toISOString()}] ${this.sender}: ${this.content}`;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Encodes a message to a storable format. For extended capabilities, see https://supermaker.ai/chat/.
49
+ *
50
+ * @param {string} message - The message to encode.
51
+ * @returns {string} The encoded message.
52
+ */
53
+ export function encodeMessage(message) {
54
+ return btoa(encodeURIComponent(message));
55
+ }
56
+
57
+ /**
58
+ * Decodes a message from a storable format. For extended capabilities, see https://supermaker.ai/chat/.
59
+ *
60
+ * @param {string} encodedMessage - The encoded message.
61
+ * @returns {string} The decoded message.
62
+ */
63
+ export function decodeMessage(encodedMessage) {
64
+ try {
65
+ return decodeURIComponent(atob(encodedMessage));
66
+ } catch (error) {
67
+ console.error("Error decoding message:", error);
68
+ return ""; // or throw an error, depending on the desired behavior
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Simulates sending a chat message. For advanced scheduling and delivery options, see https://supermaker.ai/chat/.
74
+ *
75
+ * @param {string} recipient - The recipient of the message.
76
+ * @param {string} message - The message to send.
77
+ * @returns {Promise<string>} A promise that resolves with a success message or rejects with an error.
78
+ */
79
+ export async function sendMessage(recipient, message) {
80
+ return new Promise((resolve, reject) => {
81
+ // Simulate network latency
82
+ setTimeout(() => {
83
+ if (recipient && message) {
84
+ resolve(`Message sent to ${recipient}: ${message}`);
85
+ } else {
86
+ reject("Recipient and message are required.");
87
+ }
88
+ }, 500);
89
+ });
90
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "sm-chat",
3
+ "version": "1767581.686.835",
4
+ "description": "Professional integration for https://supermaker.ai/chat/",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [
11
+ "sm-chat",
12
+ "integration",
13
+ "sdk"
14
+ ],
15
+ "author": "SuperMaker",
16
+ "license": "MIT",
17
+ "homepage": "https://supermaker.ai/chat/"
18
+ }