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
70 changes: 3 additions & 67 deletions commands/ExampleCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import {
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 { sendDirect } from "../lib/sendDirectMessage";
export class ExampleCommand implements ISlashCommand {

public command = "example"; // here is where you define the command name,
Expand Down Expand Up @@ -73,7 +71,8 @@ export class ExampleCommand implements ISlashCommand {
case "direct": // If Direct, send a direct message
message =
"I am a **DIRECT Message**. I was created for you, and only you! :heart_decoration:";
await this.sendDirect(context, read, modify, message);
await sendDirect(context,read,modify,message,this.app)

break;

case "h":
Expand All @@ -88,67 +87,4 @@ export class ExampleCommand implements ISlashCommand {
}
}
}

private async getOrCreateDirectRoom(
read: IRead,
modify: IModify,
usernames: Array<string>,
creator?: IUser
) {
let room;
// first, let's try to get the direct room for given usernames
try {
room = await read.getRoomReader().getDirectByUsernames(usernames);
} catch (error) {
this.app.getLogger().log(error);
return;
}
// nice, room exist already, lets return it.
if (room) {
return room;
} else {
// no room for the given users. Lets create a room now!
// for flexibility, we might allow different creators
// if no creator, use app user bot
if (!creator) {
creator = await read.getUserReader().getAppUser();
if (!creator) {
throw new Error("Error while getting AppUser");
}
}

let roomId: string;
// Create direct room
const newRoom = modify
.getCreator()
.startRoom()
.setType(RoomType.DIRECT_MESSAGE)
.setCreator(creator)
.setMembersToBeAddedByUsernames(usernames);
roomId = await modify.getCreator().finish(newRoom);
return await read.getRoomReader().getById(roomId);
}
}

private async sendDirect(
context: SlashCommandContext,
read: IRead,
modify: IModify,
message: string
): Promise<void> {
const messageStructure = modify.getCreator().startMessage();
const sender = context.getSender(); // get the sender from context
// get the appUser username
const appUser = await read.getUserReader().getAppUser();
if (!appUser) {
throw new Error("Something went wrong getting App User!");
}
// lets use a function we created to get or create direct room
let room = (await this.getOrCreateDirectRoom(read, modify, [
sender.username,
appUser.username,
])) as IRoom;
messageStructure.setRoom(room).setText(message); // set the text message
await modify.getCreator().finish(messageStructure); // sends the message in the room.
}
}
69 changes: 69 additions & 0 deletions lib/sendDirectMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { IApp } from "@rocket.chat/apps-engine/definition/IApp";
import { IRead, IModify } from "@rocket.chat/apps-engine/definition/accessors";
import { IRoom, RoomType } from "@rocket.chat/apps-engine/definition/rooms";
import { SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashcommands";
import { IUser } from "@rocket.chat/apps-engine/definition/users";

async function getOrCreateDirectRoom(
app:IApp,
read: IRead,
modify: IModify,
usernames: Array<string>,
creator?: IUser
) {
let room: IRoom;
// first, let's try to get the direct room for given usernames
try {
room = await read.getRoomReader().getDirectByUsernames(usernames);
} catch (error) {
app.getLogger().log(error);
return;
}
// nice, room exist already, lets return it.
if (room) {
return room;
} else {
// no room for the given users. Lets create a room now!
// for flexibility, we might allow different creators
// if no creator, use app user bot
if (!creator) {
creator = await read.getUserReader().getAppUser();
if (!creator) {
throw new Error("Error while getting AppUser");
}
}
let roomId: string;
// Create direct room
const newRoom = modify
.getCreator()
.startRoom()
.setType(RoomType.DIRECT_MESSAGE)
.setCreator(creator)
.setMembersToBeAddedByUsernames(usernames);
roomId = await modify.getCreator().finish(newRoom);
return await read.getRoomReader().getById(roomId);
}
}

export async function sendDirect(
context: SlashCommandContext,
read: IRead,
modify: IModify,
message: string,
app:IApp
): Promise<void> {
const messageStructure = modify.getCreator().startMessage();
const sender = context.getSender(); // get the sender from context
// get the appUser username
const appUser = await read.getUserReader().getAppUser();
if (!appUser) {
throw new Error("Something went wrong getting App User!");
}
// lets use a function we created to get or create direct room
let room = (await getOrCreateDirectRoom(app,read, modify, [
sender.username,
appUser.username,
])) as IRoom;
messageStructure.setRoom(room).setText(message); // set the text message
await modify.getCreator().finish(messageStructure); // sends the message in the room.
}