-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Default replies for new app users #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
priyanshuharshbodhi1
wants to merge
3
commits into
RocketChat:main
Choose a base branch
from
priyanshuharshbodhi1:feat-Default-famous-replies-new
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 +1,6 @@ | ||
| import { | ||
| IAppAccessors, | ||
| IAppInstallationContext, | ||
| IConfigurationExtend, | ||
| IEnvironmentRead, | ||
| IHttp, | ||
|
|
@@ -9,7 +10,7 @@ import { | |
| 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 { IAppInfo, RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata'; | ||
| import { QuickCommand } from './src/commands/QuickCommand'; | ||
| import { | ||
| IUIKitResponse, | ||
|
|
@@ -32,10 +33,15 @@ import { | |
| import { ActionButton } from './src/enum/modals/common/ActionButtons'; | ||
| import { ExecuteActionButtonHandler } from './src/handlers/ExecuteActionButtonHandler'; | ||
| import { settings } from './src/config/settings'; | ||
| import { ReplyStorage } from './src/storage/ReplyStorage'; | ||
| import { getDefaultReplies } from './src/data/DefaultReplies'; | ||
| import { IUser } from '@rocket.chat/apps-engine/definition/users'; | ||
| import { Language } from './src/lib/Translation/translation'; | ||
|
|
||
| export class QuickRepliesApp extends App { | ||
| private elementBuilder: ElementBuilder; | ||
| private blockBuilder: BlockBuilder; | ||
|
|
||
| constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { | ||
| super(info, logger, accessors); | ||
| } | ||
|
|
@@ -91,13 +97,119 @@ export class QuickRepliesApp extends App { | |
| blockBuilder: this.blockBuilder, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get the association records for tracking user initialization status | ||
| */ | ||
| private getInitAssociations(userId: string): RocketChatAssociationRecord[] { | ||
| return [ | ||
| new RocketChatAssociationRecord( | ||
| RocketChatAssociationModel.USER, | ||
| userId, | ||
| ), | ||
| new RocketChatAssociationRecord( | ||
| RocketChatAssociationModel.MISC, | ||
| 'initialized_replies' | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a user has been initialized with default replies | ||
| */ | ||
| private async isUserInitialized(user: IUser, read: IRead): Promise<boolean> { | ||
| try { | ||
| const association = this.getInitAssociations(user.id); | ||
| const result = await read.getPersistenceReader().readByAssociations(association); | ||
| return result && result.length > 0; | ||
| } catch (error) { | ||
| this.getLogger().error(`Error checking initialization status: ${error}`); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Mark a user as initialized in persistent storage | ||
| */ | ||
| private async markUserAsInitialized(user: IUser, persistence: IPersistence): Promise<void> { | ||
| try { | ||
| const association = this.getInitAssociations(user.id); | ||
| await persistence.updateByAssociations( | ||
| association, | ||
| { initialized: true, timestamp: new Date().toISOString() }, | ||
| true | ||
| ); | ||
| this.getLogger().debug(`User ${user.id} marked as initialized in persistence`); | ||
| } catch (error) { | ||
| this.getLogger().error(`Error marking user as initialized: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initialize default quick replies for a user who hasn't used the app before | ||
|
|
||
| */ | ||
| public async initializeDefaultRepliesForUser( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To much login in the root of the app can we define thing outside and use here ? |
||
| user: IUser, | ||
| read: IRead, | ||
| persistence: IPersistence | ||
| ): Promise<void> { | ||
| try { | ||
| // Check if the user has already been initialized using persistent storage | ||
| if (await this.isUserInitialized(user, read)) { | ||
| this.getLogger().debug(`User ${user.id} already initialized, skipping`); | ||
| return; | ||
| } | ||
|
|
||
| const replyStorage = new ReplyStorage(persistence, read.getPersistenceReader()); | ||
| const existingReplies = await replyStorage.getReplyForUser(user); | ||
|
|
||
| // Only initialize if the user doesn't have any replies yet | ||
| if (existingReplies.length === 0) { | ||
| const defaultReplies = getDefaultReplies(user.id); | ||
|
|
||
| for (const reply of defaultReplies) { | ||
| await replyStorage.createReply( | ||
| user, | ||
| reply.name, | ||
| reply.body, | ||
| Language.en | ||
| ); | ||
| } | ||
|
|
||
| this.getLogger().info(`Initialized default quick replies for user: ${user.id}`); | ||
| } | ||
|
|
||
| await this.markUserAsInitialized(user, persistence); | ||
| } catch (error) { | ||
| this.getLogger().error(`Error initializing default replies for user: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| public async onInstall( | ||
| context: IAppInstallationContext, | ||
| read: IRead, | ||
| http: IHttp, | ||
| persistence: IPersistence, | ||
| modify: IModify | ||
| ): Promise<void> { | ||
| try { | ||
| // Initialize for the admin/installer user | ||
| await this.initializeDefaultRepliesForUser(context.user, read, persistence); | ||
| this.getLogger().info('Successfully initialized default replies for admin during installation'); | ||
| } catch (error) { | ||
| this.getLogger().error(`Error in onInstall: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| public async executeViewSubmitHandler( | ||
| context: UIKitViewSubmitInteractionContext, | ||
| read: IRead, | ||
| http: IHttp, | ||
| persistence: IPersistence, | ||
| modify: IModify, | ||
| ) { | ||
|
|
||
| const handler = new ExecuteViewSubmitHandler( | ||
| this, | ||
| read, | ||
|
|
@@ -109,6 +221,7 @@ export class QuickRepliesApp extends App { | |
|
|
||
| return await handler.handleActions(); | ||
| } | ||
|
|
||
| public async executeViewClosedHandler( | ||
| context: UIKitViewCloseInteractionContext, | ||
| read: IRead, | ||
|
|
@@ -135,6 +248,9 @@ export class QuickRepliesApp extends App { | |
| persistence: IPersistence, | ||
| modify: IModify, | ||
| ): Promise<IUIKitResponse> { | ||
| // Check and initialize default replies for the user | ||
| await this.initializeDefaultRepliesForUser(context.getInteractionData().user, read, persistence); | ||
|
|
||
| const handler = new ExecuteBlockActionHandler( | ||
| this, | ||
| read, | ||
|
|
@@ -154,6 +270,9 @@ export class QuickRepliesApp extends App { | |
| persistence: IPersistence, | ||
| modify: IModify, | ||
| ): Promise<IUIKitResponse> { | ||
| // Check and initialize default replies for the user | ||
| await this.initializeDefaultRepliesForUser(context.getInteractionData().user, read, persistence); | ||
|
|
||
| const handler = new ExecuteActionButtonHandler( | ||
| this, | ||
| read, | ||
|
|
||
This file contains hidden or 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,34 @@ | ||
| import { IReply } from '../definition/reply/IReply'; | ||
|
|
||
| /** | ||
| * Collection of pre-built default quick replies that will be added for new users | ||
| */ | ||
| export const getDefaultReplies = (userId: string): IReply[] => { | ||
| return [ | ||
| { | ||
| name: 'Greeting', | ||
| body: 'Hello! How may I assist you today?', | ||
| id: `${userId}-${(Date.now() - 10).toString(36)}`, | ||
| }, | ||
| { | ||
| name: 'Acknowledgment', | ||
| body: 'Thank you for reaching out. I will get back to you shortly.', | ||
| id: `${userId}-${(Date.now() - 5).toString(36)}`, | ||
| }, | ||
| { | ||
| name: 'Follow-up', | ||
| body: 'I wanted to follow up on our previous discussion. Please let me know how you\'d like to proceed.', | ||
| id: `${userId}-${Date.now().toString(36)}`, | ||
| }, | ||
| { | ||
| name: 'Apology', | ||
| body: 'I sincerely apologize for any inconvenience. We are looking into this and will resolve it as soon as possible.', | ||
| id: `${userId}-${(Date.now() + 5).toString(36)}`, | ||
| }, | ||
| { | ||
| name: 'Closing', | ||
| body: 'It was a pleasure assisting you. Please feel free to reach out for any further queries.', | ||
| id: `${userId}-${(Date.now() + 10).toString(36)}`, | ||
| }, | ||
| ]; | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this methods ?