Skip to content

Commit 13949e8

Browse files
author
Deathgiver
committed
fix(tiktok): store and use conversation_id as recipient for outgoing messages
Add sourceConversationId field to IncomingContact/OutgoingContact and ContactInbox schema, populate it from TikTok's conversation_id on incoming messages, and use it (falling back to sourceId) when sending messages via TikTok Business API.
1 parent b33af22 commit 13949e8

11 files changed

Lines changed: 23191 additions & 15 deletions

File tree

apps/worker/src/integration/handlers/received-message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ const detectContactAndConversation = async (props: {
383383
contactId: newContact.id,
384384
originalContactId: newContact.id,
385385
source: inbox.channel,
386+
sourceConversationId: incomingContact.sourceConversationId,
386387
sourceId: incomingContact.sourceId,
387388
channel: inbox.channel,
388389
})

integrations/tiktok/src/handlers/message/incoming-message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const receiveMessage = async ({
8787

8888
const contact: IncomingContact = {
8989
sourceId: customerOpenId,
90+
sourceConversationId: content.conversation_id,
9091
firstName: isEcho
9192
? (content.to ?? content.to_user?.id ?? content.from_user.id)
9293
: (content.from ?? content.from_user.id),

integrations/tiktok/src/handlers/message/outgoing-message/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const sendMessage: MessageHandlers<TiktokAuthValue>["sendMessage"] =
1919
} = props
2020

2121
const businessId = ctx.auth.metadata.openId
22-
const conversationId = contact.sourceId
22+
const conversationId = contact.sourceConversationId ?? contact.sourceId
2323
const messageIds: string[] = []
2424

2525
try {
@@ -69,7 +69,7 @@ export const sendFlowStep: MessageHandlers<TiktokAuthValue>["sendFlowStep"] =
6969
} = props
7070

7171
const businessId = ctx.auth.metadata.openId
72-
const conversationId = contact.sourceId
72+
const conversationId = contact.sourceConversationId ?? contact.sourceId
7373
const messageIds: string[] = []
7474

7575
try {

integrations/tiktok/src/handlers/message/outgoing-message/send-text.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function* convertFlowStepText(
1515
yield {
1616
business_id: businessId,
1717
recipient_type: "CONVERSATION",
18-
recipient: contact.sourceId,
18+
recipient: contact.sourceConversationId ?? contact.sourceId,
1919
message_type: "TEXT",
2020
text: { body: step.text },
2121
}
@@ -33,7 +33,7 @@ export function* convertFlowStepText(
3333
yield {
3434
business_id: businessId,
3535
recipient_type: "CONVERSATION",
36-
recipient: contact.sourceId,
36+
recipient: contact.sourceConversationId ?? contact.sourceId,
3737
message_type: "TEMPLATE",
3838
template,
3939
}

packages/database/drizzle/20260609132605_create_integration_mailchimp_table/snapshot.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
"version": "8",
33
"dialect": "postgres",
44
"id": "00ec001e-ef94-4702-afde-69c8fba361c5",
5-
"prevIds": [
6-
"2837a2eb-228b-4d67-9094-c00454c0ed1b",
7-
"351fc50a-eea1-42ef-b92f-193cf6838d66",
8-
"c3f29eb6-e631-4178-8eaf-342854b85a0e"
9-
],
5+
"prevIds": ["c744036c-a1a7-46d5-9952-109e12fc83a3"],
106
"ddl": [
117
{
128
"values": ["pending", "success", "error", "processing"],

packages/database/drizzle/20260610051033_nullable_file_workspace_id/snapshot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "8",
33
"dialect": "postgres",
44
"id": "42a183eb-a770-4d7e-912b-81ce54d3aa5e",
5-
"prevIds": ["c744036c-a1a7-46d5-9952-109e12fc83a3"],
5+
"prevIds": ["00ec001e-ef94-4702-afde-69c8fba361c5"],
66
"ddl": [
77
{
88
"values": ["pending", "success", "error", "processing"],

packages/database/drizzle/20260610084543_drop_contact_last_activity_at/snapshot.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
"version": "8",
33
"dialect": "postgres",
44
"id": "42b9360b-d40a-4c49-b18f-df5ec19f9e88",
5-
"prevIds": [
6-
"2837a2eb-228b-4d67-9094-c00454c0ed1b",
7-
"351fc50a-eea1-42ef-b92f-193cf6838d66",
8-
"c3f29eb6-e631-4178-8eaf-342854b85a0e"
9-
],
5+
"prevIds": ["42a183eb-a770-4d7e-912b-81ce54d3aa5e"],
106
"ddl": [
117
{
128
"values": ["pending", "success", "error", "processing"],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "ContactInbox" ADD COLUMN "sourceConversationId" text;--> statement-breakpoint

0 commit comments

Comments
 (0)