Mezon SDK Technical Documentation

Mezon Development Team

April 21, 2025

Introduction

The Mezon SDK provides developers with a simple and efficient way to interact with the Mezon platform. This document outlines the core functionalities of the SDK, focusing on client initialization, event listening, and channel object retrieval.

Installation

Note: Installation instructions will depend on the target environment (e.g., npm for Node.js, CDN for browser). Please refer to the specific installation guide for your platform.

Core Functionalities

1. Initializing the Client

The MezonClient class is the entry point for interacting with the Mezon platform. To begin, you need to instantiate a MezonClient object with your authentication token.

Syntax

        
            const client = new MezonClient("your_authentication_token");
        
    

Parameters

Return Value

Example

        
            const apiKey = "abcdef1234567890";
            const client = new MezonClient(apiKey);
            console.log("Mezon Client initialized:", mezon);
            await client.login();
        
    

2. Listening for Events

The MezonClient provides a mechanism to listen for various events emitted by the Mezon platform. The onMessage event listener allows you to handle incoming messages.

Syntax

        
            client.onChannelMessage = (event: ChannelMessage) => {
              // Your message handling logic here
              console.log("Received message:", event);
            };
        
    

Parameters

Return Value

Example

        
            client.onChannelMessage = (message) => {
              console.log(`New message from channel ${message.channel_id}:`, message.content);
              // Process the message data
              if (message.content.t === 'hello') {
                console.log(`Chat message content: ${message.content.t}`);
              }
            };
        
    

3. Initiating a Channel Object

The channel object provides methods for interacting with specific channels on the Mezon platform. The Find method allows you to retrieve a reference to a channel using its unique identifier.

Syntax

        
            const channel = client.channels.fetch(channel_id);
        
    

Parameters

Return Value

Example

        
            const myChannel = await client.channels.fetch(myChannelId);
            console.log("Channel object:", myChannel);
            
                 // send message to myChannel
            await myChannel.send({t: "Hello everyone!"});
        
    

4. Initiating a Message Object

The message object provides methods for interacting with specific message on the Mezon platform. The fetch method allows you to retrieve a reference to a message using its unique identifier and the channel it belongs to.

Syntax

        
            const channel = await client.channels.fetch(channel_id);
            const message = await channel.messages.fetch(message_id);
        
    

Parameters

Return Value

Example

        
            const myChannelId = "group-chat-123";
            const myChannel = await client.channels.fetch(myChannelId);
            const myMessageId = "message-chat-123";
            const myMessage = await channel.messages.fetch(myMessageId);
            // reply a message
            await myMessage.reply({t: "Hello everyone!"});
            // update a message
            await myMessage.update({t: "Hello everyone! (edited)"});
            // delete a message
            await myMessage.delete();
        
    

5. Initiating a User Object

The user object provides methods for interacting with specific user on the Mezon platform. The fetch method allows you to retrieve a reference to a user using its unique identifier and the clan user belongs to.

Syntax

        
            const clan = await client.clans.fetch(clan_id ?? '0'); // '0' use for DM message
            const user = await clan.users.fetch(user_id);
        
    

Parameters

Return Value

Example

        
            const myClanId = "clan-chat-123";
            const myClan = await client.clans.fetch(myClanlId);
            const userId = "user-123";
            const user = await clan.users.fetch(userId);
            // send a DM message
            await user.sendDM({t: "Hello!"});
        
    

Embedded Message Format

Mezon also supports sending rich messages using an embedded format. This allows for structured messages with colors, titles, authors, descriptions, fields, images, and more. Below is the TypeScript interface defining the structure for an embedded message.

        
            export interface IEmbedProps {
              color?: string;
              title?: string;
              url?: string;
              author?: {
                name?: string;
                icon_url?: string;
                url?: string;
              };
              description?: string;
              thumbnail?: {
                url: string;
              };
              fields?: {
                name: string;
                value: string;
                inline?: string;
              };
              images?: {
               url: string;
               width: string;
               height: string;
              };
              timestamp?: string;
              footer?: string;
            }
        
    

Example

        
            const myChannelId = "group-chat-123";
            const myChannel = await client.channels.fetch(myChannelId);
            const embed = [{
                color: #fff,
                title: 'Message embed',
                description: "Description message embed",
              }];
            await myChannel.send({embed});
        
    

Webhook Format

Mezon supports receiving messages via webhooks. Below is an example of the expected format for a webhook payload sent to your registered webhook URL.

        
curl --location 'https://webhook.mezon.ai/webhooks/1839225926886363136/MTcyNzM0MDYyMTU2NTczNzQzMzoxODM5MjI1NzgyNjY1MjIwMDk2OjE4MzkyMjU5MjY4ODYzNjMxMzY.' \
--header 'Content-Type: application/json' \
--data-raw '{
    "type": "hook",
    "message": {
        "t": "long@long 123456",
        "mk": [
            {
                "type": "lk",
                "s": 0,
                "e": 22
            }
        ],
        "mk": [
            {
                "type": "pre",
                "s": 0,
                "e": 22
            }
        ],
        "mentions": [
            {
                "username":"nhan.nguyentran",
                "user_id":"134221824",
                "s": 0,
                "e":17
            }
        ],
        "images": [
            {
                "fn":"thumbnail_dog1.jpg",
                "sz":5620,
                "url":"https://cdn.mezon.vn/0/1843962578301095936/1829065039080853500/95_0thumbnail_dog1.jpg",
                "ft":"image/jpeg",
                "w":275,
                "h":183
            }
        ]
    }
}'
        
    

Clan hook

      
      {
        "content": "{\"t\": \"haha\"}",
        "attachments": [
            {
            "filename": "bandicam 2024-11-19 11-10-14-507.mp4",
            "size": 2671970,
            "url": "https://cdn.mezon.vn/109056000/255856640/4198400/173_0bandicam_2024_11_19_11_10_14_507.mp4",
            "filetype": "video/mp4",
            "width": 1280,
            "height": 724
            }
        ]
      }
      
    

The message object within the webhook payload contains details about the received message, including text, formatting, mentions, and attachments like images.

Embedded Message Format

Mezon also supports sending rich messages using an embedded format. This allows for structured messages with colors, titles, authors, descriptions, fields, images, and more. Below is the TypeScript interface defining the structure for an embedded message.

        
export interface IEmbedProps 
{
  color?: string;
  title?: string;
  fields?: IField[];
}

export interface IField 
{
  name: string;
  value: string;
  inputs?: IInputs;
}

export interface IInputs 
{
  id: string;
  type:  6 ;
  component: IComponent;
}

export interface IComponent 
{
  url_image: string;
  url_position: string;
  pool: string[][];
  repeat: number;
  duration: number;
}

Embed Animtion Example



    
const embed: IEmbedProps = {
  color: '#E67E22',
  title: '🎰 Kết quả Slots 🎰',
  fields: [
    {
      name: '',
      value: '',
      inputs: {
        id: 'slots',
        type: 6,
        component: {
          url_image: 'https://cdn.mezon.ai/...spritesheet.png',
          url_position: 'https://cdn.mezon.ai/...spritesheet.json',
          pool: [
            ['1.JPG', '2.JPG', ..., '14.JPG'],
            ['1.JPG', '2.JPG', ..., '10.JPG'],
            ['1.JPG', '2.JPG', ..., '11.JPG']
          ],
          repeat: 6,
          duration: 0.5
        }
      }
    }
  ]
};