Skip to content

Commit

Permalink
Merge pull request #39 from beabee-communityrm/feat/start-with-callout
Browse files Browse the repository at this point in the history
feat(start-with-callout): Start directly with a callout
  • Loading branch information
JumpLink authored May 17, 2024
2 parents efa35d8 + 1e97efc commit a41a9bc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
6 changes: 3 additions & 3 deletions beabee-client/src/api/callout-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ export class CalloutClient extends BaseClient {

/**
* Get a callout
* @param slug The slug of the callout to get
* @param slugOrId The slug or id of the callout to get
* @param _with The relations to include
* @returns The callout
*/
async get<With extends GetCalloutWith = void>(
slug: string,
slugOrId: string,
_with?: readonly With[],
) {
const { data } = await this.fetch.get<Serial<GetCalloutDataWith<With>>>(
`/${slug}`,
`/${slugOrId}`,
{ with: _with },
);
return this.deserialize(data);
Expand Down
10 changes: 5 additions & 5 deletions telegram-bot/commands/show.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ export class ShowCommand extends BaseCommand {
}

// Handle the /show command
async action(ctx: AppContext) {
let successful = await this.checkAction(ctx);
if (!successful) {
async action(ctx: AppContext, forceSlug?: string) {
let successful = await this.checkAction(ctx, forceSlug !== undefined);
if (!forceSlug && !successful) {
return false;
}

// Get the slug from the `/show slug` message text
const slug = ctx.message?.text?.split(" ")[1]; // Alternatively, use ctx.match
const slug = forceSlug ?? ctx.match;

if (!slug) {
if (typeof slug !== "string") {
await ctx.reply("Please specify a callout slug. E.g. `/show my-callout`");
successful = false;
return successful;
Expand Down
19 changes: 16 additions & 3 deletions telegram-bot/commands/start.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { CommunicationService } from "../services/communication.service.ts";
import { StateMachineService } from "../services/state-machine.service.ts";
import { MessageRenderer } from "../renderer/message.renderer.ts";
import { ChatState } from "../enums/index.ts";
import { ListCommand, ResetCommand } from "./index.ts";
import { ListCommand, ResetCommand, ShowCommand } from "./index.ts";
import { START_CALLOUT_PREFIX } from "../constants/index.ts";

import type { AppContext } from "../types/index.ts";

Expand All @@ -25,6 +26,7 @@ export class StartCommand extends BaseCommand {
protected readonly stateMachine: StateMachineService,
protected readonly listCommand: ListCommand,
protected readonly resetCommand: ResetCommand,
protected readonly showCommand: ShowCommand,
) {
super();
}
Expand All @@ -34,9 +36,20 @@ export class StartCommand extends BaseCommand {
const session = await ctx.session;

try {
const payload = ctx.match;

// If the payload is a callout slug, show the callout
// We check if the payload starts with "c_" to indicate that the payload is a callout slug
if (
typeof payload === "string" && payload.startsWith(START_CALLOUT_PREFIX)
) {
const slug = payload.substring(START_CALLOUT_PREFIX.length);
return await this.showCommand.action(ctx, slug);
}

// Always allow the start command, automatically reset the session if it is not on the initial state
const startCanUsed = await this.checkAction(ctx, true);
if (!startCanUsed) {
const startAlreadyUsed = await this.checkAction(ctx, true);
if (!startAlreadyUsed) {
// Send the welcome message before the reset command
await this.communication.send(ctx, this.messageRenderer.welcome());
// Execute the reset command
Expand Down
1 change: 1 addition & 0 deletions telegram-bot/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./characters.ts";
export * from "./events.ts";
export * from "./html.ts";
export * from "./payloads.ts";
export * from "./render.ts";
1 change: 1 addition & 0 deletions telegram-bot/constants/payloads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const START_CALLOUT_PREFIX = "c_";

0 comments on commit a41a9bc

Please sign in to comment.