Skip to content

feat(tiktok): add messaging and comments switches to the app credential - #1572

Merged
sung17 merged 3 commits into
mainfrom
feat/tiktok-channel-features
Oct 28, 2026
Merged

sung17 merged 3 commits into
mainfrom
feat/tiktok-channel-features

Conversation

@sung17

@sung17 sung17 commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Closes #1563. Supersedes #1564; thanks to @rancaHoratiu for the report and the original proposal.

Why

TikTok's Business Messaging API is not available to accounts registered in the EEA, Switzerland or the UK, so they can never grant message.list.*. ChatbotX treated those scopes as required, and saving the credential failed whenever TikTok rejected the DIRECT_MESSAGE webhook. Those accounts therefore couldn't use comment automation either, even though it only needs the comment scopes.

#1564 proposed a deployment-wide TIKTOK_COMMENTS_ONLY env var. This PR makes the choice per credential instead, for two reasons:

What

  • Two switches on the TikTok app credential: Messaging and Comments (TiktokCredential.features). If unset, both are on, so existing credentials behave exactly as before. At least one must stay on (zod refine, and the UI disables the last switch that is on).
  • Scopes (integrations/tiktok/src/lib/scopes.ts):
    • tiktokRequestedScopes(features): the authorize request asks only for what the enabled features use. With Messaging off, no messaging scope is requested.
    • tiktokRequiredScopes(features): what the grant must contain. With Messaging off, comment.list + comment.list.manage are required instead of the messaging scopes.
    • assertTiktokGrant: one check now used by both connect paths (exchangeCode and callbackHandler). It stamps metadata.features on the connection.
  • Webhooks (subscribeTiktokWebhooks): only the enabled features are subscribed. With Messaging off, a failed COMMENT subscription fails the save. With both on, the behaviour is unchanged: DM failure is fatal, comment failure is best-effort.
  • Features that need messaging refuse early, with a clear error (tiktokCanMessage, read from the granted scopes):
    • the comment private reply (ChannelError permission_denied)
    • setCommentToMessage
  • Re-authorize warning: no longer shown for connections whose credential had Comments off.
  • UI: switches in the TikTok platform-credential dialog and a features line on the card. 6 new fields.tiktok.* keys in all 21 locales.
  • Docs: docs/comment-automation.md.

No migration: the credential config is encrypted JSON.

Public API: exempt under invariant 23, because this is a platform credential setting (platform administration).

Test plan

  • pnpm lint
  • check-types for all packages (pre-commit hook, 63/63)
  • @chatbotx.io/integration-tiktok: 139 tests. New cases: scopes in both modes, both connect paths with a comments-only grant, webhook subscription per feature, private-reply refusal.
  • business TikTok tests (20). New case: Comment-to-Message refusal.
  • builder TikTok tests (35). New cases: features stored and passed to the subscription, both-off rejected.
  • Manual: save the credential with Messaging off, then connect an EEA account and confirm comments arrive and can be replied to, liked and hidden.

🤖 Generated with Claude Code

TikTok's Business Messaging API is not available to accounts registered in
the EEA, Switzerland or the UK, so they can never grant message.list.*.
ChatbotX required those scopes, so such accounts could not connect at all,
not even for comment automation.

The TikTok app credential now carries two switches, Messaging and Comments
(absent = both on, at least one required). With Messaging off the authorize
request asks for no messaging scope, both connect paths require the comment
scopes instead, and saving subscribes only the COMMENT webhook. The private
reply and Comment-to-Message refuse early on a connection that cannot
message.

Closes #1563

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Deathgiver and others added 2 commits October 28, 2026 09:36
…ed scopes

Both token-refresh paths re-stamp metadata.scopes from TikTok's refresh
response, so an empty or partial answer there would have blocked the
private reply and Comment-to-Message on a working DM connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sung17

sung17 commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks again @rancaHoratiu for reporting #1563 and for the original proposal in #1564. Your write-up made the root cause easy to track down, and this PR builds directly on your idea.

The main difference is that comments-only is now a switch on each TikTok app credential instead of a server-wide env var, so EU and non-EU accounts can be served side by side. If you get a chance to try it with your EEA account (turn Messaging off in the TikTok credential settings, then connect), we'd love to hear whether comments come through as expected.

@sung17
sung17 merged commit 17ab82b into main Oct 28, 2026
14 checks passed
@rancaHoratiu

Copy link
Copy Markdown

Thanks @sung17, that was fast! The per-credential switches are exactly what we needed. Our TikTok app is still in review; I'll test with Messaging off and report back here once it's approved.

@rancaHoratiu

Copy link
Copy Markdown

Hey, it's ya boy again 👋 @sung17

Tested this on our EU account (Romania), self-hosted on 1.18.0, and comments work end to end:

  • TikTok approved our Accounts API app with only user.info.basic, video.list, comment.list, comment.list.manage (no Business Messaging), so comments-only approval without DM is possible
  • Credential with Messaging off and Comments on: the consent screen asks only for the identity, profile, comment and video scopes, no message.list.*, so no invalid_scope
  • Connected fine, no re-authorize warning
  • Keyword rule with public reply: the comment arrives via webhook, the reply is posted publicly, and the bot's own reply comes back as "skipping self-authored comment", so no loop

One bug in the connect flow, though. The OAuth state is encoded as base64url in integrations/tiktok/src/apis/auth.ts:

state: Buffer.from(JSON.stringify(stateParams ?? {})).toString("base64url")

but apps/builder/src/app/integrations/[...integration]/callback.ts decodes it with atob:

rawState = JSON.parse(atob(decodeURIComponent(rawStateParam)))

atob rejects - and _ ("Invalid character"), so the callback returns a 404 whenever the encoded state contains them. For TikTok that happens a lot, because the referer in features/integration-tiktok/libs/tiktok.ts is always .../settings/channels?channel=tiktok, and the ? turns into _ in base64url. For our workspace it failed every single time. Still the same on main.

Workaround we used: replace the _ in state with %2F in the callback URL and reload it before the auth code expires (otherwise TikTok answers "The request parameters are malformed" and you get a 500).

Possible fix: decode with Buffer.from(raw, "base64url"), which also accepts standard base64, or switch the TikTok state to plain base64 like the other integrations that use btoa.

Thanks for shipping the switches so quickly, this is exactly what we needed 🙏

@sung17

sung17 commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

@rancaHoratiu thanks a lot for the detailed test and the clear bug report 🙏

You're right: TikTok encodes the OAuth state as base64url, but the callback decoded it with atob, which rejects - and _. The fix is in #1654: the callback now decodes with Buffer.from(raw, "base64url"), which accepts both base64url and the standard base64 the other integrations use. It includes a regression test with a state containing -/_.

Once it's merged, the TikTok connect flow should work without the %2F workaround.

sung17 added a commit that referenced this pull request Nov 7, 2026
TikTok encodes the OAuth state as base64url, but the integrations
callback decoded it with atob, which rejects "-" and "_". Whenever the
encoded state contained them the callback returned 404, so connecting
TikTok failed for most workspaces. Decode with Node's base64url decoder,
which accepts both base64url and the standard base64 the other
integrations produce with btoa.

Reported in #1572 (comment 6650507443).

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
…al (#1572)

* feat(tiktok): add messaging and comments switches to the app credential

TikTok's Business Messaging API is not available to accounts registered in
the EEA, Switzerland or the UK, so they can never grant message.list.*.
ChatbotX required those scopes, so such accounts could not connect at all,
not even for comment automation.

The TikTok app credential now carries two switches, Messaging and Comments
(absent = both on, at least one required). With Messaging off the authorize
request asks for no messaging scope, both connect paths require the comment
scopes instead, and saving subscribes only the COMMENT webhook. The private
reply and Comment-to-Message refuse early on a connection that cannot
message.

Closes #1563

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(tiktok): default credential features to both on when omitted

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(tiktok): decide messaging from connect-time features, not refreshed scopes

Both token-refresh paths re-stamp metadata.scopes from TikTok's refresh
response, so an empty or partial answer there would have blocked the
private reply and Comment-to-Message on a working DM connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
TikTok encodes the OAuth state as base64url, but the integrations
callback decoded it with atob, which rejects "-" and "_". Whenever the
encoded state contained them the callback returned 404, so connecting
TikTok failed for most workspaces. Decode with Node's base64url decoder,
which accepts both base64url and the standard base64 the other
integrations produce with btoa.

Reported in #1572 (comment 6650507443).

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TikTok: allow a comments-only connection (EEA accounts can't get Business Messaging scopes)

2 participants