This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 529
/
eventDebuggerMiddleware.ts
47 lines (39 loc) · 1.87 KB
/
eventDebuggerMiddleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Copyright(c) Microsoft Corporation.All rights reserved.
* Licensed under the MIT License.
*/
import { Middleware, TurnContext } from 'botbuilder';
import { Activity, ActivityTypes } from 'botframework-schema';
export class EventDebuggerMiddleware implements Middleware {
public async onTurn(turnContext: TurnContext, next: () => Promise<void>): Promise<void> {
const activity: Activity = turnContext.activity;
if (activity.type === ActivityTypes.Message) {
const text: string = activity.text;
const value: string = JSON.stringify(activity.value);
if (text && text.startsWith('/event:')) {
const json: string = text.substr('/event:'.length);
const body: Activity = JSON.parse(json);
turnContext.activity.type = ActivityTypes.Event;
turnContext.activity.name = body.name || turnContext.activity.name;
turnContext.activity.text = body.text || turnContext.activity.text;
turnContext.activity.value = body.value || turnContext.activity.value;
}
if (value && value.includes('event')) {
const body: { event: { name: string; text: string; value: string }} = JSON.parse(value);
turnContext.activity.type = ActivityTypes.Event;
if (body.event !== undefined) {
if (body.event.name !== undefined) {
turnContext.activity.name = body.event.name;
}
if (body.event.text !== undefined) {
turnContext.activity.text = body.event.text;
}
if (body.event.value !== undefined) {
turnContext.activity.value = body.event.value;
}
}
}
}
return next();
}
}