discourse/plugins/chat/lib/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
..
schemas DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
bookmark_extension.rb DEV: Refactor STI/polymorphic associations in chat (#20789) 2023-04-17 15:41:56 +02:00
category_extension.rb DEV: Refactor STI/polymorphic associations in chat (#20789) 2023-04-17 15:41:56 +02:00
channel_archive_service.rb FEATURE: Add threads support to chat archives (#24325) 2023-11-27 15:47:35 +01:00
channel_fetcher.rb DEV: sets an icon_upload_id on a channel (#29566) 2024-11-04 17:19:44 +09:00
channel_hashtag_data_source.rb FEATURE: Async load of category and chat hashtags (#25526) 2024-02-12 12:07:14 +02:00
channel_membership_manager.rb PERF: auto join & leave chat channels (#29193) 2024-11-12 15:00:59 +11:00
duplicate_message_validator.rb
engine.rb DEV: Update chat scheduled job loading to match skeleton (#26853) 2024-05-02 19:20:00 +01:00
group_extension.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
guardian_extensions.rb FIX: trashed channel thread view bug (#29192) 2024-10-14 17:45:50 +04:00
mailer.rb FIX: chat mailer log noise (#28616) 2024-08-29 11:39:08 -03:00
message_blocks_validator.rb DEV: adds blocks support to chat messages (#29782) 2024-11-19 07:07:58 +01:00
message_bookmarkable.rb FIX: Show deleted bookmark reminders in user bookmarks menu (#25905) 2024-02-29 09:03:49 +10:00
message_mover.rb DEV: Provide user input to services using params key 2024-10-25 09:57:59 +02:00
message_processor.rb DEV: Rename with_secure_uploads? to should_secure_uploads? on Post (#26549) 2024-04-09 13:23:11 +10:00
message_rate_limiter.rb
message_reactor.rb
messages_exporter.rb FIX: Chat messages exporter (#25461) 2024-01-30 18:37:11 +04:00
notification_consolidation_extension.rb FEATURE: Add ability to watch chat threads (#28639) 2024-09-02 16:45:55 +04:00
notification_levels.rb FIX: Chat NotificationLevels extension breaking in prod (#21484) 2023-05-10 18:46:06 +02:00
notifier.rb FIX: delay chat notify watching job (#28386) 2024-08-15 18:08:30 +04:00
onebox_handler.rb DEV: Revert guardian changes (#24742) 2023-12-06 16:37:32 +10:00
outgoing_web_hook_extension.rb FEATURE: granular webhooks (#23070) 2023-10-09 03:35:31 +00:00
parsed_mentions.rb DEV: better highlighting of mentions (#28403) 2024-08-20 14:37:28 +02:00
plugin_instance_extension.rb
post_notification_handler.rb
review_queue.rb FIX: Create a reviewable when flagging a chat message for 'something else' (#23264) 2023-08-25 17:38:27 +08:00
reviewable_extension.rb DEV: Refactor STI/polymorphic associations in chat (#20789) 2023-04-17 15:41:56 +02:00
secure_uploads_compatibility.rb
seeder.rb PERF: auto join & leave chat channels (#29193) 2024-11-12 15:00:59 +11:00
slack_compatibility.rb
statistics.rb DEV: Add new chat metrics (#23872) 2024-01-04 16:10:03 +04:00
transcript_service.rb FIX: Do not display chat replies as threads in transcripts (#24768) 2023-12-15 09:50:43 -03:00
user_extension.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
user_notifications_extension.rb FIX: show group based notifications in chat summary email (#27641) 2024-07-01 12:47:38 +04:00
user_option_extension.rb FEATURE: encourage users to set chat thread titles (#26617) 2024-04-29 17:20:01 +08:00