@gakr-gakr/msteams 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.ts +3 -0
- package/autobot.plugin.json +15 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +2 -0
- package/config-api.ts +4 -0
- package/contract-api.ts +4 -0
- package/index.ts +20 -0
- package/package.json +72 -0
- package/runtime-api.ts +66 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +13 -0
- package/setup-plugin-api.ts +3 -0
- package/src/ai-entity.ts +7 -0
- package/src/approval-auth.ts +44 -0
- package/src/attachments/bot-framework.ts +348 -0
- package/src/attachments/download.ts +328 -0
- package/src/attachments/graph.ts +489 -0
- package/src/attachments/html.ts +122 -0
- package/src/attachments/payload.ts +14 -0
- package/src/attachments/remote-media.ts +86 -0
- package/src/attachments/shared.ts +655 -0
- package/src/attachments/types.ts +47 -0
- package/src/attachments.ts +18 -0
- package/src/channel-api.ts +1 -0
- package/src/channel.runtime.ts +56 -0
- package/src/channel.setup.ts +77 -0
- package/src/channel.ts +1176 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +40 -0
- package/src/conversation-store-fs.ts +149 -0
- package/src/conversation-store-helpers.ts +105 -0
- package/src/conversation-store-memory.ts +51 -0
- package/src/conversation-store.ts +71 -0
- package/src/directory-live.ts +111 -0
- package/src/doctor.ts +27 -0
- package/src/errors.ts +270 -0
- package/src/feedback-reflection-prompt.ts +117 -0
- package/src/feedback-reflection-store.ts +113 -0
- package/src/feedback-reflection.ts +271 -0
- package/src/file-consent-helpers.ts +115 -0
- package/src/file-consent-invoke.ts +150 -0
- package/src/file-consent.ts +223 -0
- package/src/graph-chat.ts +36 -0
- package/src/graph-group-management.ts +168 -0
- package/src/graph-members.ts +48 -0
- package/src/graph-messages.ts +534 -0
- package/src/graph-teams.ts +114 -0
- package/src/graph-thread.ts +146 -0
- package/src/graph-upload.ts +531 -0
- package/src/graph-users.ts +29 -0
- package/src/graph.ts +308 -0
- package/src/inbound.ts +148 -0
- package/src/index.ts +4 -0
- package/src/media-helpers.ts +105 -0
- package/src/mentions.ts +114 -0
- package/src/messenger.ts +608 -0
- package/src/monitor-handler/access.ts +136 -0
- package/src/monitor-handler/inbound-media.ts +180 -0
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
- package/src/monitor-handler/message-handler.test-support.ts +102 -0
- package/src/monitor-handler/message-handler.ts +1015 -0
- package/src/monitor-handler/reaction-handler.ts +124 -0
- package/src/monitor-handler/thread-session.ts +30 -0
- package/src/monitor-handler.ts +538 -0
- package/src/monitor-handler.types.ts +27 -0
- package/src/monitor-types.ts +6 -0
- package/src/monitor.ts +476 -0
- package/src/oauth.flow.ts +77 -0
- package/src/oauth.shared.ts +37 -0
- package/src/oauth.token.ts +162 -0
- package/src/oauth.ts +130 -0
- package/src/outbound.ts +198 -0
- package/src/pending-uploads-fs.ts +235 -0
- package/src/pending-uploads.ts +121 -0
- package/src/policy.ts +245 -0
- package/src/polls-store-memory.ts +32 -0
- package/src/polls.ts +312 -0
- package/src/presentation.ts +93 -0
- package/src/probe.ts +132 -0
- package/src/reply-dispatcher.ts +523 -0
- package/src/reply-stream-controller.ts +334 -0
- package/src/resolve-allowlist.ts +309 -0
- package/src/revoked-context.ts +17 -0
- package/src/runtime.ts +12 -0
- package/src/sdk-types.ts +59 -0
- package/src/sdk.ts +916 -0
- package/src/secret-contract.ts +49 -0
- package/src/secret-input.ts +7 -0
- package/src/send-context.ts +269 -0
- package/src/send.ts +697 -0
- package/src/sent-message-cache.ts +174 -0
- package/src/session-route.ts +40 -0
- package/src/setup-core.ts +162 -0
- package/src/setup-surface.ts +319 -0
- package/src/sso-token-store.ts +166 -0
- package/src/sso.ts +300 -0
- package/src/storage.ts +25 -0
- package/src/store-fs.ts +42 -0
- package/src/streaming-message.ts +327 -0
- package/src/thread-parent-context.ts +159 -0
- package/src/token-response.ts +11 -0
- package/src/token.ts +194 -0
- package/src/user-agent.ts +53 -0
- package/src/webhook-timeouts.ts +27 -0
- package/src/welcome-card.ts +57 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { readProviderJsonResponse } from "autobot/plugin-sdk/provider-http";
|
|
2
|
+
import { fetchWithSsrFGuard } from "autobot/plugin-sdk/ssrf-runtime";
|
|
3
|
+
import {
|
|
4
|
+
MSTEAMS_DEFAULT_DELEGATED_SCOPES,
|
|
5
|
+
MSTEAMS_DEFAULT_TOKEN_FETCH_TIMEOUT_MS,
|
|
6
|
+
MSTEAMS_OAUTH_REDIRECT_URI,
|
|
7
|
+
buildMSTeamsTokenEndpoint,
|
|
8
|
+
type MSTeamsDelegatedTokens,
|
|
9
|
+
} from "./oauth.shared.js";
|
|
10
|
+
|
|
11
|
+
/** Five-minute buffer subtracted from token expiry to avoid edge-case clock drift. */
|
|
12
|
+
const EXPIRY_BUFFER_MS = 5 * 60 * 1000;
|
|
13
|
+
|
|
14
|
+
type MSTeamsTokenResponse = {
|
|
15
|
+
access_token: string;
|
|
16
|
+
refresh_token?: string;
|
|
17
|
+
expires_in: number;
|
|
18
|
+
scope?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function createMSTeamsTokenBody(params: {
|
|
22
|
+
clientId: string;
|
|
23
|
+
clientSecret: string;
|
|
24
|
+
grantType: string;
|
|
25
|
+
scopes: readonly string[];
|
|
26
|
+
values?: Record<string, string>;
|
|
27
|
+
}): URLSearchParams {
|
|
28
|
+
const body = new URLSearchParams({
|
|
29
|
+
client_id: params.clientId,
|
|
30
|
+
client_secret: params.clientSecret,
|
|
31
|
+
grant_type: params.grantType,
|
|
32
|
+
scope: [...params.scopes].join(" "),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
for (const [key, value] of Object.entries(params.values ?? {})) {
|
|
36
|
+
body.set(key, value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return body;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function fetchMSTeamsTokens(params: {
|
|
43
|
+
tokenUrl: string;
|
|
44
|
+
body: URLSearchParams;
|
|
45
|
+
auditContext: string;
|
|
46
|
+
failureLabel: string;
|
|
47
|
+
}): Promise<MSTeamsTokenResponse> {
|
|
48
|
+
const currentFetch = globalThis.fetch;
|
|
49
|
+
const { response, release } = await fetchWithSsrFGuard({
|
|
50
|
+
url: params.tokenUrl,
|
|
51
|
+
fetchImpl: async (input, guardedInit) => await currentFetch(input, guardedInit),
|
|
52
|
+
init: {
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers: {
|
|
55
|
+
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
56
|
+
Accept: "application/json",
|
|
57
|
+
},
|
|
58
|
+
body: params.body,
|
|
59
|
+
signal: AbortSignal.timeout(MSTEAMS_DEFAULT_TOKEN_FETCH_TIMEOUT_MS),
|
|
60
|
+
},
|
|
61
|
+
auditContext: params.auditContext,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
const errorText = await response.text();
|
|
67
|
+
throw new Error(`MSTeams ${params.failureLabel} failed (${response.status}): ${errorText}`);
|
|
68
|
+
}
|
|
69
|
+
return await readProviderJsonResponse<MSTeamsTokenResponse>(
|
|
70
|
+
response,
|
|
71
|
+
`MSTeams ${params.failureLabel} failed`,
|
|
72
|
+
);
|
|
73
|
+
} finally {
|
|
74
|
+
await release();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function requestMSTeamsDelegatedTokens(params: {
|
|
79
|
+
tenantId: string;
|
|
80
|
+
clientId: string;
|
|
81
|
+
clientSecret: string;
|
|
82
|
+
scopes?: readonly string[];
|
|
83
|
+
grantType: string;
|
|
84
|
+
values: Record<string, string>;
|
|
85
|
+
auditContext: string;
|
|
86
|
+
failureLabel: string;
|
|
87
|
+
resolveRefreshToken: (data: MSTeamsTokenResponse) => string;
|
|
88
|
+
}): Promise<MSTeamsDelegatedTokens> {
|
|
89
|
+
const scopes = params.scopes ?? MSTEAMS_DEFAULT_DELEGATED_SCOPES;
|
|
90
|
+
const body = createMSTeamsTokenBody({
|
|
91
|
+
clientId: params.clientId,
|
|
92
|
+
clientSecret: params.clientSecret,
|
|
93
|
+
grantType: params.grantType,
|
|
94
|
+
scopes,
|
|
95
|
+
values: params.values,
|
|
96
|
+
});
|
|
97
|
+
const data = await fetchMSTeamsTokens({
|
|
98
|
+
tokenUrl: buildMSTeamsTokenEndpoint(params.tenantId),
|
|
99
|
+
body,
|
|
100
|
+
auditContext: params.auditContext,
|
|
101
|
+
failureLabel: params.failureLabel,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
accessToken: data.access_token,
|
|
106
|
+
refreshToken: params.resolveRefreshToken(data),
|
|
107
|
+
expiresAt: Date.now() + data.expires_in * 1000 - EXPIRY_BUFFER_MS,
|
|
108
|
+
scopes: data.scope ? data.scope.split(" ") : [...scopes],
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function exchangeMSTeamsCodeForTokens(params: {
|
|
113
|
+
tenantId: string;
|
|
114
|
+
clientId: string;
|
|
115
|
+
clientSecret: string;
|
|
116
|
+
code: string;
|
|
117
|
+
verifier: string;
|
|
118
|
+
scopes?: readonly string[];
|
|
119
|
+
}): Promise<MSTeamsDelegatedTokens> {
|
|
120
|
+
return await requestMSTeamsDelegatedTokens({
|
|
121
|
+
tenantId: params.tenantId,
|
|
122
|
+
clientId: params.clientId,
|
|
123
|
+
clientSecret: params.clientSecret,
|
|
124
|
+
grantType: "authorization_code",
|
|
125
|
+
scopes: params.scopes,
|
|
126
|
+
values: {
|
|
127
|
+
code: params.code,
|
|
128
|
+
redirect_uri: MSTEAMS_OAUTH_REDIRECT_URI,
|
|
129
|
+
code_verifier: params.verifier,
|
|
130
|
+
},
|
|
131
|
+
auditContext: "msteams-oauth-token-exchange",
|
|
132
|
+
failureLabel: "token exchange",
|
|
133
|
+
resolveRefreshToken: (data) => {
|
|
134
|
+
if (!data.refresh_token) {
|
|
135
|
+
throw new Error("No refresh token received from Azure AD. Please try again.");
|
|
136
|
+
}
|
|
137
|
+
return data.refresh_token;
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function refreshMSTeamsDelegatedTokens(params: {
|
|
143
|
+
tenantId: string;
|
|
144
|
+
clientId: string;
|
|
145
|
+
clientSecret: string;
|
|
146
|
+
refreshToken: string;
|
|
147
|
+
scopes?: readonly string[];
|
|
148
|
+
}): Promise<MSTeamsDelegatedTokens> {
|
|
149
|
+
return await requestMSTeamsDelegatedTokens({
|
|
150
|
+
tenantId: params.tenantId,
|
|
151
|
+
clientId: params.clientId,
|
|
152
|
+
clientSecret: params.clientSecret,
|
|
153
|
+
grantType: "refresh_token",
|
|
154
|
+
scopes: params.scopes,
|
|
155
|
+
values: {
|
|
156
|
+
refresh_token: params.refreshToken,
|
|
157
|
+
},
|
|
158
|
+
auditContext: "msteams-oauth-token-refresh",
|
|
159
|
+
failureLabel: "token refresh",
|
|
160
|
+
resolveRefreshToken: (data) => data.refresh_token ?? params.refreshToken,
|
|
161
|
+
});
|
|
162
|
+
}
|
package/src/oauth.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildMSTeamsAuthUrl,
|
|
3
|
+
generateOAuthState,
|
|
4
|
+
generatePkce,
|
|
5
|
+
parseCallbackInput,
|
|
6
|
+
shouldUseManualOAuthFlow,
|
|
7
|
+
waitForLocalCallback,
|
|
8
|
+
} from "./oauth.flow.js";
|
|
9
|
+
import {
|
|
10
|
+
MSTEAMS_DEFAULT_DELEGATED_SCOPES,
|
|
11
|
+
MSTEAMS_OAUTH_CALLBACK_PORT,
|
|
12
|
+
type MSTeamsDelegatedOAuthContext,
|
|
13
|
+
type MSTeamsDelegatedTokens,
|
|
14
|
+
} from "./oauth.shared.js";
|
|
15
|
+
import { exchangeMSTeamsCodeForTokens } from "./oauth.token.js";
|
|
16
|
+
|
|
17
|
+
export type { MSTeamsDelegatedOAuthContext, MSTeamsDelegatedTokens };
|
|
18
|
+
|
|
19
|
+
export async function loginMSTeamsDelegated(
|
|
20
|
+
ctx: MSTeamsDelegatedOAuthContext,
|
|
21
|
+
params: {
|
|
22
|
+
tenantId: string;
|
|
23
|
+
clientId: string;
|
|
24
|
+
clientSecret: string;
|
|
25
|
+
scopes?: readonly string[];
|
|
26
|
+
},
|
|
27
|
+
): Promise<MSTeamsDelegatedTokens> {
|
|
28
|
+
const scopes = params.scopes ?? MSTEAMS_DEFAULT_DELEGATED_SCOPES;
|
|
29
|
+
const needsManual = shouldUseManualOAuthFlow(ctx.isRemote);
|
|
30
|
+
|
|
31
|
+
await ctx.note(
|
|
32
|
+
needsManual
|
|
33
|
+
? [
|
|
34
|
+
"You are running in a remote/VPS environment.",
|
|
35
|
+
"A URL will be shown for you to open in your LOCAL browser.",
|
|
36
|
+
"After signing in, copy the redirect URL and paste it back here.",
|
|
37
|
+
].join("\n")
|
|
38
|
+
: [
|
|
39
|
+
"Browser will open for Microsoft authentication.",
|
|
40
|
+
`Sign in to grant delegated permissions for MSTeams.`,
|
|
41
|
+
`The callback will be captured automatically on localhost:${MSTEAMS_OAUTH_CALLBACK_PORT}.`,
|
|
42
|
+
].join("\n"),
|
|
43
|
+
"MSTeams Delegated OAuth",
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const { verifier, challenge } = generatePkce();
|
|
47
|
+
const state = generateOAuthState();
|
|
48
|
+
const authUrl = buildMSTeamsAuthUrl({
|
|
49
|
+
tenantId: params.tenantId,
|
|
50
|
+
clientId: params.clientId,
|
|
51
|
+
challenge,
|
|
52
|
+
state,
|
|
53
|
+
scopes,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (needsManual) {
|
|
57
|
+
return manualFlow(ctx, authUrl, state, verifier, params);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
ctx.progress.update("Complete sign-in in browser...");
|
|
61
|
+
try {
|
|
62
|
+
await ctx.openUrl(authUrl);
|
|
63
|
+
} catch {
|
|
64
|
+
ctx.log(`\nOpen this URL in your browser:\n\n${authUrl}\n`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
const { code } = await waitForLocalCallback({
|
|
69
|
+
expectedState: state,
|
|
70
|
+
timeoutMs: 5 * 60 * 1000,
|
|
71
|
+
onProgress: (msg) => ctx.progress.update(msg),
|
|
72
|
+
});
|
|
73
|
+
ctx.progress.update("Exchanging authorization code for tokens...");
|
|
74
|
+
return await exchangeMSTeamsCodeForTokens({
|
|
75
|
+
tenantId: params.tenantId,
|
|
76
|
+
clientId: params.clientId,
|
|
77
|
+
clientSecret: params.clientSecret,
|
|
78
|
+
code,
|
|
79
|
+
verifier,
|
|
80
|
+
scopes,
|
|
81
|
+
});
|
|
82
|
+
} catch (err) {
|
|
83
|
+
// EADDRINUSE or other listen errors: fall back to manual flow
|
|
84
|
+
if (
|
|
85
|
+
err instanceof Error &&
|
|
86
|
+
(err.message.includes("EADDRINUSE") ||
|
|
87
|
+
err.message.includes("port") ||
|
|
88
|
+
err.message.includes("listen"))
|
|
89
|
+
) {
|
|
90
|
+
ctx.progress.update("Local callback server failed. Switching to manual mode...");
|
|
91
|
+
return manualFlow(ctx, authUrl, state, verifier, params, err);
|
|
92
|
+
}
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function manualFlow(
|
|
98
|
+
ctx: MSTeamsDelegatedOAuthContext,
|
|
99
|
+
authUrl: string,
|
|
100
|
+
state: string,
|
|
101
|
+
verifier: string,
|
|
102
|
+
params: {
|
|
103
|
+
tenantId: string;
|
|
104
|
+
clientId: string;
|
|
105
|
+
clientSecret: string;
|
|
106
|
+
scopes?: readonly string[];
|
|
107
|
+
},
|
|
108
|
+
cause?: Error,
|
|
109
|
+
): Promise<MSTeamsDelegatedTokens> {
|
|
110
|
+
ctx.progress.update("OAuth URL ready");
|
|
111
|
+
ctx.log(`\nOpen this URL in your LOCAL browser:\n\n${authUrl}\n`);
|
|
112
|
+
ctx.progress.update("Waiting for you to paste the callback URL...");
|
|
113
|
+
const callbackInput = await ctx.prompt("Paste the redirect URL here: ");
|
|
114
|
+
const parsed = parseCallbackInput(callbackInput, state);
|
|
115
|
+
if ("error" in parsed) {
|
|
116
|
+
throw new Error(parsed.error, cause ? { cause } : undefined);
|
|
117
|
+
}
|
|
118
|
+
if (parsed.state !== state) {
|
|
119
|
+
throw new Error("OAuth state mismatch - please try again", cause ? { cause } : undefined);
|
|
120
|
+
}
|
|
121
|
+
ctx.progress.update("Exchanging authorization code for tokens...");
|
|
122
|
+
return exchangeMSTeamsCodeForTokens({
|
|
123
|
+
tenantId: params.tenantId,
|
|
124
|
+
clientId: params.clientId,
|
|
125
|
+
clientSecret: params.clientSecret,
|
|
126
|
+
code: parsed.code,
|
|
127
|
+
verifier,
|
|
128
|
+
scopes: params.scopes,
|
|
129
|
+
});
|
|
130
|
+
}
|
package/src/outbound.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import {
|
|
2
|
+
attachChannelToResult,
|
|
3
|
+
createAttachedChannelResultAdapter,
|
|
4
|
+
} from "autobot/plugin-sdk/channel-send-result";
|
|
5
|
+
import { resolveOutboundSendDep } from "autobot/plugin-sdk/outbound-send-deps";
|
|
6
|
+
import {
|
|
7
|
+
resolvePayloadMediaUrls,
|
|
8
|
+
resolveTextChunksWithFallback,
|
|
9
|
+
sendPayloadMediaSequence,
|
|
10
|
+
} from "autobot/plugin-sdk/reply-payload";
|
|
11
|
+
import { chunkTextForOutbound, type ChannelOutboundAdapter } from "../runtime-api.js";
|
|
12
|
+
import { createMSTeamsPollStoreFs } from "./polls.js";
|
|
13
|
+
import { buildMSTeamsPresentationCard, MSTEAMS_PRESENTATION_CAPABILITIES } from "./presentation.js";
|
|
14
|
+
import { sendAdaptiveCardMSTeams, sendMessageMSTeams, sendPollMSTeams } from "./send.js";
|
|
15
|
+
|
|
16
|
+
function asObjectRecord(value: unknown): Record<string, unknown> | undefined {
|
|
17
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
18
|
+
? (value as Record<string, unknown>)
|
|
19
|
+
: undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const MSTEAMS_TEXT_CHUNK_LIMIT = 4000;
|
|
23
|
+
|
|
24
|
+
export const msteamsOutbound: ChannelOutboundAdapter = {
|
|
25
|
+
deliveryMode: "direct",
|
|
26
|
+
chunker: chunkTextForOutbound,
|
|
27
|
+
chunkerMode: "markdown",
|
|
28
|
+
textChunkLimit: MSTEAMS_TEXT_CHUNK_LIMIT,
|
|
29
|
+
pollMaxOptions: 12,
|
|
30
|
+
deliveryCapabilities: {
|
|
31
|
+
durableFinal: {
|
|
32
|
+
text: true,
|
|
33
|
+
media: true,
|
|
34
|
+
payload: true,
|
|
35
|
+
messageSendingHooks: true,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
presentationCapabilities: MSTEAMS_PRESENTATION_CAPABILITIES,
|
|
39
|
+
renderPresentation: ({ payload, presentation }) => {
|
|
40
|
+
if (payload.mediaUrl || payload.mediaUrls?.length) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const card = buildMSTeamsPresentationCard({
|
|
44
|
+
presentation,
|
|
45
|
+
text: payload.text,
|
|
46
|
+
});
|
|
47
|
+
const msteamsData = asObjectRecord(payload.channelData?.msteams) ?? {};
|
|
48
|
+
return {
|
|
49
|
+
...payload,
|
|
50
|
+
channelData: {
|
|
51
|
+
...payload.channelData,
|
|
52
|
+
msteams: {
|
|
53
|
+
...msteamsData,
|
|
54
|
+
presentationCard: card,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
sendPayload: async ({
|
|
60
|
+
cfg,
|
|
61
|
+
to,
|
|
62
|
+
text,
|
|
63
|
+
mediaUrl,
|
|
64
|
+
mediaLocalRoots,
|
|
65
|
+
mediaReadFile,
|
|
66
|
+
payload,
|
|
67
|
+
deps,
|
|
68
|
+
}) => {
|
|
69
|
+
const msteamsData = asObjectRecord(payload.channelData?.msteams);
|
|
70
|
+
const presentationCard = msteamsData?.presentationCard;
|
|
71
|
+
if (
|
|
72
|
+
presentationCard &&
|
|
73
|
+
typeof presentationCard === "object" &&
|
|
74
|
+
!Array.isArray(presentationCard)
|
|
75
|
+
) {
|
|
76
|
+
const result = await sendAdaptiveCardMSTeams({
|
|
77
|
+
cfg,
|
|
78
|
+
to,
|
|
79
|
+
card: presentationCard as Record<string, unknown>,
|
|
80
|
+
});
|
|
81
|
+
return attachChannelToResult("msteams", result);
|
|
82
|
+
}
|
|
83
|
+
const mediaUrls = resolvePayloadMediaUrls({
|
|
84
|
+
...payload,
|
|
85
|
+
mediaUrl: payload.mediaUrl ?? mediaUrl,
|
|
86
|
+
})
|
|
87
|
+
.map((url) => url.trim())
|
|
88
|
+
.filter(Boolean);
|
|
89
|
+
if (mediaUrls.length > 0) {
|
|
90
|
+
type SendFn = (
|
|
91
|
+
to: string,
|
|
92
|
+
text: string,
|
|
93
|
+
opts?: {
|
|
94
|
+
mediaUrl?: string;
|
|
95
|
+
mediaLocalRoots?: readonly string[];
|
|
96
|
+
mediaReadFile?: (filePath: string) => Promise<Buffer>;
|
|
97
|
+
},
|
|
98
|
+
) => Promise<{ messageId: string; conversationId: string }>;
|
|
99
|
+
const send =
|
|
100
|
+
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
|
|
101
|
+
((to, text, opts) =>
|
|
102
|
+
sendMessageMSTeams({
|
|
103
|
+
cfg,
|
|
104
|
+
to,
|
|
105
|
+
text,
|
|
106
|
+
mediaUrl: opts?.mediaUrl,
|
|
107
|
+
mediaLocalRoots: opts?.mediaLocalRoots,
|
|
108
|
+
mediaReadFile: opts?.mediaReadFile,
|
|
109
|
+
}));
|
|
110
|
+
const result = await sendPayloadMediaSequence({
|
|
111
|
+
text,
|
|
112
|
+
mediaUrls,
|
|
113
|
+
send: async ({ text, mediaUrl }) =>
|
|
114
|
+
await send(to, text, { mediaUrl, mediaLocalRoots, mediaReadFile }),
|
|
115
|
+
});
|
|
116
|
+
if (result) {
|
|
117
|
+
return attachChannelToResult("msteams", result);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (text.trim()) {
|
|
121
|
+
type SendFn = (
|
|
122
|
+
to: string,
|
|
123
|
+
text: string,
|
|
124
|
+
) => Promise<{ messageId: string; conversationId: string }>;
|
|
125
|
+
const send =
|
|
126
|
+
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
|
|
127
|
+
((to, text) => sendMessageMSTeams({ cfg, to, text }));
|
|
128
|
+
const chunks = resolveTextChunksWithFallback(
|
|
129
|
+
text,
|
|
130
|
+
chunkTextForOutbound(text, MSTEAMS_TEXT_CHUNK_LIMIT),
|
|
131
|
+
);
|
|
132
|
+
let result: Awaited<ReturnType<SendFn>>;
|
|
133
|
+
for (const chunk of chunks) {
|
|
134
|
+
result = await send(to, chunk);
|
|
135
|
+
}
|
|
136
|
+
return attachChannelToResult("msteams", result!);
|
|
137
|
+
}
|
|
138
|
+
throw new Error("MS Teams payload send requires text, media, or a presentation card.");
|
|
139
|
+
},
|
|
140
|
+
...createAttachedChannelResultAdapter({
|
|
141
|
+
channel: "msteams",
|
|
142
|
+
sendText: async ({ cfg, to, text, deps }) => {
|
|
143
|
+
type SendFn = (
|
|
144
|
+
to: string,
|
|
145
|
+
text: string,
|
|
146
|
+
) => Promise<{ messageId: string; conversationId: string }>;
|
|
147
|
+
const send =
|
|
148
|
+
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
|
|
149
|
+
((to, text) => sendMessageMSTeams({ cfg, to, text }));
|
|
150
|
+
return await send(to, text);
|
|
151
|
+
},
|
|
152
|
+
sendMedia: async ({ cfg, to, text, mediaUrl, mediaLocalRoots, mediaReadFile, deps }) => {
|
|
153
|
+
type SendFn = (
|
|
154
|
+
to: string,
|
|
155
|
+
text: string,
|
|
156
|
+
opts?: {
|
|
157
|
+
mediaUrl?: string;
|
|
158
|
+
mediaLocalRoots?: readonly string[];
|
|
159
|
+
mediaReadFile?: (filePath: string) => Promise<Buffer>;
|
|
160
|
+
},
|
|
161
|
+
) => Promise<{ messageId: string; conversationId: string }>;
|
|
162
|
+
const send =
|
|
163
|
+
resolveOutboundSendDep<SendFn>(deps, "msteams") ??
|
|
164
|
+
((to, text, opts) =>
|
|
165
|
+
sendMessageMSTeams({
|
|
166
|
+
cfg,
|
|
167
|
+
to,
|
|
168
|
+
text,
|
|
169
|
+
mediaUrl: opts?.mediaUrl,
|
|
170
|
+
mediaLocalRoots: opts?.mediaLocalRoots,
|
|
171
|
+
mediaReadFile: opts?.mediaReadFile,
|
|
172
|
+
}));
|
|
173
|
+
return await send(to, text, { mediaUrl, mediaLocalRoots, mediaReadFile });
|
|
174
|
+
},
|
|
175
|
+
sendPoll: async ({ cfg, to, poll }) => {
|
|
176
|
+
const maxSelections = poll.maxSelections ?? 1;
|
|
177
|
+
const result = await sendPollMSTeams({
|
|
178
|
+
cfg,
|
|
179
|
+
to,
|
|
180
|
+
question: poll.question,
|
|
181
|
+
options: poll.options,
|
|
182
|
+
maxSelections,
|
|
183
|
+
});
|
|
184
|
+
const pollStore = createMSTeamsPollStoreFs();
|
|
185
|
+
await pollStore.createPoll({
|
|
186
|
+
id: result.pollId,
|
|
187
|
+
question: poll.question,
|
|
188
|
+
options: poll.options,
|
|
189
|
+
maxSelections,
|
|
190
|
+
createdAt: new Date().toISOString(),
|
|
191
|
+
conversationId: result.conversationId,
|
|
192
|
+
messageId: result.messageId,
|
|
193
|
+
votes: {},
|
|
194
|
+
});
|
|
195
|
+
return result;
|
|
196
|
+
},
|
|
197
|
+
}),
|
|
198
|
+
};
|