forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsendMessageToPostActivitySaga.ts
More file actions
44 lines (40 loc) · 1.62 KB
/
sendMessageToPostActivitySaga.ts
File metadata and controls
44 lines (40 loc) · 1.62 KB
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
import { put, takeEvery } from 'redux-saga/effects';
import postActivity from '../actions/postActivity';
import sendMessage, { SEND_MESSAGE } from '../actions/sendMessage';
import whileConnected from './effects/whileConnected';
function* postActivityWithMessage({
payload: { attachments = [], channelData, method, text }
}: ReturnType<typeof sendMessage>) {
yield put(
postActivity(
{
attachments: attachments.map(({ blob, thumbnailURL }) => ({
contentType: (blob instanceof File && blob.type) || 'application/octet-stream',
// Chat adapter should download the file as binary.
// In case the chat adapter naively echo back the URL, it will be treated as binary.
// eslint-disable-next-line no-restricted-properties
contentUrl: URL.createObjectURL(new Blob([blob], { type: 'application/octet-stream' })),
name: blob instanceof File ? blob.name : undefined,
thumbnailUrl: thumbnailURL?.toString()
})),
channelData: {
...channelData,
attachmentSizes: attachments.map(({ blob: { size } }) => size)
},
text: text || undefined,
textFormat: 'plain',
type: 'message'
} as any, // TODO: Fix WebChatActivity. Currently, it only works with incoming activity, not outgoing.
method
)
);
}
function* sendMessageToPostActivity() {
yield takeEvery(
({ payload, type }) => type === SEND_MESSAGE && (payload.text || payload.attachments?.length),
postActivityWithMessage
);
}
export default function* sendMessageToPostActivitySaga() {
yield whileConnected(sendMessageToPostActivity);
}