-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7821486
commit dbbcc0c
Showing
8 changed files
with
263 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.