Skip to content

Terminal based application, written in Go, that serves as a custom client for Salesforce's Enhanced Chat (previously known as MIAW)

License

Notifications You must be signed in to change notification settings

alex-shekhter/miaw-tester

Repository files navigation

MIAW Tester - Command-Line Chat Client for Salesforce

1. Introduction

miaw-tester is a command-line interface (CLI) application, written in Go, that serves as a custom client for Salesforce's Messaging for In-App and Web (MIAW). It provides a direct, terminal-based interface for interacting with an Enhanced Messaging Channel, allowing for real-time, two-way communication with a Salesforce Service Agent.

1.1. Project Boundaries & Disclaimer

This application is an illustrative example. Its primary purpose is to demonstrate the API sequence and architectural patterns required to build a custom chat client for an unauthenticated user. It is not a production-ready, feature-complete solution. It serves as a foundation and a reference for developers looking to understand and implement the MIAW REST and Server-Sent Events (SSE) APIs.

2. Features

  • Interactive Chat Interface: A full-screen, terminal-based UI for sending and receiving messages.
  • Configuration Flexibility: Supports configuration via a JSON file (default-chat-cfg.json or specified with --config) with a fallback to interactive prompts for setup.
  • Adaptive UI: The user interface uses adaptive colors to ensure readability on both light and dark terminal themes.
  • Debug Logging: Includes a --debug flag to generate detailed logs of all API requests and responses for troubleshooting.

3. Getting Started

3.1. Prerequisites

  • Go (version 1.18 or later)
  • A configured Salesforce Enhanced Messaging Channel.

3.2. Build

Clone the repository and run the following command from the project's root directory to build the executable:

go build -o miaw-tester ./cmd/miaw-tester

3.3. Usage

There are two ways to run the application:

Option A: Using a Configuration File Create a JSON file named default-chat-cfg.json in the same directory as the executable.

Populate it with your Salesforce channel details:

{
  "OrganizationId": "00DO200000CwMXV",
  "DeveloperName": "MIAW_MOBILE_CH_1",
  "Url": "https://your-instance.my.salesforce-scrt.com",
}

Run the application:

./miaw-tester

To use a different configuration file, use the --config flag:

./miaw-tester --config my-prod-config.json

Option B: Interactive Prompts If no configuration file is found, the application will prompt you to enter the required details interactively.

4. Architecture

The application is designed with a clean separation of concerns, dividing responsibilities between the API client, the user interface, and the configuration loader.

4.1. High-Level Component Diagram

This diagram illustrates the primary components and their relationships.

classDiagram
    direction LR

    class cmd_Main {
        +main()
        +promptForConfig()
    }
    class internal_config_Config {
        +OrganizationId
        +DeveloperName
        +Url
    }
    class internal_config_Loader {
        +LoadFromFile(path) Config
    }
    class internal_tui_TUIModel {
        -client: salesforce.Client
        -viewport
        -textarea
        +Init()
        +Update()
        +View()
    }
    class internal_tui_Commands {
        +startSessionCmd()
        +sendMessageCmd()
        +waitForEventCmd()
    }
    class pkg_salesforce_SalesforceClient {
        -httpClient
        -baseURL
        -orgID
        +StartSession() error
        +SendMessage(text) error
        +ListenForMessages(eventCh)
    }
    class pkg_salesforce_Event["<<Interface>> Event"]
    class pkg_salesforce_ChatMessage
    class pkg_salesforce_ErrorMsg

    cmd_Main --> internal_config_Loader : Uses
    cmd_Main --> internal_config_Config : Creates
    cmd_Main --> internal_tui_TUIModel : Creates & Runs

    internal_tui_TUIModel --> internal_tui_Commands : Executes
    internal_tui_TUIModel --> pkg_salesforce_SalesforceClient : Holds reference to

    internal_tui_Commands --> pkg_salesforce_SalesforceClient : Calls methods on

    pkg_salesforce_SalesforceClient --> pkg_salesforce_Event : Sends events
    pkg_salesforce_ChatMessage --|> pkg_salesforce_Event
    pkg_salesforce_ErrorMsg --|> pkg_salesforce_Event
Loading

4.2. Sequence Diagram: Session Initialization

This diagram shows the flow of events from starting the application to establishing a chat session.

sequenceDiagram
    actor User
    participant Main as cmd/main
    participant ConfigLoader as config/Loader
    participant TUI as tui/Model
    participant SalesforceClient as salesforce/Client
    participant SalesforceAPI as Salesforce API

    User->>Main: ./miaw-tester
    Main->>ConfigLoader: LoadFromFile()
    alt Config File Not Found
        Main->>User: Prompt for Config
        User-->>Main: Provide Config
    else Config File Found
        ConfigLoader-->>Main: Return Config
    end

    Main->>TUI: NewModel(config)
    Main->>TUI: p.Run()
    TUI->>SalesforceClient: Init(): startSessionCmd()
    activate SalesforceClient
    SalesforceClient->>SalesforceAPI: POST /authorization/unauthenticated/access-token
    SalesforceAPI-->>SalesforceClient: 200 OK (accessToken, lastEventId)
    SalesforceClient->>SalesforceAPI: POST /conversation
    SalesforceAPI-->>SalesforceClient: 201 Created
    deactivate SalesforceClient
    SalesforceClient-->>TUI: sessionStartedMsg
    TUI->>User: Display "Successfully connected"
    TUI->>SalesforceClient: go ListenForMessages()
    activate SalesforceClient
    SalesforceClient->>SalesforceAPI: GET /eventrouter/v1/sse (stream)
    activate SalesforceAPI
    Note over SalesforceClient,SalesforceAPI: SSE Connection is now open
Loading

4.3. Sequence Diagram: Message Loop

This diagram illustrates the two-way, asynchronous flow of messages after the session is established.

sequenceDiagram
    actor User
    participant TUI as tui/Model
    participant SalesforceClient as salesforce/Client
    participant SalesforceAPI as Salesforce API

    User->>TUI: Types message and presses Enter
    TUI->>SalesforceClient: sendMessageCmd(text)
    activate SalesforceClient
    SalesforceClient->>SalesforceAPI: POST /conversation/{id}/message
    SalesforceAPI-->>SalesforceClient: 202 Accepted
    deactivate SalesforceClient

    participant Agent
    Agent->>SalesforceAPI: Sends message from Service Console
    SalesforceAPI-->>SalesforceClient: SSE Event (CONVERSATION_MESSAGE)
    activate SalesforceClient
    SalesforceClient-->>TUI: ChatMessage Event
    deactivate SalesforceClient
    TUI->>User: Displays Agent's message
Loading

5. Dependencies

  • Bubble Tea: A powerful TUI framework based on The Elm Architecture, used for the entire presentation layer.

  • Lipgloss: A library for declarative, stylish terminal rendering and adaptive colors.

  • PromptUI: A simple and elegant library for interactive command-line prompts.

  • r3labs/sse: A client library for handling Server-Sent Events connections.

About

Terminal based application, written in Go, that serves as a custom client for Salesforce's Enhanced Chat (previously known as MIAW)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages