Skip to content

Commit

Permalink
feat(user-feedback): Render intro text directly after the description
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Jul 2, 2024
1 parent 64ff582 commit 67fb728
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 258 deletions.
20 changes: 16 additions & 4 deletions telegram-bot/commands/show.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ export class ShowCommand extends BaseCommand {
}

try {
// const session = await ctx.session;
const callout = await this.callout.get(slug);
const render = await this.calloutRenderer.calloutDetails(callout);
const callout = await this.callout.get(slug, ["form"]);
const calloutDetailsRender = await this.calloutRenderer.calloutDetails(
callout,
);
const calloutIntroRender = this.calloutRenderer.intro(
callout,
);
const calloutStartResponseKeyboard = this.calloutRenderer
.startResponseKeyboard(
callout,
);

const signal = await this.stateMachine.setSessionState(
ctx,
Expand All @@ -65,7 +73,11 @@ export class ShowCommand extends BaseCommand {
throw new Error("The AbortSignal is required!");
}

await this.communication.sendAndReceiveAll(ctx, render, signal);
await this.communication.sendAndReceiveAll(ctx, [
calloutDetailsRender,
calloutIntroRender,
calloutStartResponseKeyboard,
], signal);
} catch (error) {
console.error("Error sending callout", error);
successful = false;
Expand Down
3 changes: 1 addition & 2 deletions telegram-bot/constants/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ export const INLINE_BUTTON_CALLBACK_PREFIX = "callback_query:data";
// Note: We choose short strings here because the length of these strings is limited in the Telegram bot API
// and the slug is appended here.The max allowed size of a callback string is 64 bytes.
export const INLINE_BUTTON_CALLBACK_SHOW_CALLOUT = "1";
export const INLINE_BUTTON_CALLBACK_CALLOUT_INTRO = "2";
export const INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE = "3";
export const INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE = "2";

/**
* Prefix for the callout response interaction events, used for skip and done inline buttons, perhaps useful for others.
Expand Down
184 changes: 27 additions & 157 deletions telegram-bot/deno.lock

Large diffs are not rendered by default.

76 changes: 16 additions & 60 deletions telegram-bot/event-managers/callout-response.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { ListCommand } from "../commands/list.command.ts";
import { ChatState } from "../enums/index.ts";
import {
FALSY_MESSAGE_KEY,
INLINE_BUTTON_CALLBACK_CALLOUT_INTRO,
INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE,
INLINE_BUTTON_CALLBACK_PREFIX,
TRUTHY_MESSAGE_KEY,
Expand Down Expand Up @@ -43,14 +42,6 @@ export class CalloutResponseEventManager extends BaseEventManager {
}

public init() {
// Listen for the callback query data event with the `callout-respond:yes` data
this.event.on(
`${INLINE_BUTTON_CALLBACK_PREFIX}:${INLINE_BUTTON_CALLBACK_CALLOUT_INTRO}`,
(event) => {
this.onCalloutIntroKeyboardPressed(event);
},
);

this.event.on(
`${INLINE_BUTTON_CALLBACK_PREFIX}:${INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE}`,
(event) => {
Expand All @@ -61,8 +52,12 @@ export class CalloutResponseEventManager extends BaseEventManager {

protected async onCalloutParticipateKeyboardPressed(ctx: AppContext) {
const data = ctx.callbackQuery?.data?.split(":");
const slug = data?.[1];
const startResponse = data?.[2] as "continue" | "cancel" === "continue";
// const slug = data?.[1];
const shortSlug = data?.[1];
// const startResponse = data?.[2] as "continue" | "cancel" === "continue";
const startResponse =
data?.[2] as typeof TRUTHY_MESSAGE_KEY | typeof FALSY_MESSAGE_KEY ===
TRUTHY_MESSAGE_KEY; // This is the key, so it's not localized
const session = await ctx.session;

await this.keyboard.removeInlineKeyboard(ctx);
Expand All @@ -75,14 +70,21 @@ export class CalloutResponseEventManager extends BaseEventManager {
return;
}

if (!slug) {
if (!shortSlug) {
await this.communication.send(
ctx,
this.messageRenderer.calloutNotFound(),
);
return;
}

// remove loading animation
await this.communication.answerCallbackQuery(ctx);
const slug = this.callout.getSlug(shortSlug);

if (!slug) {
await this.communication.send(
ctx,
this.messageRenderer.calloutNotFound(),
);
return;
}

Expand Down Expand Up @@ -176,50 +178,4 @@ export class CalloutResponseEventManager extends BaseEventManager {
console.error(error);
}
}

/**
* Handle the callback query data event with the `callout-respond:yes` or `callout-respond:no` data.
* Called when the user presses the "Yes" or "No" button on the callout response keyboard.
* @param ctx
*/
protected async onCalloutIntroKeyboardPressed(ctx: AppContext) {
const data = ctx.callbackQuery?.data?.split(":");
const shortSlug = data?.[1];
const startIntro =
data?.[2] as typeof TRUTHY_MESSAGE_KEY | typeof FALSY_MESSAGE_KEY ===
TRUTHY_MESSAGE_KEY; // This is the key, so it's not localized

await this.keyboard.removeInlineKeyboard(ctx);

if (!shortSlug) {
await this.communication.send(
ctx,
this.messageRenderer.calloutNotFound(),
);
return;
}

const slug = this.callout.getSlug(shortSlug);

if (!slug) {
await this.communication.send(
ctx,
this.messageRenderer.calloutNotFound(),
);
return;
}

if (!startIntro) {
await this.communication.send(ctx, this.messageRenderer.stop());
// Forward cancel to the cancel command
await this.resetCommand.action(ctx);
return;
}

// Start intro
const calloutWithForm = await this.callout.get(slug, ["form"]);

const res = this.calloutResponseRenderer.intro(calloutWithForm);
await this.communication.send(ctx, res);
}
}
18 changes: 14 additions & 4 deletions telegram-bot/event-managers/callout.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ export class CalloutEventManager extends BaseEventManager {
}

try {
const callout = await this.callout.get(slug);

const calloutFormRender = await this.calloutRenderer.calloutDetails(
const callout = await this.callout.get(slug, ["form"]);
const calloutDetailsRender = await this.calloutRenderer.calloutDetails(
callout,
);
const calloutIntroRender = this.calloutRenderer.intro(
callout,
);
const calloutStartResponseKeyboard = this.calloutRenderer
.startResponseKeyboard(
callout,
);

const signal = await this.stateMachine.setSessionState(
ctx,
Expand All @@ -78,7 +84,11 @@ export class CalloutEventManager extends BaseEventManager {

await this.communication.sendAndReceiveAll(
ctx,
calloutFormRender,
[
calloutDetailsRender,
calloutIntroRender,
calloutStartResponseKeyboard,
],
signal,
);
} catch (error) {
Expand Down
23 changes: 0 additions & 23 deletions telegram-bot/renderer/callout-response.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { ConditionService } from "../services/condition.service.ts";
import { MessageRenderer } from "./message.renderer.ts";
import {
EMPTY_RENDER,
INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE,
INLINE_BUTTON_CALLBACK_CALLOUT_RESPONSE,
} from "../constants/index.ts";

Expand Down Expand Up @@ -806,28 +805,6 @@ export class CalloutResponseRenderer {
return results;
}

/**
* Render a callout response intro in HTML
*/
public intro(callout: GetCalloutDataWithExt<"form">) {
const result: Render = {
key: callout.slug,
type: RenderType.HTML,
accepted: this.condition.replayConditionNone(),
html: "",
parseType: ParsedResponseType.NONE,
forceReply: false,
};
result.html = `${sanitizeHtml(callout.intro)}`;

const continueKeyboard = this.keyboard.inlineContinueCancel(
`${INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE}:${callout.slug}`,
);
result.inlineKeyboard = continueKeyboard;

return result;
}

/**
* Render the callout response thank you
*/
Expand Down
32 changes: 24 additions & 8 deletions telegram-bot/renderer/callout.renderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InputFile, InputMediaBuilder, Singleton } from "../deps/index.ts";
import { downloadImage, escapeMd } from "../utils/index.ts";
import { downloadImage, escapeMd, sanitizeHtml } from "../utils/index.ts";
import { ParsedResponseType, RenderType } from "../enums/index.ts";
import { INLINE_BUTTON_CALLBACK_CALLOUT_INTRO } from "../constants/index.ts";
import { INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE } from "../constants/index.ts";

import { ConditionService } from "../services/condition.service.ts";
import { KeyboardService } from "../services/keyboard.service.ts";
Expand All @@ -10,6 +10,7 @@ import { I18nService } from "../services/i18n.service.ts";
import type {
CalloutDataExt,
GetCalloutDataExt,
GetCalloutDataWithExt,
Render,
} from "../types/index.ts";

Expand All @@ -29,19 +30,20 @@ export class CalloutRenderer {
}

/**
* @fires `${INLINE_BUTTON_CALLBACK_PREFIX}:${INLINE_BUTTON_CALLBACK_CALLOUT_INTRO}`
* Renders a start response keyboard for a callout
* @fires `${INLINE_BUTTON_CALLBACK_PREFIX}:${INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE}`
*
* @param callout
* @returns
*/
protected startResponseKeyboard(
public startResponseKeyboard(
callout: CalloutDataExt,
): Render {
const keyboardMessageMd = `_${
escapeMd(this.i18n.t("bot.response.messages.calloutStartResponse"))
}_`;
const yesNoInlineKeyboard = this.keyboard.inlineYesNo(
`${INLINE_BUTTON_CALLBACK_CALLOUT_INTRO}:${callout.shortSlug}`,
`${INLINE_BUTTON_CALLBACK_CALLOUT_PARTICIPATE}:${callout.shortSlug}`,
);

const result: Render = {
Expand Down Expand Up @@ -150,6 +152,22 @@ export class CalloutRenderer {
return result;
}

/**
* Render a callout response intro in HTML
*/
public intro(callout: GetCalloutDataWithExt<"form">) {
const result: Render = {
key: callout.slug,
type: RenderType.HTML,
accepted: this.condition.replayConditionNone(),
html: "",
parseType: ParsedResponseType.NONE,
forceReply: false,
};
result.html = `${sanitizeHtml(callout.intro)}`;
return result;
}

/**
* Render a callout with photo.
*
Expand All @@ -176,8 +194,6 @@ export class CalloutRenderer {
parseType: ParsedResponseType.NONE,
};

const keyboardResult = this.startResponseKeyboard(callout);

return [calloutResult, keyboardResult];
return calloutResult;
}
}

0 comments on commit 67fb728

Please sign in to comment.