-
Notifications
You must be signed in to change notification settings - Fork 19
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 all 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
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,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 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)}`, | ||
| }, | ||
| ]; | ||
| }; |
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,95 @@ | ||
| import { | ||
| RocketChatAssociationModel, | ||
| RocketChatAssociationRecord, | ||
| } from '@rocket.chat/apps-engine/definition/metadata'; | ||
| import { | ||
| IPersistence, | ||
| IPersistenceRead, | ||
| ILogger, | ||
| } from '@rocket.chat/apps-engine/definition/accessors'; | ||
| import { IUser } from '@rocket.chat/apps-engine/definition/users'; | ||
| import { ReplyStorage } from '../storage/ReplyStorage'; | ||
| import { getDefaultReplies } from '../data/DefaultReplies'; | ||
| import { Language } from '../lib/Translation/translation'; | ||
|
|
||
| export class UserInitStorage { | ||
| constructor( | ||
| private readonly persistence: IPersistence, | ||
| private readonly persistenceRead: IPersistenceRead, | ||
| private readonly logger: ILogger, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Check if a user has been initialized with default replies | ||
| */ | ||
| public async isUserInitialized(user: IUser): Promise<boolean> { | ||
| try { | ||
| const association = new RocketChatAssociationRecord( | ||
| RocketChatAssociationModel.USER, | ||
| `${user.id}#initialized_replies`, | ||
| ); | ||
| const result = await this.persistenceRead.readByAssociation(association); | ||
|
|
||
| return result && result.length > 0; | ||
| } catch (error) { | ||
| this.logger.error(`Error checking initialization status: ${error}`); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Mark a user as initialized in persistent storage | ||
| */ | ||
| public async markUserAsInitialized(user: IUser): Promise<void> { | ||
| try { | ||
| const association = new RocketChatAssociationRecord( | ||
| RocketChatAssociationModel.USER, | ||
| `${user.id}#initialized_replies`, | ||
| ); | ||
| await this.persistence.updateByAssociation( | ||
| association, | ||
| { initialized: true, timestamp: new Date().toISOString() }, | ||
| true | ||
| ); | ||
| this.logger.debug(`User ${user.id} marked as initialized in persistence`); | ||
| } catch (error) { | ||
| this.logger.error(`Error marking user as initialized: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initialize default quick replies for a user who hasn't used the app before | ||
| */ | ||
| public async initializeDefaultRepliesForUser(user: IUser): Promise<void> { | ||
| try { | ||
| // Check if the user has already been initialized | ||
| if (await this.isUserInitialized(user)) { | ||
| this.logger.debug(`User ${user.id} already initialized, skipping`); | ||
| return; | ||
| } | ||
|
|
||
| const replyStorage = new ReplyStorage(this.persistence, this.persistenceRead); | ||
| 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.logger.info(`Initialized default quick replies for user: ${user.id}`); | ||
| } | ||
|
|
||
| await this.markUserAsInitialized(user); | ||
| } catch (error) { | ||
| this.logger.error(`Error initializing default replies for user: ${error}`); | ||
| } | ||
| } | ||
| } |
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.
To much login in the root of the app can we define thing outside and use here ?