Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions packages/bp/src/core/messaging/messaging-bot-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ export class MessagingBotRouter extends CustomRouter {
return res.sendStatus(404)
}

const [incomingEvent] = await this.eventRepo.findEvents({
incomingEventId: messageEvent.incomingEventId,
direction: 'incoming',
botId: req.params.botId
})
// If the matched event is already incoming, return it directly
if (messageEvent.direction === 'incoming') {
return res.send(messageEvent.event)
}

if (!incomingEvent) {
return res.sendStatus(404)
// For outgoing events with a linked incoming event, follow the chain
if (messageEvent.incomingEventId) {
const [incomingEvent] = await this.eventRepo.findEvents({
incomingEventId: messageEvent.incomingEventId,
direction: 'incoming',
botId: req.params.botId
})

if (incomingEvent) {
return res.send(incomingEvent.event)
}
}

return res.send(incomingEvent.event)
// Fallback: return the outgoing event itself (graceful degradation
// instead of 404 when incomingEventId is not set)
return res.send(messageEvent.event)
})
)

Expand Down