1.0.1 • Published 5 months ago

rocket-chat-widget-integration v1.0.1

Weekly downloads
-
License
Aevoco
Repository
-
Last release
5 months ago

Rocket.Chat Web Integration

A lightweight JavaScript library for seamlessly integrating Rocket.Chat into web applications with additional auto-reply capabilities.

Features

  • 🚀 Easy integration with Rocket.Chat
  • 🎨 Customizable configuration
  • 🔒 Secure with api key
  • 🔑 Easy to use with session storage or a path to get the user context

Installation

npm install rocket-chat-web-integration or yarn add rocket-chat-web-integration

Quick Start

  1. Import the RocketChatIntegration class:

html

Configuration Options

OptionTypeRequiredDefaultDescription
rocketChatUrlstringYes-URL of your Rocket.Chat server
autoReplyServerUrlstringNonullURL of your auto-reply server
userEmailstringNonullUser's email for identification
logoutFormIdstringNo'logoutForm'ID of the logout form element
userContextPathstringNonullPath to user context in window object
getUserContextfunctionNonullFunction to get user context
useSessionStoragebooleanNofalseUse session storage for user context
sessionStorageKeystringNo'userContext'Key for session storage
fieldMappingsobjectNo{}Custom field mappings for user data

Basic Integration

With Auto-Reply Feature

<script type="module">
    import { RocketChatIntegration } from 'rocket-chat-web-integration';

    const chatWidget = new RocketChatIntegration({
        rocketChatUrl: 'https://chat.example.com',
        autoReplyServerUrl: 'https://autoreply.example.com',
        userEmail: 'user@example.com'
    });
</script>

Auto-register the user in Rocket.Chat widget if the user is logged in

Using Session Storage

<script type="module">
    import { RocketChatIntegration } from 'rocket-chat-web-integration';

    // Store user context in session storage (pass in the session storage key)
    const chatWidget = new RocketChatIntegration({
        rocketChatUrl: "http://localhost:3000",
        autoReplyServerUrl: "http://localhost:5800",
        useSessionStorage: true,
        sessionStorageKey: "customUserContextKey"
    });
</script>

Using Context Path

<script type="module">
    import { RocketChatIntegration } from 'rocket-chat-web-integration';

    // Define user context in window object
    window.testUser = {
        identity: {
            email: "user@example.com",
            name: "Test User"
        }
    };

    const chatWidget = new RocketChatIntegration({
        rocketChatUrl: "http://localhost:3000",
        autoReplyServerUrl: "http://localhost:5800",
        userContextPath: "testUser.identity"
    });
</script>

Using Custom Context Function

<script type="module">
    import { RocketChatIntegration } from 'rocket-chat-web-integration';

    const chatWidget = new RocketChatIntegration({
        rocketChatUrl: "http://localhost:3000",
        autoReplyServerUrl: "http://localhost:5800",
        getUserContext: () => {
          // Return a promise that resolves with the user context
          return fetch("http://localhost:5800/api/v1/users/me")
            .then((response) => {
              if (!response.ok) {
                throw new Error("Failed to fetch user context");
              }
              return response.json();
            })
            .then((data) => ({
              email: data.email,
              name: data.name || data.username,
              username: data.username,
            }))
            .catch((error) => {
              console.error("Error fetching user context:", error);
              return null;
            });
        },
    });
</script>

With Logout Handling

<form id="customLogoutForm" action="/logout" method="POST">
    <button type="submit">Logout</button>
</form>

<script type="module">
    import { RocketChatIntegration } from 'rocket-chat-web-integration';

    const chatWidget = new RocketChatIntegration({
        rocketChatUrl: 'https://chat.example.com',
        logoutFormId: 'customLogoutForm'
    });
</script>

License

This project is licensed under the Aevoco Sdn Bhd.

Support

If you encounter any problems or have questions, please feel free to contact us at hello@aevoco.com.

1.0.1

5 months ago

1.0.0

5 months ago