Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions DemoApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ import { ExampleEndpoint } from "./api/ExampleEndPoint";
import { ApiWithPersistence } from "./api/PersistenceWithEndPoint";
import { ExampleCommand } from "./commands/ExampleCommand";
import { IncrementCommand } from "./commands/IncrementCommand";
import { ScheduleCommand } from "./commands/ScheduleCommand";
import { buttons } from "./config/Buttons";
import { settings } from "./config/Settings";
import { ExampleActionButtonHandler } from "./handlers/ActionButton";
import { ExampleViewSubmitHandler } from "./handlers/ViewSubmit";

export class DemoAppApp extends App {
constructor(
info: IAppInfo,
logger: ILogger,
accessors: IAppAccessors,
) {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}

Expand Down Expand Up @@ -63,12 +60,64 @@ export class DemoAppApp extends App {
new ExampleCommand(this)
);
await configuration.slashCommands.provideSlashCommand(
new IncrementCommand(this),
new IncrementCommand(this)
);
await configuration.slashCommands.provideSlashCommand(
new ScheduleCommand(this)
);
// Registering Action Buttons
await Promise.all(
buttons.map((button) => configuration.ui.registerButton(button))
);
// Registring schedluing processors
// This processor can be scheduled using the process id
await configuration.scheduler.registerProcessors([
{
id: "reminder",
processor: async (jobContext, read, modify, http, persis) => {
const block = modify.getCreator().getBlockBuilder();
const time = jobContext.time;
const text = jobContext.message;
const username = jobContext.username;
block.addSectionBlock({
text: block.newPlainTextObject(`@${username} You created a reminder to remind you at ${time}
*About*
${text}`),
});
const message = modify
.getCreator()
.startMessage()
.addBlocks(block.getBlocks())
.setRoom(
(await read
.getRoomReader()
.getById(jobContext.room))!
);
await modify.getCreator().finish(message);
},
},
{
id: "job",
processor: async (jobContext, read, modify, http, persis) => {
const block = modify.getCreator().getBlockBuilder();
block.addSectionBlock({
text: block.newPlainTextObject(`@${jobContext.username} You created a recurring reminder to remind you at an interval of ${jobContext.interval}
*About*
${jobContext.message}`),
});
const message = modify
.getCreator()
.startMessage()
.addBlocks(block.getBlocks())
.setRoom(
(await read
.getRoomReader()
.getById(jobContext.room))!
);
await modify.getCreator().finish(message);
},
},
]);
}

public async onSettingUpdated(
Expand All @@ -81,7 +130,10 @@ export class DemoAppApp extends App {
// this will show in ADMIN > APPS > INSTALLED APP > THIS APP > LOGS
// Log from inside the app
// note that you can pass both any or a list or any
let list_to_log = ["Some Setting was Updated. SUCCESS MESSAGE: ", setting]
let list_to_log = [
"Some Setting was Updated. SUCCESS MESSAGE: ",
setting,
];
// you can have a different type of logs:
this.getLogger().success(list_to_log);
this.getLogger().info(list_to_log);
Expand Down
8 changes: 7 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "67a4e34b-5c04-4c9c-9358-3d0fd217cefd",
"version": "0.0.1",
"requiredApiVersion": "^1.19.0",
"requiredApiVersion": "^1.36.0",
"iconFile": "icon.png",
"author": {
"name": "Duda Nogueira",
Expand Down Expand Up @@ -34,6 +34,12 @@
},
{
"name": "persistence"
},
{
"name": "ui.interact"
},
{
"name": "scheduler"
}
]
}
83 changes: 83 additions & 0 deletions commands/ScheduleCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
IHttp,
IModify,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { IRoom, RoomType } from "@rocket.chat/apps-engine/definition/rooms";
import {
ISlashCommand,
SlashCommandContext,
} from "@rocket.chat/apps-engine/definition/slashcommands";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { DemoAppApp } from "../DemoApp";
import { sendMessage } from "../lib/sendMessage";
import { sendNotification } from "../lib/sendNotification";
import { addJob } from "../modals/addJobModal";
import { addReminder } from "../modals/addReminderModal";

export class ScheduleCommand implements ISlashCommand {
public command = "schedule"; // here is where you define the command name,
// users will need to run /schedule to trigger this command
public i18nParamsExample = "ScheduleCommand_Params";
public i18nDescription = "ScheduleCommand_Description";
public providesPreview = false;

constructor(private readonly app: DemoAppApp) {}

public async executor(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp
): Promise<void> {
// lets log this slash command call
this.app
.getLogger()
.info(
`Slash Command /${
this.command
} initiated. Trigger id: ${context.getTriggerId()} with arguments ${context.getArguments()}`
);
// let's discover if we have a subcommand
const [subcommand] = context.getArguments();
const room = context.getRoom();
const sender = context.getSender();
read.getUserReader();
// lets define a default help message
const helpMessage = `
*You can schedule your tasks or reminders with this command*
Schedule a reminder -> \`/schedule [reminder|r]\`
Schedule a recurring reminer -> \`/schedule [job|j]\`
Helper message for this command -> \`/schedule [help|h]\``;
if (!subcommand) {
// no subcommand, let's just show that
var message = `No Subcommand :confounded:
${helpMessage}`;
await sendNotification(modify, room, sender, message);
} else {
switch (
subcommand // Try to match the argument in the list of allowed subcommands
) {
case "r":
case "reminder": // If Subcommand is reminder or r then
await addReminder(context, read, modify);
break;

case "j":
case "job": // If Subcommand is job or then
await addJob(context, read, modify);
break;

case "delete":
await modify.getScheduler().cancelAllJobs();
break;

case "h":
case "help":
default: // If subcommand is some giberish or help or h is the subcommand then send help message
await sendNotification(modify, room, sender, helpMessage);
break;
}
}
}
}
16 changes: 16 additions & 0 deletions enums/actionId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Enums help to reduce redundancy in the code, as they allow you to define a set of
related constantsin a single place and then reuse them throughout your code.
This makes your code more maintainable, as any changes to the set of allowed values
can be made in one place, rather than scattered throughout your codebase.*/

export enum actionId {
MESSAGE='message',
DATE='date',
INTERVAL='interval',
FORMAT='format',
TIME='time',
REMINDER_SUBMIT='reminder_submit',
JOB_SUBMIT='job-submit',
ROOM='room'
}
10 changes: 10 additions & 0 deletions enums/blockId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Enums help to reduce redundancy in the code, as they allow you to define a set of
related constantsin a single place and then reuse them throughout your code.
This makes your code more maintainable, as any changes to the set of allowed values
can be made in one place, rather than scattered throughout your codebase.
*/
export enum blockId {
REMINDER='reminder',
JOB='job'
}
10 changes: 10 additions & 0 deletions enums/viewId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Enums help to reduce redundancy in the code, as they allow you to define a set of
related constantsin a single place and then reuse them throughout your code.
This makes your code more maintainable, as any changes to the set of allowed values
can be made in one place, rather than scattered throughout your codebase.
*/
export enum viewId {
REMINDER='reminder',
JOB='job'
}
Loading