Skip to content

Commit

Permalink
feat: qr command
Browse files Browse the repository at this point in the history
  • Loading branch information
VipinDevelops committed Sep 9, 2024
1 parent d6901e7 commit ccaf8b0
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 113 deletions.
4 changes: 4 additions & 0 deletions QuickRepliesApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import { ActionButton } from './src/enum/modals/common/ActionButtons';
import { ExecuteActionButtonHandler } from './src/handlers/ExecuteActionButtonHandler';
import { settings } from './src/config/settings';
import { QrCommand } from './src/commands/QrCommand';

export class QuickRepliesApp extends App {
private elementBuilder: ElementBuilder;
Expand All @@ -49,6 +50,9 @@ export class QuickRepliesApp extends App {
await configuration.slashCommands.provideSlashCommand(
new QsCommand(this),
);
await configuration.slashCommands.provideSlashCommand(
new QrCommand(this),
);
this.elementBuilder = new ElementBuilder(this.getID());
this.blockBuilder = new BlockBuilder(this.getID());

Expand Down
106 changes: 53 additions & 53 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
{
"id": "e664d2cb-7beb-413a-837a-80fd840c387b",
"version": "0.0.1",
"requiredApiVersion": "1.44.0",
"iconFile": "icon.png",
"author": {
"name": "Vipin Chaudhary",
"homepage": "https://github.com/RocketChat/Apps.QuickReplies",
"support": "https://github.com/RocketChat/Apps.QuickReplies/issues"
},
"name": "QuickReplies",
"nameSlug": "quickreplies",
"classFile": "QuickRepliesApp.ts",
"description": "Instantly craft and send customizable responses within Rocket.Chat.",
"implements": [],
"permissions": [
{
"name": "ui.registerButtons"
},
{
"name": "api"
},
{
"name": "slashcommand"
},
{
"name": "server-setting.read"
},
{
"name": "room.read"
},
{
"name": "persistence"
},
{
"name": "ui.interact"
},
{
"name": "networking"
},
{
"name": "message.write"
},
{
"name": "user.read"
},
{
"name": "room.write"
},
{
"name": "message.read"
}
]
}
"id": "e664d2cb-7beb-413a-837a-80fd840c387b",
"version": "0.0.1",
"requiredApiVersion": "1.44.0",
"iconFile": "icon.png",
"author": {
"name": "Vipin Chaudhary",
"homepage": "https://github.com/RocketChat/Apps.QuickReplies",
"support": "https://github.com/RocketChat/Apps.QuickReplies/issues"
},
"name": "QuickReplies",
"nameSlug": "quickreplies",
"classFile": "QuickRepliesApp.ts",
"description": "Instantly craft and send customizable responses within Rocket.Chat.",
"implements": [],
"permissions": [
{
"name": "ui.registerButtons"
},
{
"name": "api"
},
{
"name": "slashcommand"
},
{
"name": "server-setting.read"
},
{
"name": "room.read"
},
{
"name": "persistence"
},
{
"name": "ui.interact"
},
{
"name": "networking"
},
{
"name": "message.write"
},
{
"name": "user.read"
},
{
"name": "room.write"
},
{
"name": "message.read"
}
]
}
3 changes: 3 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"Quick_Search_Command_Params": "<replyname>",
"Quick_Search_Command_Description": "Search Quick Replies to send. Type /qs <replyname>.",
"Quick_Search_Command_Preview_Title": "Quick Search Command Preview",
"Quick_Response_Command_Parameters": "qr params",
"Quick_Response_Command_Description": "Generate a response for last message using AI.",
"Quick_Response_Command_Preview_Title": "Quick Response Command Preview",
"Create_Quick_Reply_Action_Button_Label": "Create a new Quick Reply",
"List_Quick_Reply_Action_Button_Label": "List all Quick Reply",
"Reply_Using_AI_Label": "🤖 Reply using AI",
Expand Down
108 changes: 108 additions & 0 deletions src/commands/QrCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
ISlashCommand,
ISlashCommandPreview,
ISlashCommandPreviewItem,
SlashCommandContext,
SlashCommandPreviewItemType,
} from '@rocket.chat/apps-engine/definition/slashcommands';
import { QuickRepliesApp } from '../../QuickRepliesApp';
import {
IRead,
IModify,
IHttp,
IPersistence,
} from '@rocket.chat/apps-engine/definition/accessors';
import { getSortedMessages, sendMessage } from '../helper/message';
import AIHandler from '../handlers/AIHandler';
import { UserPreferenceStorage } from '../storage/userPreferenceStorage';
import { sendNotification } from '../helper/notification';

export class QrCommand implements ISlashCommand {
constructor(private readonly app: QuickRepliesApp) {}

public command = 'qr';
public i18nDescription = 'Quick_Response_Command_Description';
public providesPreview = true;
public i18nParamsExample = 'Quick_Response_Command_Params';

public async executor(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
): Promise<void> {
// Placeholder for command execution logic
}

public async previewer(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
): Promise<ISlashCommandPreview> {
const sender = context.getSender();
const room = context.getRoom();
const prevMessages = await getSortedMessages(room.id, read);
const lastMessage = prevMessages[0];

const userPreference = new UserPreferenceStorage(
persis,
read.getPersistenceReader(),
sender.id,
);

const Preference = await userPreference.getUserPreference();
const AiHandler = new AIHandler(this.app, http, Preference);

let items = [] as ISlashCommandPreviewItem[];
if (lastMessage.text && lastMessage.sender._id != sender.id) {
const data = await AiHandler.handleResponse(
lastMessage?.text,
'',
true,
);

if (data.success) {
const str = data.response;
const arr = JSON.parse(str);
items = arr.map((message, index) => ({
id: (index + 1).toString(),
type: SlashCommandPreviewItemType.TEXT,
value: message,
})) as ISlashCommandPreviewItem[];

return {
i18nTitle: 'Quick_Response_Command_Preview_Title',
items,
};
} else {
await sendNotification(read, modify, sender, room, {
message: data.response,
});
items = [];
return {
i18nTitle: 'Quick_Response_Command_Preview_Title',
items,
};
}
} else {
items = [];
return { i18nTitle: 'Quick_Response_Command_Preview_Title', items };
}
}

public async executePreviewItem(
item: ISlashCommandPreviewItem,
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
): Promise<void> {
const room = context.getRoom();
const message = item.value;
await sendMessage(modify, context.getSender(), room, message);
}
}
Loading

0 comments on commit ccaf8b0

Please sign in to comment.