Text input block ui kit

Description

the problem is the following. I need the
addInputBlock I made, where the user can enter the data needed for the comment, and pass it to the request. I already have the working code of my application. But I can’t get the value of addInputBlock actionId: “commentAction”. Here’s how the logic for clicking buttons is done now
if (data.value === “complete”) {
responseText = “Done”;
} else {
const apiKey = “”; // Replace with the created key for the bot
const url = http://url:3000/issues/${issueId}.json;

await http.put(url, {
headers: {
“Content-Type”: “application/json”,
“X-Redmine-API-Key”: apiKey,
},
data: {
issue: {
status_id: 8,
notes: comment ?? “No comment” // Using the null coalescing operator
},
},
});
}
But it seems to me that I’m going in the wrong direction. Please help. Maybe someone can show me an example of how it works on a small application (one button, a comment field. And when the button is pressed, the test message should be sent to the chat). I’ve been struggling with this for several days now. And it seems to me that there is simply no way to make it work

Server Setup Information

Version of Rocket.Chat Server: 6.13.0
Operating System: linux
Deployment Method:
NodeJS Version: v14.21.2
MongoDB Version: 5.0.15

Any additional Information

I thought it would be nice to post the test code I wrote to make it easier to understand what I need)
testApp.ts file
import {
IAppAccessors,
IConfigurationExtend,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from “@rocket.chat/apps-engine/definition/accessors”;
import { App } from “@rocket.chat/apps-engine/definition/App”;
import { IAppInfo } from “@rocket.chat/apps-engine/definition/metadata”;
import {
IUIKitResponse,
UIKitBlockInteractionContext,
} from “@rocket.chat/apps-engine/definition/uikit”;
import { IMessage, IPostMessageSent } from “@rocket.chat/apps-engine/definition/messages”;
import { initiatorMessage } from “./lib/initiatorMessage”;

export class MemeBuddyApp extends App implements IPostMessageSent { private appInfo: IAppInfo;

constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { super(info, logger, accessors);
this.appInfo = info; // Save app info
}

public async executePostMessageSent(
message: IMessage,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise {
// Check that the message is not from LiveChat and not from the bot itself
if (message.room.type === “l” || message.sender.id === this.appInfo.id) {
return;
}

// Check that the message text is defined
if (!message.text) {
console.warn(“The message does not contain text.”);
return; // If there is no text, exit the function
}

// React to the message “hello”
if (message.text.toLowerCase() === “hello”) {
const data = {
room: message.room,
messageText: message.text
};
await initiatorMessage({ data, read, persistence, modify, http });
}
}
public async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
const data = context.getInteractionData();
const { actionId } = data;

try {
const { room } = context.getInteractionData();
if (!room) {
console.error(“Room is undefined”);
return { success: false };
}

// Handle the “test comment” button click
if (actionId === “test_comment”) {
const comment = data.value; // Get the comment from the input field
const mess = await modify.getCreator().startMessage().setRoom(room)
.setText(Comment from the user: ${JSON.stringify(data)})
await modify.getCreator().finish(mess);

return { success: true };
}

return { success: false };
} catch (err) {
console.error(err);
return { success: false };
}
}

protected async extendConfiguration(
configuration: IConfigurationExtend
): Promise {
// Additional application settings can be added here
}
}

file lib/initiatorMessage.ts
import {
IHttp,
IModify,
IPersistence,
IRead,
} from “@rocket.chat/apps-engine/definition/accessors”;
import { ButtonStyle } from “@rocket.chat/apps-engine/definition/uikit”;

export async function initiatorMessage({
data,
read,
persistence,
modify,
http,
}: {
data;
read: IRead;
persistence: IPersistence;
modify: IModify;
http: IHttp;
}) {
const builder = await modify.getCreator().startMessage().setRoom(data.room);
const block = modify.getCreator().getBlockBuilder();
const test = document.getElementByiD(‘commentAction’) as HTMLInputElement ?? null;
// Check that the message is “hello”
if (data.messageText.toLowerCase() === “hello”) {
// Add the “test comment” button
block.addActionsBlock({
blockId: “commentButtonBlock”,
elements: [
block.newButtonElement({
actionId: “test_comment”,
text: block.newPlainTextObject(“test comment”),
value: “test_comment_value”,
style: ButtonStyle.PRIMARY,
}),
],
});

// Add a comment input field
block.addInputBlock({
blockId: “commentInput”,
element: block.newPlainTextInputElement({
actionId: “commentAction”,
placeholder: block.newPlainTextObject(“Comment input field”),
}),
label: block.newPlainTextObject(“Comment input field”),
});

builder.setBlocks(block);

// Send a message with a button and an input field to the general chat
await modify.getCreator().finish(builder);
}
}

Actually as you didn’t format it, it is hard to read.

For this much code use a gist or pastebin as it makes the post illegible.

testApp.ts file

import { IAppAccessors, IConfigurationExtend, IHttp, ILogger, IModify, IPersistence, IRead, } from "@rocket.chat/apps-engine/definition/accessors";
import { App } from "@rocket.chat/apps-engine/definition/App";
import { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata";
import { IUIKitResponse, UIKitBlockInteractionContext, } from "@rocket.chat/apps-engine/definition/uikit";
import { IMessage, IPostMessageSent } from "@rocket.chat/apps-engine/definition/messages";
import { initiatorMessage } from "./lib/initiatorMessage";

export class MemeBuddyApp extends App implements IPostMessageSent {
private appInfo: IAppInfo;

constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
this.appInfo = info; // Save app info
} public async executePostMessageSent(
message: IMessage,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<void> {
// Check that the message is not from LiveChat and not from the bot itself
if (message.room. type === "l" || message.sender.id === this.appInfo.id) {
return;
}

// Check that the message text is defined
if (!message.text) {
console.warn("The message does not contain text.");
return; // If there is no text, exit the function
}

// React to the message "hello" if (message.text.toLowerCase() === "hello") {
const data = {
room: message.room,
messageText: message.text
};
await initiatorMessage({ data, read, persistence, modify, http }); }
}
public async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
const data = context.getInteractionData();
const { actionId } = data;

try {
const { room } = context.getInteractionData();
if (!room) {
console.error("Room is undefined");
return { success: false };
}

// Handle the click of the "test comment" button
if (actionId === "test_comment") {
const comment = data.value; // Get the comment from the input field
const mess = await modify.getCreator().startMessage().setRoom(room)
.setText(`Comment from user: ${ JSON.stringify(data)}`)
await modify.getCreator().finish(mess);

return { success: true };
}

return { success: false };
} catch (err) {
console.error(err);
return { success: false };
}
}

protected async extendConfiguration(
configuration: IConfigurationExtend
): Promise<void> {
// Additional application settings can be added here
}
}

initiatorMessage.ts

import {
 IHttp
 IModify
 IPersistence,
 IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { ButtonStyle } from "@rocket.chat/apps-engine/definition/uikit";

export async function initiatorMessage({
 data,
 read
 persistence
 modify
 http,
}: {
 data;
 read: IRead;
 persistence: IPpersistence;
 modify: IModify;
 http:IHttp;
}) {
 const builder = await modify.getCreator().startMessage().setRoom(data.room);
 const block = modify.getCreator().getBlockBuilder();
 const test = document.getElementByiD('commentAction') as HTMLInputElement ?? null;
 // Check that the message is "hello"
if (data.messageText.toLowerCase() === "hello") {
// Add the "test comment" button
block.addActionsBlock({
blockId: "commentButtonBlock",
elements: [
block .newButtonElement({
actionId: "test_comment",
text: block.newPlainTextObject("test comment"),
value: "test_comment_value",
style: ButtonStyle.PRIMARY,
}),
],
});

// Add a field for entering a comment
block.addInputBlock({
blockId: "commentInput",
element: block.newPlainTextInputElement({
actionId: "commentAction",
placeholder: block.newPlainTextObject("Comment field"),
}),
label: block .newPlainTextObject("Comment field"),
});

builder.setBlocks(block);

// Send a message with a button and an input field to the general chat
await modify.getCreator().finish(builder);
}
}

for testing it is enough to create a test application. Copy the code and apply it. If suddenly it will be possible to transfer a message from a text field somewhere. I want to know how to do this and where exactly I was mistaken in my reasoning, that I could not do this

I made the code as it should be. Now it can be copied into the test application. And I also attached a screenshot of it working. But not quite as I need

Could you please at least give an example of a small application that could, at the click of a button, display the contents of addInputBlock in the chat, just to see how this can be done in principle. This is very important to me

Stick to one place,. You are going to get ignored.

This might be urgent for you but not for anyone else.

Read this.

If you think your issue is urgent, then you have two choices.

a) Pay for support - [Pricing](https://rocket.chat/pricing)

Email: [support@rocket.chat](mailto:support@rocket.chat)

b) Patiently wait for a volunteer to consider helping you.

**You are entitled to exactly what you paid for**

No more, and no less.