# :icon-comment: Sending messages

In this page we'll guide you on creating custom messages from other scripts. <br>
You can still create messages through the **"chat:addMessage" event** or **our export** even though these
approaches are discouraged.

<div style="display: flex; width: 100%; margin-block: 2rem; gap: 1rem;">
  <img style="width: 50%;" src="/static/contents/chat/preview_chat_component.png">
  
  <div>
    <h3 style="text-align: center;">What fields are used to render a message?</h3>
    <span style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.5rem;">
      <code>setBackground(color: hexcolor)</code>
      <b style="padding-inline: 0.40rem; background-color: #8C39BD; border-radius: 0.35rem; color: white;">Background</b>
    </span>
    <span style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.5rem;">
      <code>setDecoration(content: string, color?: hexcolor)</code>
      <b style="padding-inline: 0.40rem; background-color: #6539EF; border-radius: 0.35rem; color: white;">Decor</b>
    </span>
    <span style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.5rem;">
      <code>addTag(content: string, color?: hexcolor)</code>
      <b style="padding-inline: 0.40rem; background-color: #8C39BD; border-radius: 0.35rem; color: white;">Tags[#Tags++]</b>
    </span>
    <span style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.5rem;">
      <code>setPrefix(content: string, color?: hexcolor)</code>
      <b style="padding-inline: 0.40rem; background-color: #6539EF; border-radius: 0.35rem; color: white;">Prefix</b>
    </span>
    <span style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.5rem;">
      <code>setMessage(content: string, color?: hexcolor)</code>
      <b style="padding-inline: 0.40rem; background-color: #BF3965; border-radius: 0.35rem; color: white;">Message</b>
    </span>
  </div>
  
</div>

### 1. Creating & Sending a message
It's incredibly easy to customize a message. You'll be needing to export our Message Factory provided by our chatApi.
After doing so, you'll have access to a set of methods that'll effortesly help you create messages from the ground up. Only the message content
is required and if no color is provided, we'll default to your template defaults.

```lua
-- getApiContext is available Client & Server side
local chatApi = exports["of_chat"]:getApiContext()

  chatApi.createMessage("Some fancy runtime created message")
    :setDecoration("ID 2") -- No color provided? We default to the template color
    -- :setTargets({ 1, 2 }) When using the export server-side (Who's gonna receive the message)
    :addTag("POLICE", "#0000ff")
    :setPrefix("Amyset", "#000000")
    :setBackground("#ffffff")
    :send()

  -- Fields are optional but the message content
  chatApi.createMessage("Another fancy runtime created message")
    :setPrefix("Amyset", "#000000")
    :setBackground("#ffffff")
    :send()

  -- Text content can be multi-fragment
  chatApi.createMessage({"My", "multi", "fragmented", nil, "message"})
    :setPrefix({"Name", "Firstname"}, "#000000")
    :setBackground("#ffffff")
    :send()

```
