Skip to content

Commit 253d214

Browse files
committed
Add Support for Bot Typing Indicator
1 parent 5dc7da9 commit 253d214

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

handler/PostMessageSentHandler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AppSetting, DefaultMessage } from '../config/Settings';
66
import { ActionIds } from '../enum/ActionIds';
77
import { DialogflowRequestType, IDialogflowAction, IDialogflowMessage, IDialogflowPayload, LanguageCode, Message } from '../enum/Dialogflow';
88
import { Logs } from '../enum/Logs';
9+
import { botTypingListener, removeBotTypingListener } from '../lib//BotTyping';
910
import { Dialogflow } from '../lib/Dialogflow';
1011
import { createDialogflowMessage, createMessage } from '../lib/Message';
1112
import { handlePayloadActions } from '../lib/payloadAction';
@@ -66,6 +67,7 @@ export class PostMessageSentHandler {
6667
const { visitor: { token: visitorToken } } = room as ILivechatRoom;
6768

6869
try {
70+
await botTypingListener(rid, this.modify.getNotifier().typing({ id: rid, username: DialogflowBotUsername }));
6971
response = (await Dialogflow.sendRequest(this.http, this.read, this.modify, rid, text, DialogflowRequestType.MESSAGE));
7072
} catch (error) {
7173
this.app.getLogger().error(`${Logs.DIALOGFLOW_REST_API_ERROR} ${error.message}`);
@@ -90,11 +92,14 @@ export class PostMessageSentHandler {
9092
// synchronous handover check
9193
const { isFallback } = response;
9294
if (isFallback) {
95+
await removeBotTypingListener(rid);
9396
return incFallbackIntentAndSendResponse(this.read, this.modify, rid, createResponseMessage);
9497
}
9598

9699
await createResponseMessage();
97100

101+
await removeBotTypingListener(rid);
102+
98103
return resetFallbackIntent(this.read, this.modify, rid);
99104
}
100105

lib/BotTyping.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const rooms = {};
2+
3+
export async function botTypingListener(rid: string, callback: any) {
4+
if (rooms[rid]) {
5+
return;
6+
}
7+
rooms[rid] = callback;
8+
}
9+
10+
export async function removeBotTypingListener(rid: string) {
11+
if (rooms[rid]) {
12+
(await rooms[rid])();
13+
delete rooms[rid];
14+
}
15+
}

0 commit comments

Comments
 (0)