Tutorial

How to Build a TikTok Live Chat Bot
in 5 Minutes

Build an automated TikTok LIVE chat bot that responds to commands, welcomes viewers, tracks gifts, and runs interactive games — using the TikTool Live API.

tiktok.com/@
Stream offlineEnter a live TikTok username above
Connecting to @ live
Offline
Live Stream Monitor
@
<50msLatency
99.9%Uptime
FreeTo Test

What You'll Build

A fully functional TikTok LIVE chat bot that:

  • Responds to chat commands (!hello, !stats, !top)
  • Tracks gifts and maintains a real-time leaderboard
  • Logs all events to the console (or your database)
  • Runs 24/7 with automatic reconnection

Prerequisites

  • Node.js 18+ installed
  • A free TikTool Live API key — get one here

Step 1: Set Up Your Project

mkdir tiktok-bot && cd tiktok-bot
npm init -y
npm install @tiktool/live

Step 2: Create the Bot

Create bot.mjs with the following code:

import { TikTokLive } from '@tiktool/live'

// ── Config ──
const STREAMER = 'target_username'  // TikTok username (without @)
const API_KEY = process.env.TIKTOOL_API_KEY

// ── State ──
const giftLeaderboard = new Map()
let totalMessages = 0
let totalGifts = 0

// ── Client ──
const client = new TikTokLive({
  uniqueId: STREAMER,
  apiKey: API_KEY
})

// ── Event Handlers ──

// Chat messages & commands
client.on('chat', (event) => {
  totalMessages++
  const msg = event.comment.toLowerCase().trim()

  console.log(`[CHAT] ${event.nickname}: ${event.comment}`)

  // Command: !hello
  if (msg === '!hello') {
    console.log(`>> BOT: Welcome ${event.nickname}! 👋`)
  }

  // Command: !stats
  if (msg === '!stats') {
    console.log(`>> BOT: ${totalMessages} messages, ${totalGifts} gifts so far!`)
  }

  // Command: !top
  if (msg === '!top') {
    const sorted = [...giftLeaderboard.entries()]
      .sort((a, b) => b[1] - a[1])
      .slice(0, 5)

    if (sorted.length === 0) {
      console.log('>> BOT: No gifts yet!')
    } else {
      console.log('>> BOT: 🏆 Top Gifters:')
      sorted.forEach(([name, diamonds], i) => {
        console.log(`   ${i + 1}. ${name} — ${diamonds} 💎`)
      })
    }
  }
})

// Gift tracking
client.on('gift', (event) => {
  totalGifts++
  const current = giftLeaderboard.get(event.nickname) || 0
  giftLeaderboard.set(event.nickname, current + event.diamondCount)

  console.log(`[GIFT] ${event.nickname} sent ${event.giftName} (${event.diamondCount} 💎)`)

  // Big gift alert
  if (event.diamondCount >= 100) {
    console.log(`>> BOT: 🎉 HUGE gift from ${event.nickname}! Thank you!`)
  }
})

// New followers
client.on('follow', (event) => {
  console.log(`[FOLLOW] ${event.nickname} just followed! ❤️`)
})

// Viewer count
client.on('viewer_count', (event) => {
  console.log(`[VIEWERS] ${event.viewerCount} watching`)
})

// ── Connect ──
console.log(`Connecting to @${STREAMER}...`)
await client.connect()
console.log('✅ Bot is running! Listening for events...')

Step 3: Run the Bot

TIKTOOL_API_KEY=your_api_key node bot.mjs

Step 4: Expected Output

Connecting to @target_username...
✅ Bot is running! Listening for events...
[VIEWERS] 2,341 watching
[CHAT] viewer_1: !hello
>> BOT: Welcome viewer_1! 👋
[GIFT] big_fan sent Rose (1 💎)
[CHAT] viewer_2: !stats
>> BOT: 14 messages, 3 gifts so far!
[GIFT] whale_user sent Universe (34,000 💎)
>> BOT: 🎉 HUGE gift from whale_user! Thank you!
[CHAT] viewer_3: !top
>> BOT: 🏆 Top Gifters:
   1. whale_user — 34,000 💎
   2. big_fan — 156 💎

What's Next?

You now have a working TikTok LIVE chat bot. Here are ideas to extend it:

  • Database integration — store messages and gifts in a database like PostgreSQL or MongoDB
  • AI responses — feed chat messages to GPT/Claude for intelligent auto-responses
  • Web dashboard — build a real-time dashboard showing chat, gifts, and viewer counts
  • Multi-stream — monitor multiple streams simultaneously (Pro tier supports 50 connections)
  • Moderation — add spam filters, word blacklists, and rate limiting