Skip to content

Commit 123ed24

Browse files
testing comment replies
1 parent 33773f7 commit 123ed24

File tree

1 file changed

+30
-34
lines changed
  • src/app/(protected)/api/webhook/instagram

1 file changed

+30
-34
lines changed

src/app/(protected)/api/webhook/instagram/route.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -176,50 +176,46 @@ export async function POST(req: NextRequest) {
176176
webhook_payload.entry[0].changes[0].value
177177
);
178178

179+
// Handle both comment reply and DM in parallel
180+
const actions = [];
181+
179182
if (automation.listener.commentReply) {
180183
console.log("Replying to comment with template");
181-
182-
const comment_reply = await replyToComment(
183-
webhook_payload.entry[0].changes[0].value.id,
184-
automation.listener.commentReply,
185-
automation.User?.integrations[0].token!
184+
actions.push(
185+
replyToComment(
186+
webhook_payload.entry[0].changes[0].value.id,
187+
automation.listener.commentReply,
188+
automation.User?.integrations[0].token!
189+
)
186190
);
191+
}
187192

188-
if (comment_reply.status === 200) {
189-
const tracked = await trackResponses(automation.id, "COMMENT");
190-
if (tracked) {
191-
await trackAnalytics(automation.userId!, "comment").catch(console.error);
192-
return NextResponse.json(
193-
{
194-
message: "Comment reply sent",
195-
},
196-
{ status: 200 }
197-
);
198-
}
199-
}
200-
} else {
201-
console.log("Sending private message");
202-
const direct_message = await sendPrivateMessage(
193+
console.log("Sending private message");
194+
actions.push(
195+
sendPrivateMessage(
203196
webhook_payload.entry[0].id,
204197
webhook_payload.entry[0].changes[0].value.from.id,
205198
automation.listener?.prompt,
206199
automation.User?.integrations[0].token!
207-
);
200+
)
201+
);
208202

209-
if (direct_message.status === 200) {
210-
const tracked = await trackResponses(automation.id, "COMMENT");
203+
const results = await Promise.all(actions);
204+
const direct_message = results[actions.length - 1];
211205

212-
if (tracked) {
213-
await trackAnalytics(automation.userId!, "comment").catch(
214-
console.error
215-
);
216-
return NextResponse.json(
217-
{
218-
message: "Message sent",
219-
},
220-
{ status: 200 }
221-
);
222-
}
206+
if (direct_message.status === 200) {
207+
const tracked = await trackResponses(automation.id, "COMMENT");
208+
209+
if (tracked) {
210+
await trackAnalytics(automation.userId!, "comment").catch(
211+
console.error
212+
);
213+
return NextResponse.json(
214+
{
215+
message: "Messages sent successfully",
216+
},
217+
{ status: 200 }
218+
);
223219
}
224220
}
225221
}

0 commit comments

Comments
 (0)