Skip to content

Commit

Permalink
refactor(context): typing postback action
Browse files Browse the repository at this point in the history
- Default as string
- Enable different type of postback action
- Simplify handlePostback() argument with SDK types
  • Loading branch information
MrOrz committed Oct 18, 2023
1 parent d062abb commit cbd4064
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 35 deletions.
21 changes: 6 additions & 15 deletions src/types/chatbotState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Message, MessageEvent } from '@line/bot-sdk';
import type { Message, MessageEvent, PostbackEvent } from '@line/bot-sdk';

export type ChatbotState =
| '__INIT__'
Expand Down Expand Up @@ -28,18 +28,9 @@ type ArgumentedEventParams = {
input: string;
};

export type ChatbotEvent = (
| MessageEvent
| ServerChooseEvent
/**
* A special format of postback that Chatbot actually uses: postback + input (provided in `ArgumentedEventParams`)
* @FIXME Replace with original PostbackEvent and parse its action to support passing more thing than a string
*/
| {
type: 'postback';
}
) &
ArgumentedEventParams;
export type ChatbotEvent =
| ((MessageEvent | ServerChooseEvent) & ArgumentedEventParams)
| PostbackEvent;

export type Context = {
/** Used to differientiate different search sessions (searched text or media) */
Expand Down Expand Up @@ -82,8 +73,8 @@ export type ChatbotStateHandler = (
*
* @FIXME Replace input: string with something that is more structured
*/
export type PostbackActionData = {
input: string;
export type PostbackActionData<T> = {
input: T;
sessionId: number;
state: ChatbotState;
};
13 changes: 3 additions & 10 deletions src/webhook/handlePostback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ChatbotState,

Check warning on line 11 in src/webhook/handlePostback.ts

View workflow job for this annotation

GitHub Actions / install-and-test

'ChatbotState' is defined but never used

Check warning on line 11 in src/webhook/handlePostback.ts

View workflow job for this annotation

GitHub Actions / install-and-test

'ChatbotState' is defined but never used
ChatbotStateHandlerParams,
ChatbotStateHandlerReturnType,
PostbackActionData,
} from 'src/types/chatbotState';
import { Message } from '@line/bot-sdk';

Expand All @@ -24,23 +25,15 @@ import { Message } from '@line/bot-sdk';
*/
export default async function handlePostback(
{ data = {} },
state: ChatbotState,
postbackData: PostbackActionData<unknown>,
event: ChatbotEvent,
userId: string
) {
let replies: Message[] = [];

if (event.input === undefined) {
throw new Error('input undefined');
}

if (event.type !== 'postback') {
throw new Error('wrong event type');
}

let params: ChatbotStateHandlerParams | ChatbotStateHandlerReturnType = {
data,
state,
state: postbackData.state,
event,
userId,
replies,
Expand Down
6 changes: 3 additions & 3 deletions src/webhook/handlers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ const splitter = new GraphemeSplitter();
* @param sessionId - Current session ID
* @param state - the state that processes the postback
*/
export function createPostbackAction(
export function createPostbackAction<INPUT = string>(
label: string,
input: string,
input: INPUT,
displayText: string,
sessionId: number,
state: ChatbotState
): Action {
// Ensure the data type before stringification
const data: PostbackActionData = {
const data: PostbackActionData<INPUT> = {
input,
sessionId,
state,
Expand Down
9 changes: 2 additions & 7 deletions src/webhook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,9 @@ const singleUserHandler = async (
})
.send();
} else if (webhookEvent.type === 'postback') {
/**
* @FIXME Replace with runtime type check to be future-proof
*/
const postbackData = JSON.parse(
webhookEvent.postback.data
) as PostbackActionData;
) as PostbackActionData<unknown>;

// Handle the case when user context in redis is expired
if (!context.data) {
Expand Down Expand Up @@ -195,9 +192,7 @@ const singleUserHandler = async (
return;
}

const input = postbackData.input;
const event: ChatbotEvent = { type: webhookEvent.type, input };
result = await handlePostback(context, postbackData.state, event, userId);
result = await handlePostback(context, postbackData, webhookEvent, userId);
}

if (isReplied) {
Expand Down

0 comments on commit cbd4064

Please sign in to comment.