A TikTok LIVE chat bot is a loop: poll /v1/live/chat with the cursor from the last call, look at each event’s type (WebcastChatMessage, WebcastGiftMessage, WebcastMemberMessage…), and react. In your own room the bot answers, pins or removes comments and mutes viewers through /v1/account/live/moderate. Start the stream itself with /v1/account/live/start and push video to the RTMP URL it returns.
What you will build
- A stream started from code, with OBS or ffmpeg pushing to TikTok’s RTMP ingest.
- A chat loop that reads every new event a few seconds after it happens.
!commandsthat reply in chat, a keyword blocklist that removes spam, and a gift handler for alerts or text-to-speech.
Step 1: Connect the account and go LIVE
Connect the TikTok account you stream from in the dashboard (email code or session import); you get an account_id. Then create the room. The response has the room_id, an rtmp_push_url, a stream_key and a ping_interval_sec.
import os, requests
API = "https://api.tikultra.com"
HEADERS = {"x-api-key": os.environ["TIKULTRA_API_KEY"]}
def get(path, **params):
r = requests.get(API + path, headers=HEADERS, params=params, timeout=60)
r.raise_for_status()
return r.json()
def post(path, body):
r = requests.post(API + path, headers=HEADERS, json=body, timeout=60)
r.raise_for_status()
return r.json()
ACCOUNT = "acc_..." # your connected account
room = post("/v1/account/live/start", {"account_id": ACCOUNT, "title": "Desk build, part 2"})
print("RTMP:", room["rtmp_push_url"])
print("Key: ", room["stream_key"])
ROOM = room["room_id"]Paste the URL and key into OBS (Settings → Stream → Custom), or push a file with ffmpeg. While you stream, send /v1/account/live/ping every ping_interval_sec seconds so the room stays open, and call /v1/account/live/finish when you are done.
Step 2: Read chat and gift events
/v1/live/chat returns the events since the cursor you pass, plus the next cursor and a suggested fetch_interval in milliseconds. Each event has a method (its Webcast message type), a msg_id and a protobuf payload. Decode payloads with any protobuf decoder; the /v1/crypto/protobuf/decode endpoint does it for free.
import base64
def fields(payload_b64):
hexstr = base64.b64decode(payload_b64).hex()
return post("/v1/crypto/protobuf/decode", {"hex": hexstr})["fields"]
def chat_text(payload_b64):
# In WebcastChatMessage the comment text is field 3.
for f in fields(payload_b64):
if f["field_number"] == 3 and f["wire_type"] == "bytes_utf8":
return f["value"]
return ""Step 3: The bot loop
import time
BLOCKLIST = ["free followers", "dm me for", "http://"]
COMMANDS = {
"!link": "The product link is pinned at the top 👆",
"!schedule": "LIVE every Tue & Thu at 8pm ET",
}
def act(action, **extra):
return post("/v1/account/live/moderate", {"account_id": ACCOUNT, "room_id": ROOM, "action": action, **extra})
cursor = None
while True:
page = get("/v1/live/chat", room_id=ROOM, **({"cursor": cursor} if cursor else {}))
chat = page["chat"]
for m in chat["messages"]:
if m["method"] == "WebcastChatMessage":
text = chat_text(m["payload"]).lower()
if any(bad in text for bad in BLOCKLIST):
act("delete", target_id=m["msg_id"])
elif text.split(" ")[0] in COMMANDS:
act("comment", text=COMMANDS[text.split(" ")[0]])
elif m["method"] == "WebcastGiftMessage":
print("gift!", m["msg_id"]) # play an alert, run TTS, update a leaderboard
cursor = chat["cursor"]
time.sleep(max(chat.get("fetch_interval", 1000) / 1000, 3))Polling speed and cost
Each chat poll is 2 reqs and each moderation action is 10 reqs. Polling every 3 to 5 seconds feels live to viewers. An hour at one poll every 5 seconds is 720 polls, 1,440 requests, about $1.31 on Growth before moderation actions.
Ideas to add next
- Gift leaderboard: keep a running total per gifter and show the top five in an OBS browser source.
- Text-to-speech: read chat or gift messages aloud for gifts above a coin value from
/v1/live/gifts. - Pinned link: pin your current product or link with
action: "pin"whenever someone asks. - Mute repeat offenders: count blocklist hits per viewer and
muteafter three. - Co-hosts: invite a guest with
/v1/account/live/cohost.