/
botSagas.ts
197 lines (180 loc) · 6.75 KB
/
botSagas.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import {
beginAdd,
botHashGenerated,
newNotification,
open as openEditorDocument,
BotAction,
BotActionType,
BotConfigWithPathPayload,
SharedConstants,
} from '@bfemulator/app-shared';
import {
CommandServiceImpl,
CommandServiceInstance,
ConversationService,
StartConversationParams,
uniqueIdv4,
User,
isLocalHostUrl,
} from '@bfemulator/sdk-shared';
import { call, ForkEffect, put, select, takeEvery, takeLatest } from 'redux-saga/effects';
import { ActiveBotHelper } from '../../ui/helpers/activeBotHelper';
import { generateHash } from '../helpers/botHelpers';
import { RootState } from '../store';
import { SharedSagas } from './sharedSagas';
import { ChatSagas } from './chatSagas';
const getServerUrl = (state: RootState): string => {
return state.clientAwareSettings.serverUrl;
};
const getCustomUserGUID = (state: RootState): string => {
return state.framework.userGUID;
};
export class BotSagas {
@CommandServiceInstance()
private static commandService: CommandServiceImpl;
public static *browseForBot(): IterableIterator<any> {
yield call([ActiveBotHelper, ActiveBotHelper.confirmAndOpenBotFromFile]);
}
public static *generateHashForActiveBot(action: BotAction<BotConfigWithPathPayload>): IterableIterator<any> {
const { bot } = action.payload;
const generatedHash = yield call(generateHash, bot);
yield put(botHashGenerated(generatedHash));
}
public static *openBotViaFilePath(action: BotAction<string>) {
try {
yield call([ActiveBotHelper, ActiveBotHelper.confirmAndOpenBotFromFile], action.payload);
} catch (e) {
const errorNotification = beginAdd(
newNotification(`An Error occurred opening the bot at ${action.payload}: ${e}`)
);
yield put(errorNotification);
}
}
public static *openBotViaUrl(
action: BotAction<StartConversationParams & { isFromBotFile?: boolean }>
): IterableIterator<any> {
const user = {
id: yield select(getCustomUserGUID) || uniqueIdv4(), // use custom id or generate new one
name: 'User',
role: 'user',
};
const serverUrl = yield select(getServerUrl);
const payload = {
botUrl: action.payload.endpoint,
channelServiceType: action.payload.channelService,
members: [user],
mode: action.payload.mode,
msaAppId: action.payload.appId,
msaPassword: action.payload.appPassword,
};
let res: Response = yield ConversationService.startConversation(serverUrl, payload);
if (!res.ok) {
throw new Error(
`Error occurred while starting a new conversation: ${res.status}: ${res.statusText || 'No status text'}`
);
}
const {
conversationId,
endpointId,
members,
}: { conversationId: string; endpointId: string; members: User[] } = yield res.json();
const documentId = `${conversationId}`;
// trigger chat saga that will populate the chat object in the store
yield ChatSagas.bootstrapChat({
conversationId,
documentId,
endpointId,
mode: action.payload.mode,
msaAppId: action.payload.appId,
msaPassword: action.payload.appPassword,
user,
});
// add a document to the store so the livechat tab is rendered
const { CONTENT_TYPE_DEBUG, CONTENT_TYPE_LIVE_CHAT } = SharedConstants.ContentTypes;
yield put(
openEditorDocument({
contentType: action.payload.mode === 'debug' ? CONTENT_TYPE_DEBUG : CONTENT_TYPE_LIVE_CHAT,
documentId,
isGlobal: false,
})
);
// call emulator to report proper status to chat panel (listening / ngrok)
res = yield ConversationService.sendInitialLogReport(serverUrl, conversationId, action.payload.endpoint);
if (!res.ok) {
throw new Error(
`Error occurred while sending the initial log report: ${res.status}: ${res.statusText || 'No status text'}`
);
}
// send CU or debug INSPECT message
res = yield ChatSagas.sendInitialActivity({ conversationId, members, mode: action.payload.mode });
if (!res.ok) {
throw new Error(
`Error occurred while sending the initial activity: ${res.status}: ${res.statusText || 'No status text'}`
);
}
// remember the endpoint
yield call(
[BotSagas.commandService, BotSagas.commandService.remoteCall],
SharedConstants.Commands.Settings.SaveBotUrl,
action.payload.endpoint
);
// telemetry
if (!action.payload.isFromBotFile) {
BotSagas.commandService
.remoteCall(SharedConstants.Commands.Telemetry.TrackEvent, 'bot_open', {
numOfServices: 0,
source: 'url',
})
.catch(_ => void 0);
}
BotSagas.commandService
.remoteCall(SharedConstants.Commands.Telemetry.TrackEvent, 'livechat_open', {
isDebug: action.payload.mode === 'debug',
isGov: action.payload.channelService === 'azureusgovernment',
isRemote: !isLocalHostUrl(action.payload.endpoint),
})
.catch(_ => void 0);
}
}
export function* botSagas(): IterableIterator<ForkEffect> {
yield takeEvery(BotActionType.browse, BotSagas.browseForBot);
yield takeEvery(BotActionType.openViaUrl, BotSagas.openBotViaUrl);
yield takeEvery(BotActionType.openViaFilePath, BotSagas.openBotViaFilePath);
yield takeEvery(BotActionType.setActive, BotSagas.generateHashForActiveBot);
yield takeLatest(
[BotActionType.setActive, BotActionType.load, BotActionType.close],
SharedSagas.refreshConversationMenu
);
}