mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 13:51:18 +08:00
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)`
This commit is contained in:
@ -13,10 +13,124 @@ describe Chat::Message do
|
||||
expect(Chat::MessageCustomField.first.message.id).to eq(message.id)
|
||||
end
|
||||
|
||||
describe "normalization" do
|
||||
context "when normalizing blocks" do
|
||||
it "adds a schema version to the blocks" do
|
||||
message.update!(
|
||||
blocks: [
|
||||
{
|
||||
type: "actions",
|
||||
elements: [{ text: { text: "Foo", type: "plain_text" }, type: "button" }],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
expect(message.blocks[0]["schema_version"]).to eq(1)
|
||||
end
|
||||
|
||||
it "adds a schema version to the elements" do
|
||||
message.update!(
|
||||
blocks: [
|
||||
{
|
||||
type: "actions",
|
||||
elements: [{ text: { text: "Foo", type: "plain_text" }, type: "button" }],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
expect(message.blocks[0]["elements"][0]["schema_version"]).to eq(1)
|
||||
end
|
||||
|
||||
it "adds a block_id if not present" do
|
||||
message.update!(
|
||||
blocks: [
|
||||
{
|
||||
type: "actions",
|
||||
elements: [{ text: { text: "Foo", type: "plain_text" }, type: "button" }],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
expect(message.blocks[0]["block_id"]).to be_present
|
||||
end
|
||||
|
||||
it "adds an action_id if not present" do
|
||||
message.update!(
|
||||
blocks: [
|
||||
{
|
||||
type: "actions",
|
||||
elements: [{ text: { text: "Foo", type: "plain_text" }, type: "button" }],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
expect(message.blocks[0]["elements"][0]["action_id"]).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "validations" do
|
||||
subject(:message) { described_class.new(message: "") }
|
||||
|
||||
let(:blocks) { nil }
|
||||
|
||||
it { is_expected.to validate_length_of(:cooked).is_at_most(20_000) }
|
||||
|
||||
context "when blocks format is invalid" do
|
||||
let(:blocks) { [{ type: "actions", elements: [{ type: "buttoxn" }] }] }
|
||||
|
||||
it do
|
||||
is_expected.to_not allow_value(blocks).for(:blocks).with_message(
|
||||
[
|
||||
"value at `/0/elements/0/type` is not one of: [\"button\"]",
|
||||
"object at `/0/elements/0` is missing required properties: text",
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when action_id is duplicated" do
|
||||
let(:blocks) do
|
||||
[
|
||||
{
|
||||
type: "actions",
|
||||
elements: [
|
||||
{ type: "button", text: { text: "Foo", type: "plain_text" }, action_id: "foo" },
|
||||
{ type: "button", text: { text: "Foo", type: "plain_text" }, action_id: "foo" },
|
||||
],
|
||||
},
|
||||
]
|
||||
end
|
||||
|
||||
it do
|
||||
is_expected.to_not allow_value(blocks).for(:blocks).with_message(
|
||||
"have duplicated action_id: foo",
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when block_id is duplicated" do
|
||||
let(:blocks) do
|
||||
[
|
||||
{
|
||||
type: "actions",
|
||||
block_id: "foo",
|
||||
elements: [{ type: "button", text: { text: "Foo", type: "plain_text" } }],
|
||||
},
|
||||
{
|
||||
type: "actions",
|
||||
block_id: "foo",
|
||||
elements: [{ type: "button", text: { text: "Foo", type: "plain_text" } }],
|
||||
},
|
||||
]
|
||||
end
|
||||
|
||||
it do
|
||||
is_expected.to_not allow_value(blocks).for(:blocks).with_message(
|
||||
"have duplicated block_id: foo",
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".in_thread?" do
|
||||
|
Reference in New Issue
Block a user