@@ -111,6 +111,10 @@ function whatsAppReplyDeliveryVisibilityFromDurableResult(result: {
|
111 | 111 | return whatsAppReplyDeliveryVisibility(result.visibleReplySent === true); |
112 | 112 | } |
113 | 113 | |
| 114 | +function readTrimmedString(value: unknown): string { |
| 115 | +return typeof value === "string" ? value.trim() : ""; |
| 116 | +} |
| 117 | + |
114 | 118 | function markWhatsAppReplyDeliveryErrorVisibleAfterFlush( |
115 | 119 | error: unknown, |
116 | 120 | flushResult: WhatsAppMediaOnlyFlushResult, |
@@ -141,6 +145,29 @@ function logWhatsAppReplyDeliveryError(params: {
|
141 | 145 | ); |
142 | 146 | } |
143 | 147 | |
| 148 | +function resolveWhatsAppDurableReplyToId(params: { |
| 149 | +context: Record<string, unknown>; |
| 150 | +info: ReplyDeliveryInfo; |
| 151 | +msg: AdmittedWebInboundMessage; |
| 152 | +payload: DeliverableWhatsAppOutboundPayload<ReplyPayload>; |
| 153 | +}): string | null { |
| 154 | +if (params.payload.replyToId === null) { |
| 155 | +return null; |
| 156 | +} |
| 157 | +const explicitPayloadReplyToId = readTrimmedString(params.payload.replyToId); |
| 158 | +if (explicitPayloadReplyToId) { |
| 159 | +return explicitPayloadReplyToId; |
| 160 | +} |
| 161 | +const hasVisibleInboundReplyTarget = |
| 162 | +Boolean(readTrimmedString(params.context.ReplyToId)) || |
| 163 | +Boolean(readTrimmedString(params.context.ReplyToIdFull)); |
| 164 | +const currentInboundMessageId = readTrimmedString(params.msg.event.id); |
| 165 | +if (params.info.kind === "final" && hasVisibleInboundReplyTarget && currentInboundMessageId) { |
| 166 | +return currentInboundMessageId; |
| 167 | +} |
| 168 | +return null; |
| 169 | +} |
| 170 | + |
144 | 171 | function resolveWhatsAppDisableBlockStreaming(cfg: ReturnType<LoadConfigFn>): boolean | undefined { |
145 | 172 | if (typeof cfg.channels?.whatsapp?.blockStreaming !== "boolean") { |
146 | 173 | return undefined; |
@@ -691,7 +718,12 @@ export async function dispatchWhatsAppBufferedReply(params: {
|
691 | 718 | payload: normalizedDeliveryPayload, |
692 | 719 | info, |
693 | 720 | to: conversationId, |
694 | | -replyToId: normalizedDeliveryPayload.replyToId ?? null, |
| 721 | +replyToId: resolveWhatsAppDurableReplyToId({ |
| 722 | +context: params.context, |
| 723 | + info, |
| 724 | +msg: params.msg, |
| 725 | +payload: normalizedDeliveryPayload, |
| 726 | +}), |
695 | 727 | formatting: { |
696 | 728 | textLimit, |
697 | 729 | tableMode, |
|