discourse/plugins/chat/spec/requests/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_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channel_messages_controller_spec.rb DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
channel_thread_messages_controller_spec.rb FIX: allows listing messages of any thread (#27259) 2024-05-30 10:20:40 +02:00
channel_threads_controller_spec.rb DEV: Refactor the Chat::CreateThread service a bit 2024-11-06 15:53:43 +01:00
channel_threads_current_user_notification_settings_controller_spec.rb FEATURE: encourage users to set chat thread titles (#26617) 2024-04-29 17:20:01 +08:00
channel_threads_current_user_title_prompt_seen_controller_spec.rb FEATURE: encourage users to set chat thread titles (#26617) 2024-04-29 17:20:01 +08:00
channels_archives_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_current_user_membership_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_current_user_membership_follows_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_current_user_notifications_settings_controller_spec.rb DEV: consolidate chat channel notification settings (#29080) 2024-10-08 13:13:01 +04:00
channels_drafts_controller_spec.rb FEATURE: implements drafts for threads (#24483) 2023-11-22 11:54:23 +01:00
channels_invites_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_memberships_controller_spec.rb FEATURE: introduces group channels (#24288) 2023-11-10 11:29:28 +01:00
channels_messages_flags_controller_spec.rb DEV: Don’t provide an array to site settings group lists in specs 2024-10-17 11:25:31 +02:00
channels_messages_streaming_controller_spec.rb FIX: allows bots to create/update/stream messages (#26900) 2024-05-07 15:17:42 +02:00
channels_moves_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_read_controller_spec.rb FEATURE: implements last read message for threads (#26702) 2024-04-25 10:47:54 +02:00
channels_status_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
channels_threads_drafts_controller_spec.rb FEATURE: implements drafts for threads (#24483) 2023-11-22 11:54:23 +01:00
channels_threads_read_controller_spec.rb FEATURE: implements last read message for threads (#26702) 2024-04-25 10:47:54 +02:00
chatables_controller_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
current_user_channels_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
current_user_threads_spec.rb DEV: Stop injecting a service result object in the caller object 2024-10-22 16:58:54 +02:00
direct_messages_controller_spec.rb FIX: load existing chat dm channel via url (#26998) 2024-05-24 12:12:49 +04:00
hints_controller_spec.rb DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
messages_controller_spec.rb FIX: chat replies are not always in a thread (#27023) 2024-05-16 16:10:23 +02:00