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);
}
}