Skip to content

Use nnz.api.chat to type in chat, nnz.api.music to read or control music, and nnz.api.tts to speak through the overlay. They are separate outputs: sending a chat reply does not also play it aloud, and a spoken line does not also appear in chat. Script basics explains the grants and how to inspect a failed call.

Before you start

Custom Code must be on for the channel. Script basics shows where you switch it on. Every call on this page needs its grant, and the bot adds the grant when you save code that names the call.

A left-out, null, empty or unknown value follows the one rule on Script basics. The only exception on this page is speakSequence, under Speak in several voices.

Chat

The bot posts every line in chat in the channel’s one bot voice.

CallResultGrant
nnz.api.chat.send(text)nothingchat.send
nnz.api.chat.reply(text)nothingchat.reply

Send a line

nnz.api.chat.send(text) posts text in the channel’s chat. Empty text is a failed call with the code invalid_argument, and the script goes on. A line that does not reach chat sets the code upstream_failed.

Reply to a message

nnz.api.chat.reply(text) answers the chat message that started the script, as a threaded reply. When the platform refuses the thread, the bot posts the line with a mention of the viewer instead. When no chat message started the script, the bot posts a normal line.

Chat values a command script can read

A script that a chat command or a chat trigger runs can read these values with bot.getVar(key).

KeyValue
userThe viewer’s display name.
user.idThe viewer’s id on the platform.
user.nameThe viewer’s login.
user.providerThe platform the viewer chats on.
providerThe platform the chat message came from (the same as user.provider).
user.roleThe viewer’s role in the channel.
user.bitsThe bits on this message.
messageThe whole chat line, the command word included.
argsThe text after the command word.
args.countHow many words follow the command word.
args.1, args.2 and so onOne word after the command word, counted from 1.
targetThe viewer named by the first word after the command word.

Music

CallResultGrant
nnz.api.music.nowPlaying()the track, or nullmusic.nowPlaying
nnz.api.music.queue(uri)true or falsemusic.queue
nnz.api.music.playNow(uri)true or falseactions.invoke:play_track_once

The player itself is on Music player.

Read the track that plays now

nnz.api.music.nowPlaying() gives the track that plays now, or null when nothing plays.

FieldTypeMeaning
trackstring | nullThe track title, or null when the service does not report it.
artiststring | nullThe artist name, or null when the service does not report it.
albumstring | nullThe album name, or null when it is not known.
durationMsnumberThe length of the track, in milliseconds.
progressMsnumberHow far the playback is, in milliseconds.
isPlayingbooleantrue while the track plays, false when it is paused.
requestedBystring | nullWho requested the track, or null when nobody did.
providerstringThe music service that plays the track.

Request a song

nnz.api.music.queue(uri) requests a song. Pass a title, an artist or a link as uri. It gives true when the song is in the queue, and false when it was refused.

Use this for a normal song request that should wait in the queue. Use playNow only when you intentionally want immediate playback outside that queue.

The request counts as one from the viewer who started the script, with the rights of everyone. A music service that the channel limits to a higher role refuses it.

Play a track now

nnz.api.music.playNow(uri) plays a track at once and skips the song-request queue. It gives true when the track plays, and false when it was refused. When the track ends, the bot plays the track that played before again, from the point where it stopped. It runs the pipeline action that plays one track, so its grant is the grant of that action: actions.invoke:play_track_once.

Text to speech

CallResultGrant
nnz.api.tts.speak(text, voiceId, ratePercent, pitchPercent, asUser)the result, or nulltts.speak
nnz.api.tts.speakSequence(segments)the result, or nulltts.speakSequence
nnz.api.tts.getVoice(userIdOrLogin)the voice, or nulltts.voice.get
nnz.api.tts.setVoice(userIdOrLogin, voiceId)true or falsetts.voice.set
nnz.api.tts.listVoices(provider, locale)a list of voicestts.voice.list

The channel’s speech settings are on Text to speech. A spoken line passes the same checks as any other line of text to speech.

Speak a line

nnz.api.tts.speak(text) speaks text on the overlay. Only text is required.

ParameterMeaning
voiceIdThe voice for this one line. Leave it out for the normal voice.
ratePercentA change of speaking speed for this one line, in percent.
pitchPercentA change of pitch for this one line, in percent.
asUserWhose saved voice reads the line. Leave it out for the bot’s own voice.

The line speaks in the channel’s one bot voice, unless you set asUser. Set asUser to 'viewer' when the line holds the words of the viewer who started the script. 'user' and 'trigger' work the same as 'viewer', and 'bot' is the same as leaving it out. A login or an id reads the line in that viewer’s saved voice. Use it only for that viewer’s own words. A name that matches no viewer is a failed call with the code not_found: the call gives null, and the script goes on.

The speed and pitch changes apply to this one line only. They do not change the channel’s settings.

The call gives null when text to speech refused the line. Otherwise it gives this result:

FieldTypeMeaning
voiceIdstringThe id of the voice that speaks the line.
characterCountnumberHow many characters text to speech accepted.
durationMsnumberHow long the line takes to play, in milliseconds. 0 for a test run, or for a line that waits for approval.

Speak in several voices

nnz.api.tts.speakSequence(segments) speaks a list of blocks as one audio clip. The list holds 1 to 20 blocks, in any order. A block is either a text block, which has words and its own voice, or a pause block.

Field of a text blockMeaning
textThe words to speak. Required.
voiceThe voice id for this block. Leave it out for the normal voice.
rateA change of speaking speed, in percent.
pitchA change of pitch, in percent.
volumeA change of loudness, in percent, for example 20.
stresstrue to speak this block with stress. Leave it out or use false for none.
breakAfterMsSilence after this block, from 0 to 10,000 milliseconds. The last block ignores it.

A pause block has only pause, the silence in milliseconds from 0 to 10,000: \{ pause: 500 \}. The silence goes after the block before it, and adds to that block’s breakAfterMs up to the 10,000 limit. A pause with no block before it is dropped. A pause field on a text block does nothing. Use breakAfterMs on a text block, or a separate pause block.

Each speech provider reads the blocks its own way. Azure joins blocks of one voice that follow each other, so they flow as one sentence, and a stressed block comes out slower, higher and louder. Edge cannot stress, so it joins neighboring blocks that sound the same and cuts at a pause. ElevenLabs speaks a run of blocks with one voice as one text. It ignores rate, pitch, volume, stress and pauses. A provider that cannot stress speaks a stressed block plain. When the channel needs mod approval for text to speech, the blocks play as one text in the voice of the first block.

The call gives the same result as speak, or null. This call is the exception to the one rule. The list is required, but the bot checks it, not the script. So a left-out or null list does not stop the run. These cases are each a failed call with the code invalid_argument:

  • a left-out, null or empty list
  • more than 20 blocks, pause blocks included
  • a block without text and without a valid pause
  • a list that holds only pause blocks
  • a breakAfterMs outside 0 to 10,000
  • a field of the wrong type, for example a rate that is text or a stress that is not true or false

The call gives null, and the script goes on.

Read a viewer’s voice

nnz.api.tts.getVoice(userIdOrLogin) gives the voice a viewer has, as voiceId and displayName. Leave out userIdOrLogin for the viewer who started the script. It gives null when the viewer uses the channel’s normal voice, and when the name matches no viewer. Both cases are a failed call with the code not_found, and the script goes on. The error message tells them apart: “No viewer matches that user.” means the name is unknown.

Set a viewer’s voice

nnz.api.tts.setVoice(userIdOrLogin, voiceId) gives a viewer a voice. Leave out voiceId to put the viewer back on the channel’s normal voice. userIdOrLogin is required: leave it out and the run stops with “tts.setVoice needs a user id or login”. A name that matches no viewer is a failed call with the code not_found: the call gives false, and the script goes on. It also gives false when the voice is not known.

List the voices

nnz.api.tts.listVoices(provider, locale) gives at most 100 voices. Both parameters are optional. provider keeps the voices of one service, for example Edge.

locale with a region, such as en-US, keeps that locale. locale with only a language, such as en, keeps every locale of that language. The list is empty when the voice list cannot be read.

Each voice has voiceId, displayName, locale and gender.

Speak, then post

This script requests a spoken line, waits for up to five seconds of its reported duration, then posts the same text in chat. It demonstrates combining outputs, rather than waiting for a confirmed playback-complete event. Overlay playback and approval can affect when a viewer actually hears the line.

nnz.time.sleep caps each pause at five seconds and counts against the script’s own execution budget. If the reported duration is longer, the chat message can appear while the audio is still playing. When durationMs is 0, or speak returns null, the script posts at once.

JavaScript
const line = 'Welcome to the stream!';

const spoken = nnz.api.tts.speak(line);

if (spoken) {
nnz.time.sleep(spoken.durationMs);
}

nnz.api.chat.send(line);

The bot speaks and posts in the channel’s one bot voice.