NAV
javascript

Introduction

Welcome to Picarto Chatbot API.

Authentication

Authentication Variables

Variable Description
BOT_USERNAME The account (username) that the chatbot uses to send chat messages. This can be your PICARTO account. Alternately, many developers choose to create a second Picarto account for their bot, so it’s clear from whom the messages originate.
OAUTH_TOKEN The token to authenticate your chatbot with Picarto’s servers. Generate this with https://oauth.picarto.tv/chat/bot, while logged in to your chatbot account. The token will be an alphanumeric string.

Command & Message Limits

There are limits of the number of commands or messages you are allowed to send to the server. If you exceed these limits, you are locked out of chat for 30 minutes.

Command and Message Limits are:

Limit Applies to
20 per 30 seconds Users sending commands or messages to channels

For Whispers, which are private chat message between two users:

Limit Applies to
2 per second, 20 per 30 seconds Users

Building the Bot

const pmi = require('pmi.js');

/* Define configuration options */
const opts = {
identity: {
username: "<BOT_USERNAME>",
password: "<OAUTH_TOKEN>"
}
};

/* Create a client with our options */
const client = new pmi.client(opts);

/* Register our event handlers */
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);

/* Connect to Picarto: */
client.connect();

/* Called every time a message comes in*/
function onMessageHandler (target, context, msg, self) {
if (self) { return; } /* Ignore messages from the bot */

const commandName = msg;

/* If the command is known, let's execute it */
if (commandName === '!hello') {
client.say(target, sayHello());
console.log(`* Executed ${commandName} command`);
} else {
console.log(`* Unknown command ${commandName}`);
}
}

/* Function called when the "hello" command is issued */
function sayHello () {
return 'Greetings from ChatBot';
}

/* Called every time the bot connects to Picarto chat */
function onConnectedHandler (addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}

If you prefer to build and run our bot example locally, follow these steps:

  1. Install node.js and npm, following the instructions on the links.
  2. On the command line, install pmi.js:
    npm install pmi.js
  3. In bot.js below, replace the three environmental variables with the values obtained above.
  4. Run bot.js locally using node:
    node bot.js
  5. Now that the bot is running and connected to the Picarto Chat Server, we can interact with it. On https://picarto.tv/<CHANNEL_NAME>, type !hello. You’ll see Greetings from ChatBot.

PMI: Commands

Command Description
chat Send Chat Message
clearHistory Purge Chat Message
clearMessage Purge Channel Message
banUser Ban User
unbanUser Unban User
setMod Set User Moderator
unsetMod Revoke User Moderator
ignoreUser Ignore User Message
unignoreUser Unignore User Message
whisper Send Whisper Message