@matimo/microsoft 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.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * ms_send_teams_message
3
+ * New message: POST /teams/{team-id}/channels/{channel-id}/messages
4
+ * https://learn.microsoft.com/en-us/graph/api/channel-post-messages
5
+ * Reply: POST /teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies
6
+ * https://learn.microsoft.com/en-us/graph/api/chatmessage-post-replies
7
+ */
8
+ import { MatimoError, ErrorCode } from '@matimo/core';
9
+ import { getAccessToken, requireParams, graphRequest, type ToolContext } from '../graph-client';
10
+
11
+ const VALID_CONTENT_TYPES = ['text', 'html'];
12
+
13
+ interface ChannelMessage {
14
+ id?: string;
15
+ webUrl?: string;
16
+ createdDateTime?: string;
17
+ }
18
+
19
+ export default async function execute(
20
+ params: Record<string, unknown>,
21
+ context?: ToolContext
22
+ ): Promise<unknown> {
23
+ requireParams(params, ['team_id', 'channel_id', 'text'], 'ms_send_teams_message');
24
+
25
+ const teamId = String(params.team_id);
26
+ const channelId = String(params.channel_id);
27
+ const text = String(params.text);
28
+
29
+ const contentType = params.content_type === undefined ? 'text' : String(params.content_type);
30
+ if (!VALID_CONTENT_TYPES.includes(contentType)) {
31
+ throw new MatimoError(
32
+ `ms_send_teams_message: 'content_type' must be one of ${VALID_CONTENT_TYPES.join(', ')} (received '${contentType}')`,
33
+ ErrorCode.VALIDATION_FAILED,
34
+ { content_type: params.content_type }
35
+ );
36
+ }
37
+
38
+ const replyToMessageId =
39
+ typeof params.reply_to_message_id === 'string' && params.reply_to_message_id
40
+ ? params.reply_to_message_id
41
+ : undefined;
42
+
43
+ const token = getAccessToken(context);
44
+
45
+ const basePath = `/teams/${encodeURIComponent(teamId)}/channels/${encodeURIComponent(channelId)}/messages`;
46
+ const path = replyToMessageId
47
+ ? `${basePath}/${encodeURIComponent(replyToMessageId)}/replies`
48
+ : basePath;
49
+
50
+ const message = await graphRequest<ChannelMessage>({
51
+ method: 'POST',
52
+ path,
53
+ token,
54
+ resourceType: 'Teams channel',
55
+ body: {
56
+ body: {
57
+ contentType,
58
+ content: text,
59
+ },
60
+ },
61
+ });
62
+
63
+ return {
64
+ success: true,
65
+ message_id: message?.id ?? '',
66
+ web_url: message?.webUrl ?? '',
67
+ created_at: message?.createdDateTime ?? '',
68
+ };
69
+ }