Skip to content

Commit 0576148

Browse files
committed
fix for bots
1 parent 272b989 commit 0576148

File tree

3 files changed

+111
-71
lines changed

3 files changed

+111
-71
lines changed

apps/sim/app/api/tools/microsoft_teams/write_channel/route.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { checkHybridAuth } from '@/lib/auth/hybrid'
44
import { createLogger } from '@/lib/logs/console/logger'
55
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
66
import { generateRequestId } from '@/lib/utils'
7-
import { resolveMentionsForChannel } from '@/tools/microsoft_teams/utils'
7+
import { resolveMentionsForChannel, type TeamsMention } from '@/tools/microsoft_teams/utils'
88

99
export const dynamic = 'force-dynamic'
1010

@@ -142,11 +142,7 @@ export async function POST(request: NextRequest) {
142142

143143
let messageContent = validatedData.content
144144
let contentType: 'text' | 'html' = 'text'
145-
const mentionEntities: Array<{
146-
type: 'mention'
147-
mentioned: { id: string; name: string }
148-
text: string
149-
}> = []
145+
const mentionEntities: TeamsMention[] = []
150146

151147
try {
152148
const mentionResult = await resolveMentionsForChannel(
@@ -158,8 +154,9 @@ export async function POST(request: NextRequest) {
158154

159155
if (mentionResult.hasMentions) {
160156
contentType = 'html'
161-
mentionEntities.push(...mentionResult.entities)
162-
logger.info(`[${requestId}] Resolved ${mentionResult.entities.length} mention(s)`)
157+
messageContent = mentionResult.updatedContent
158+
mentionEntities.push(...mentionResult.mentions)
159+
logger.info(`[${requestId}] Resolved ${mentionResult.mentions.length} mention(s)`)
163160
}
164161
} catch (error) {
165162
logger.warn(`[${requestId}] Failed to resolve mentions, continuing without them:`, error)
@@ -170,7 +167,7 @@ export async function POST(request: NextRequest) {
170167
const attachmentTags = attachments
171168
.map((att) => `<attachment id="${att.id}"></attachment>`)
172169
.join(' ')
173-
messageContent = `${validatedData.content}<br/>${attachmentTags}`
170+
messageContent = `${messageContent}<br/>${attachmentTags}`
174171
}
175172

176173
const messageBody: {
@@ -179,11 +176,7 @@ export async function POST(request: NextRequest) {
179176
content: string
180177
}
181178
attachments?: any[]
182-
mentions?: Array<{
183-
type: 'mention'
184-
mentioned: { id: string; name: string }
185-
text: string
186-
}>
179+
mentions?: TeamsMention[]
187180
} = {
188181
body: {
189182
contentType,

apps/sim/app/api/tools/microsoft_teams/write_chat/route.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { checkHybridAuth } from '@/lib/auth/hybrid'
44
import { createLogger } from '@/lib/logs/console/logger'
55
import { downloadFileFromStorage, processFilesToUserFiles } from '@/lib/uploads/file-processing'
66
import { generateRequestId } from '@/lib/utils'
7-
import { resolveMentionsForChat } from '@/tools/microsoft_teams/utils'
7+
import { resolveMentionsForChat, type TeamsMention } from '@/tools/microsoft_teams/utils'
88

99
export const dynamic = 'force-dynamic'
1010

@@ -140,11 +140,7 @@ export async function POST(request: NextRequest) {
140140

141141
let messageContent = validatedData.content
142142
let contentType: 'text' | 'html' = 'text'
143-
const mentionEntities: Array<{
144-
type: 'mention'
145-
mentioned: { id: string; name: string }
146-
text: string
147-
}> = []
143+
const mentionEntities: TeamsMention[] = []
148144

149145
try {
150146
const mentionResult = await resolveMentionsForChat(
@@ -155,8 +151,9 @@ export async function POST(request: NextRequest) {
155151

156152
if (mentionResult.hasMentions) {
157153
contentType = 'html'
158-
mentionEntities.push(...mentionResult.entities)
159-
logger.info(`[${requestId}] Resolved ${mentionResult.entities.length} mention(s)`)
154+
messageContent = mentionResult.updatedContent
155+
mentionEntities.push(...mentionResult.mentions)
156+
logger.info(`[${requestId}] Resolved ${mentionResult.mentions.length} mention(s)`)
160157
}
161158
} catch (error) {
162159
logger.warn(`[${requestId}] Failed to resolve mentions, continuing without them:`, error)
@@ -167,7 +164,7 @@ export async function POST(request: NextRequest) {
167164
const attachmentTags = attachments
168165
.map((att) => `<attachment id="${att.id}"></attachment>`)
169166
.join(' ')
170-
messageContent = `${validatedData.content}<br/>${attachmentTags}`
167+
messageContent = `${messageContent}<br/>${attachmentTags}`
171168
}
172169

173170
const messageBody: {
@@ -176,11 +173,7 @@ export async function POST(request: NextRequest) {
176173
content: string
177174
}
178175
attachments?: any[]
179-
mentions?: Array<{
180-
type: 'mention'
181-
mentioned: { id: string; name: string }
182-
text: string
183-
}>
176+
mentions?: TeamsMention[]
184177
} = {
185178
body: {
186179
contentType,

apps/sim/tools/microsoft_teams/utils.ts

Lines changed: 97 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const logger = createLogger('MicrosoftTeamsUtils')
77
interface ParsedMention {
88
name: string
99
fullTag: string
10-
startIndex: number
11-
endIndex: number
10+
mentionId: number
1211
}
1312

1413
interface TeamMember {
@@ -17,13 +16,22 @@ interface TeamMember {
1716
userIdentityType?: string
1817
}
1918

20-
export interface MentionEntity {
21-
type: 'mention'
22-
mentioned: {
23-
id: string
24-
name: string
25-
}
26-
text: string
19+
export interface TeamsMention {
20+
id: number
21+
mentionText: string
22+
mentioned:
23+
| {
24+
id: string
25+
name: string
26+
userIdentityType?: string
27+
}
28+
| {
29+
application: {
30+
displayName: string
31+
id: string
32+
applicationIdentityType: 'bot'
33+
}
34+
}
2735
}
2836

2937
/**
@@ -126,15 +134,15 @@ function parseMentions(content: string): ParsedMention[] {
126134
const mentions: ParsedMention[] = []
127135
const mentionRegex = /<at>([^<]+)<\/at>/gi
128136
let match: RegExpExecArray | null
137+
let mentionId = 0
129138

130139
while ((match = mentionRegex.exec(content)) !== null) {
131140
const name = match[1].trim()
132141
if (name) {
133142
mentions.push({
134143
name,
135144
fullTag: match[0],
136-
startIndex: match.index,
137-
endIndex: match.index + match[0].length,
145+
mentionId: mentionId++,
138146
})
139147
}
140148
}
@@ -201,40 +209,63 @@ export async function resolveMentionsForChat(
201209
content: string,
202210
chatId: string,
203211
accessToken: string
204-
): Promise<{ entities: MentionEntity[]; hasMentions: boolean }> {
212+
): Promise<{ mentions: TeamsMention[]; hasMentions: boolean; updatedContent: string }> {
205213
const parsedMentions = parseMentions(content)
206214

207215
if (parsedMentions.length === 0) {
208-
return { entities: [], hasMentions: false }
216+
return { mentions: [], hasMentions: false, updatedContent: content }
209217
}
210218

211219
const members = await fetchChatMembers(chatId, accessToken)
212-
const entities: MentionEntity[] = []
213-
const resolvedNames = new Set<string>()
220+
const mentions: TeamsMention[] = []
221+
const resolvedTags = new Set<string>()
222+
let updatedContent = content
214223

215224
for (const mention of parsedMentions) {
216-
if (resolvedNames.has(mention.fullTag)) {
225+
if (resolvedTags.has(mention.fullTag)) {
217226
continue
218227
}
219228

220229
const member = findMemberByName(members, mention.name)
221230

222231
if (member) {
223-
entities.push({
224-
type: 'mention',
225-
mentioned: {
226-
id: member.id,
227-
name: member.displayName,
228-
},
229-
text: mention.fullTag,
230-
})
231-
resolvedNames.add(mention.fullTag)
232+
const isBot = member.userIdentityType === 'bot'
233+
234+
if (isBot) {
235+
mentions.push({
236+
id: mention.mentionId,
237+
mentionText: mention.name,
238+
mentioned: {
239+
application: {
240+
displayName: member.displayName,
241+
id: member.id,
242+
applicationIdentityType: 'bot',
243+
},
244+
},
245+
})
246+
} else {
247+
mentions.push({
248+
id: mention.mentionId,
249+
mentionText: mention.name,
250+
mentioned: {
251+
id: member.id,
252+
name: member.displayName,
253+
userIdentityType: member.userIdentityType,
254+
},
255+
})
256+
}
257+
resolvedTags.add(mention.fullTag)
258+
updatedContent = updatedContent.replace(
259+
mention.fullTag,
260+
`<at id="${mention.mentionId}">${mention.name}</at>`
261+
)
232262
}
233263
}
234264

235265
return {
236-
entities,
237-
hasMentions: entities.length > 0,
266+
mentions,
267+
hasMentions: mentions.length > 0,
268+
updatedContent,
238269
}
239270
}
240271

@@ -243,39 +274,62 @@ export async function resolveMentionsForChannel(
243274
teamId: string,
244275
channelId: string,
245276
accessToken: string
246-
): Promise<{ entities: MentionEntity[]; hasMentions: boolean }> {
277+
): Promise<{ mentions: TeamsMention[]; hasMentions: boolean; updatedContent: string }> {
247278
const parsedMentions = parseMentions(content)
248279

249280
if (parsedMentions.length === 0) {
250-
return { entities: [], hasMentions: false }
281+
return { mentions: [], hasMentions: false, updatedContent: content }
251282
}
252283

253284
const members = await fetchChannelMembers(teamId, channelId, accessToken)
254-
const entities: MentionEntity[] = []
255-
const resolvedNames = new Set<string>()
285+
const mentions: TeamsMention[] = []
286+
const resolvedTags = new Set<string>()
287+
let updatedContent = content
256288

257289
for (const mention of parsedMentions) {
258-
if (resolvedNames.has(mention.fullTag)) {
290+
if (resolvedTags.has(mention.fullTag)) {
259291
continue
260292
}
261293

262294
const member = findMemberByName(members, mention.name)
263295

264296
if (member) {
265-
entities.push({
266-
type: 'mention',
267-
mentioned: {
268-
id: member.id,
269-
name: member.displayName,
270-
},
271-
text: mention.fullTag,
272-
})
273-
resolvedNames.add(mention.fullTag)
297+
const isBot = member.userIdentityType === 'bot'
298+
299+
if (isBot) {
300+
mentions.push({
301+
id: mention.mentionId,
302+
mentionText: mention.name,
303+
mentioned: {
304+
application: {
305+
displayName: member.displayName,
306+
id: member.id,
307+
applicationIdentityType: 'bot',
308+
},
309+
},
310+
})
311+
} else {
312+
mentions.push({
313+
id: mention.mentionId,
314+
mentionText: mention.name,
315+
mentioned: {
316+
id: member.id,
317+
name: member.displayName,
318+
userIdentityType: member.userIdentityType,
319+
},
320+
})
321+
}
322+
resolvedTags.add(mention.fullTag)
323+
updatedContent = updatedContent.replace(
324+
mention.fullTag,
325+
`<at id="${mention.mentionId}">${mention.name}</at>`
326+
)
274327
}
275328
}
276329

277330
return {
278-
entities,
279-
hasMentions: entities.length > 0,
331+
mentions,
332+
hasMentions: mentions.length > 0,
333+
updatedContent,
280334
}
281335
}

0 commit comments

Comments
 (0)