discourse/plugins/chat/app/models/chat
Joffrey JAFFEUX 582de0ffe3
DEV: adds blocks support to chat messages (#29782)
Blocks allow BOTS to augment the capacities of a chat message. At the moment only one block is available: `actions`, accepting only one type of element: `button`.

<img width="708" alt="Screenshot 2024-11-15 at 19 14 02" src="https://github.com/user-attachments/assets/63f32a29-05b1-4f32-9edd-8d8e1007d705">

# Usage

```ruby
Chat::CreateMessage.call(
  params: {
    message: "Welcome!",
    chat_channel_id: 2,
    blocks: [
      {
         type: "actions",
         elements: [
           { value: "foo", type: "button", text: { text: "How can I install themes?", type: "plain_text" } }
         ]
      }
    ]
  },
  guardian: Discourse.system_user.guardian
)
```

# Documentation

## Blocks

### Actions

Holds interactive elements: button.

#### Fields

| Field | Type | Description | Required? |
|--------|--------|--------|--------|
| type | string | For an actions block, type is always `actions` | Yes |
| elements | array | An array of interactive elements, maximum 10 elements | Yes |
| block_id | string | An unique identifier for the block, will be generated if not specified. It has to be unique per message | No |

#### Example

```json
{
  "type": "actions",
  "block_id": "actions_1",
  "elements": [...]
}
```

## Elements

### Button

#### Fields

| Field | Type | Description | Required? |
|--------|--------|--------|--------|
| type | string | For a button, type is always `button` | Yes |
| text | object | A text object holding the type and text. Max 75 characters | Yes |
| value | string | The value returned after the interaction has been validated. Maximum length is 2000 characters | No |
| style | string | Can be `primary` ,  `success` or `danger` | No |
| action_id | string | An unique identifier for the action, will be generated if not specified. It has to be unique per message | No |

#### Example

```json
{
  "type": "actions",
  "block_id": "actions_1",
  "elements": [
    {
      "type": "button",
      "text": {
          "type": "plain_text",
          "text": "Ok"
      },
      "value": "ok",
      "action_id": "button_1"
    }
  ]
}
```

## Interactions

When a user interactions with a button the following flow will happen:

- We send an interaction request to the server
- Server checks if the user can make this interaction
- If the user can make this interaction, the server will:

  * `DiscourseEvent.trigger(:chat_message_interaction, interaction)`
  * return a JSON document
  
  ```json
  {
    "interaction": {
        "user": {
            "id": 1,
            "username": "j.jaffeux"
        },
        "channel": {
            "id": 1,
            "title": "Staff"
        },
        "message": {
            "id": 1,
            "text": "test",
            "user_id": -1
        },
        "action": {
            "text": {
                "text": "How to install themes?",
                "type": "plain_text"
            },
            "type": "button",
            "value": "click_me_123",
            "action_id": "bf4f30b9-de99-4959-b3f5-632a6a1add04"
        }
    }
  }
  ```
  * Fire a `appEvents.trigger("chat:message_interaction", interaction)`
2024-11-19 07:07:58 +01:00
..
all_mention.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
category_channel.rb PERF: auto join & leave chat channels (#29193) 2024-11-12 15:00:59 +11:00
channel_archive.rb DEV: add maxlength to additional chat text columns (#23505) 2023-09-12 14:52:50 +08:00
channel_custom_field.rb FEATURE: add custom fields to chat (channel/message/thread) (#29504) 2024-11-01 09:12:19 +11:00
channel.rb DEV: sets an icon_upload_id on a channel (#29566) 2024-11-04 17:19:44 +09:00
direct_message_channel.rb DEV: Extract leave logic to the Chat::Channel model 2024-10-17 14:58:45 +02:00
direct_message_user.rb
direct_message.rb UX: remove alias from chat direct message channel titles (#28958) 2024-09-18 22:18:46 +04:00
draft.rb
group_mention.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
here_mention.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
incoming_webhook.rb DEV: add maxlength to additional chat text columns (#23505) 2023-09-12 14:52:50 +08:00
mention_notification.rb DEV: Drop old notification id columns (#28550) 2024-10-15 11:58:57 +03:00
mention.rb DEV: Drop user_id from chat_mentions (#25022) 2024-04-11 18:50:29 +04:00
message_custom_field.rb FEATURE: add custom fields to chat (channel/message/thread) (#29504) 2024-11-01 09:12:19 +11:00
message_interaction.rb DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
message_reaction.rb DEV: add maxlength to additional chat text columns (#23505) 2023-09-12 14:52:50 +08:00
message_revision.rb DEV: add maxlength limits to chat messages and revisions (#23530) 2023-09-12 18:02:04 +08:00
message.rb DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
null_message.rb FIX: build chat message excerpt for thread preview (#26765) 2024-04-26 14:29:35 +08:00
null_user.rb FIX: ensures an empty last message won't cause errors (#23647) 2023-09-25 12:43:04 +02:00
reviewable_message.rb DEV: remap all core icons for fontawesome 6 upgrade (#28715) 2024-09-13 16:50:52 +01:00
separate_sidebar_mode_site_setting.rb FEATURE: implements user based sidebar mode (#23078) 2023-08-18 20:33:07 +02:00
thread_custom_field.rb FEATURE: add custom fields to chat (channel/message/thread) (#29504) 2024-11-01 09:12:19 +11:00
thread.rb FEATURE: add custom fields to chat (channel/message/thread) (#29504) 2024-11-01 09:12:19 +11:00
threads_view.rb UI: refines thread list item (#23207) 2023-08-24 18:45:20 +02:00
tracking_state_report.rb FEATURE: Add ability to watch chat threads (#28639) 2024-09-02 16:45:55 +04:00
user_chat_channel_membership.rb DEV: consolidate chat channel notification settings (#29080) 2024-10-08 13:13:01 +04:00
user_chat_thread_membership.rb FEATURE: encourage users to set chat thread titles (#26617) 2024-04-29 17:20:01 +08:00
user_mention.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
webhook_event.rb