Skip to content

Commit 7ff56ab

Browse files
authored
Merge pull request RocketChat#37 from Shailesh351/sb_bot_typing
Add Support for Bot Typing Indicator
2 parents a3027f3 + 253d214 commit 7ff56ab

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';
@@ -73,6 +74,7 @@ export class PostMessageSentHandler {
7374
const { visitor: { token: visitorToken } } = room as ILivechatRoom;
7475

7576
try {
77+
await botTypingListener(rid, this.modify.getNotifier().typing({ id: rid, username: DialogflowBotUsername }));
7678
response = (await Dialogflow.sendRequest(this.http, this.read, this.modify, rid, text, DialogflowRequestType.MESSAGE));
7779
} catch (error) {
7880
this.app.getLogger().error(`${Logs.DIALOGFLOW_REST_API_ERROR} ${error.message}`);
@@ -97,11 +99,14 @@ export class PostMessageSentHandler {
9799
// synchronous handover check
98100
const { isFallback } = response;
99101
if (isFallback) {
102+
await removeBotTypingListener(rid);
100103
return incFallbackIntentAndSendResponse(this.read, this.modify, rid, createResponseMessage);
101104
}
102105

103106
await createResponseMessage();
104107

108+
await removeBotTypingListener(rid);
109+
105110
return resetFallbackIntent(this.read, this.modify, rid);
106111
}
107112

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)