discourse/plugins/chat/app/controllers/chat/api
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
..
category_chatables_controller.rb DEV: Update rubocop-discourse to latest version 2024-03-04 15:08:35 +01:00
channel_messages_controller.rb DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
channel_thread_messages_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
channel_threads_controller.rb DEV: Refactor the Chat::CreateThread service a bit 2024-11-06 15:53:43 +01:00
channel_threads_current_user_notifications_settings_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
channel_threads_current_user_title_prompt_seen_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
channels_archives_controller.rb
channels_controller.rb DEV: Provide user input to services using params key 2024-10-25 09:57:59 +02:00
channels_current_user_membership_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_current_user_membership_follows_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
channels_current_user_notifications_settings_controller.rb DEV: consolidate chat channel notification settings (#29080) 2024-10-08 13:13:01 +04:00
channels_drafts_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_invites_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_memberships_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_messages_flags_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_messages_interactions_controller.rb DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
channels_messages_moves_controller.rb
channels_messages_streaming_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_read_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
channels_status_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
channels_threads_drafts_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
channels_threads_read_controller.rb DEV: Make params explicit for services in controllers 2024-10-03 16:56:39 +09:00
chatables_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
current_user_channels_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
current_user_threads_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
direct_messages_controller.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
hints_controller.rb DEV: Update rubocop-discourse to latest version 2024-03-04 15:08:35 +01:00