Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: fix music for tiktok

Nevo David25b478871201
diff --git a/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx b/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx
index 9caa44e99..d13007a40 100644
--- a/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx
+++ b/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx
@@ -8,6 +8,7 @@ import clsx from 'clsx';
interface MusicResult {
id: string;
+ commercialMusicId: string;
title: string;
artist: string;
image: string;
@@ -17,6 +18,7 @@ interface MusicResult {
interface SelectedMusic {
id: string;
+ commercialMusicId?: string;
title?: string;
artist?: string;
image?: string;
@@ -157,6 +159,7 @@ export const TikTokMusicSelector: FC<{
setOpen(false);
emit({
id: track.id,
+ commercialMusicId: track.commercialMusicId,
title: track.title,
artist: track.artist,
image: track.image,
diff --git a/libraries/nestjs-libraries/src/dtos/posts/providers-settings/tiktok.dto.ts b/libraries/nestjs-libraries/src/dtos/posts/providers-settings/tiktok.dto.ts
index f5be6b0c7..e46e9c908 100644
--- a/libraries/nestjs-libraries/src/dtos/posts/providers-settings/tiktok.dto.ts
+++ b/libraries/nestjs-libraries/src/dtos/posts/providers-settings/tiktok.dto.ts
@@ -13,6 +13,14 @@ export class TikTokMusic {
})
id: string;
+ @IsOptional()
+ @IsString()
+ @JSONSchema({
+ description:
+ 'The "commercialMusicId" returned by the musicSearch function. Always include it when available - photo posts publish with this id, not with "id".',
+ })
+ commercialMusicId?: string;
+
@IsOptional()
@IsString()
title?: string;
diff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts
index 42bcea508..1e7554f82 100644
--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts
@@ -27,7 +27,7 @@ import { Tool } from '@gitroom/nestjs-libraries/integrations/tool.decorator';
'content_posting_method=DIRECT_POST publishes the post to the account. content_posting_method=UPLOAD does NOT publish: it only saves the media as a draft in the user inbox of the TikTok app, where the user must manually complete and publish it. Use DIRECT_POST unless the user explicitly asks to review or edit the post inside the TikTok app first.',
'With content_posting_method=UPLOAD, TikTok ignores every setting except the title / post content. Never tell the user that video_made_with_ai, privacy_level, duet, stitch, comment, autoAddMusic, brand_content_toggle or brand_organic_toggle will be applied in UPLOAD mode - they are silently discarded. If the user asks for any of those settings, tell them it requires DIRECT_POST.',
'video_made_with_ai, duet and stitch apply to video posts only. privacy_level and autoAddMusic apply to photo posts only - the TikTok Business API has no privacy field for video posts, so those settings are discarded when the attachment is a video.',
- 'The music setting attaches a commercial music library track (found via the musicSearch function) to a video or photo post, and the location setting tags the post with a location (found via the locationSearch function). Both apply only with content_posting_method=DIRECT_POST. music.audio_volume / music.video_volume (0-100, default 50) apply to video posts only. For photos, autoAddMusic=yes attaches a RANDOM commercial music library track and overrides any music selection - use autoAddMusic=no when the user wants a specific track.',
+ 'The music setting attaches a commercial music library track (found via the musicSearch function) to a video or photo post, and the location setting tags the post with a location (found via the locationSearch function). Both apply only with content_posting_method=DIRECT_POST. When setting music, copy both "id" and "commercialMusicId" from the musicSearch result - photo posts publish with commercialMusicId. music.audio_volume / music.video_volume (0-100, default 50) apply to video posts only. For photos, autoAddMusic=yes attaches a RANDOM commercial music library track and overrides any music selection - use autoAddMusic=no when the user wants a specific track.',
'Media is pulled by TikTok from its URL, so the media must be uploaded to Postiz and the media domain must be a verified URL property of the TikTok Business app.',
].join(' ')
)
@@ -591,11 +591,40 @@ export class TiktokBusinessProvider
accessToken: string,
businessId: string
): Promise<string | undefined> {
- const tracks = await this.musicSearch(accessToken, {}, businessId);
+ // Photos publish with commercial_music_id only - a track without one
+ // returns undefined so the photo body falls back to auto_add_music.
+ const tracks = (await this.musicSearch(accessToken, {}, businessId)).filter(
+ (track: { commercialMusicId?: string }) => track.commercialMusicId
+ );
if (!tracks.length) {
return undefined;
}
- return tracks[Math.floor(Math.random() * tracks.length)].id;
+ return tracks[Math.floor(Math.random() * tracks.length)].commercialMusicId;
+ }
+
+ // The photo endpoint expects the commercial_music_id - passing the
+ // song_clip_id the video endpoint uses fails the publish with TikTok's
+ // generic 51065 "Something is wrong" error. Selections saved before
+ // commercialMusicId existed only carry the song_clip_id, so resolve those
+ // back through the trending list.
+ private async resolvePhotoMusicId(
+ accessToken: string,
+ businessId: string,
+ music: TikTokDto['music']
+ ): Promise<string | undefined> {
+ if (music?.commercialMusicId) {
+ return music.commercialMusicId;
+ }
+ if (!music?.id) {
+ return undefined;
+ }
+ const tracks = await this.musicSearch(accessToken, {}, businessId);
+ return (
+ tracks.find(
+ (track: { id: string; commercialMusicId?: string }) =>
+ track.id === music.id
+ )?.commercialMusicId || music.id
+ );
}
private async buildPhotoBody(
@@ -608,12 +637,15 @@ export class TiktokBusinessProvider
// Random music wins over an explicitly selected track: the UI hides the
// music selector while "add random music" is on, so a stale selection must
// not silently override it.
- const musicId =
- firstPost.settings.autoAddMusic === 'yes'
- ? !isDraft
- ? await this.pickRandomMusicId(accessToken, businessId)
- : undefined
- : firstPost.settings.music?.id;
+ const musicId = isDraft
+ ? undefined
+ : firstPost.settings.autoAddMusic === 'yes'
+ ? await this.pickRandomMusicId(accessToken, businessId)
+ : await this.resolvePhotoMusicId(
+ accessToken,
+ businessId,
+ firstPost.settings.music
+ );
return {
business_id: businessId,
@@ -833,11 +865,13 @@ export class TiktokBusinessProvider
return (music?.data?.list || [])
.map((track: any) => ({
- // song_clip_id is the value music_sound_id expects; older responses
- // only carry commercial_music_id, which TikTok accepts as well.
+ // song_clip_id is the value the video music_sound_id expects; older
+ // responses only carry commercial_music_id, which TikTok accepts as
+ // well. The photo music_sound_id expects commercial_music_id instead.
id:
track?.full_duration_song_clip?.song_clip_id ||
track?.commercial_music_id,
+ commercialMusicId: track?.commercial_music_id || '',
title: track?.commercial_music_name || '',
artist: track?.artist || '',
image: track?.thumbnail_url || '',