# Messages

## **yseries:server:messages:sent**

Triggered when a new message is successfully sent and saved to the database. This event fires after the message has been inserted into the database and notifications have been sent to recipients.

**Event Data:**

```lua
{
    messageId = string,        -- Unique message ID
    channelId = string,        -- Channel/conversation ID
    sender = string,           -- Sender's phone number
    senderImei = string|nil,   -- Sender's phone IMEI (can be nil for non-existent numbers like system messages)
    content = string,          -- Message content
    attachments = string|nil,  -- JSON string of attachments array (can be nil)
    participants = string|table, -- Recipient phone number(s) - can be string or array
    timestamp = string|nil,    -- Message timestamp from database
    targetSource = number|nil  -- Target player source ID if recipient is online (can be nil)
}
```

**Usage Example:**

```lua
RegisterNetEvent('yseries:server:messages:sent', function(messageData)
    print("New message sent!")
    print("Message ID:", messageData.messageId)
    print("From:", messageData.sender)
    print("To:", json.encode(messageData.participants, { indent = true }))
    print("Content:", messageData.content)
    
    -- Example: Log message to external system
    -- YourLoggingSystem:LogMessage(messageData)
    
    -- Example: Trigger custom logic based on message content
    -- if string.find(messageData.content, "keyword") then
    --     -- Do something
    -- end
end)
```

**Notes:**

* This event fires for all messages, including group messages
* The `senderImei` can be `nil` for messages sent from non-existent phone numbers (e.g. system messages)
* The `attachments` field is a JSON string that needs to be decoded if you want to access the attachment data
* The `participants` field can be either a string (single recipient) or a table/array (multiple recipients for group messages)
* The `targetSource` is only set if at least one recipient is online; otherwise it's `nil`
