Create easily chat bots for any YouTube stream's LiveChat.
Install via NPM, PNPM or Yarn from repo
$ npm i yt-livechat --save
$ pnpm i yt-livechat
$ yarn add yt-livechat
// Import the lib
const { LiveChat } = require("yt-livechat");
// Or with TypeScript:
// import { LiveChat } from "yt-livechat"
// Let's do some config
const config = {
liveChatID: process.env.LIVE_CHAT_ID || "", // ID of the LiveChat
oauth: { // OAuth2 keys from Google Developers Console
client_id: process.env.CLIENT_ID || "",
client_secret: process.env.CLIENT_SECRET || "",
refresh_token: process.env.REFRESH_TOKEN || "",
},
};
const chat = new LiveChat(config); // Init chat object
// Register some events
chat.on("connected", () => console.log("Connected to the YouTube API."));
chat.on("error", (error) => console.log(error));
chat.on("chat", (message) => {
console.log(`New message from ${message.authorDetails.displayName}.`);
if (message.snippet.displayMessage === "/hello") {
chat.say("Hello world !");
}
});
// Start polling messages
chat.connect();
- Constructor
- Properties
auth
connected
- Methods
connect()
disconnect()
reconnect()
say()
delete()
- Events
connected
disconnected
reconnected
polling
tokens
error
chat
const { LiveChat } = require("yt-livechat");
const config = { ... };
const chat = new LiveChat(config);
{
oauth: { // See this: https://developers.google.com/identity/protocols/OAuth2
client_id?: string;
client_secret?: string;
refresh_token?: string;
access_token?: string;
token_type?: "Bearer" | string;
expiry_date?: number;
};
liveChatID: string; // ID of the LiveChat
interval?: number; // Force time interval in ms between each poll.
}
You might be able to find ID of the Live Chat with this API endpoint.
OAuth2 client from the google-auth-library lib. You can use it to make custom authenticated requests for example.
Equals true
if the lib polls messages. Else equals false
.
Start polling messages from the chat.
chat.connect()
Stop polling messages from the chat.
chat.disconnect()
Re-create OAuth client and just execute disconnect()
and connect()
.
chat.reconnect()
Send a message.
chat.say("Hello !")
Delete a message based on his ID.
chat.delete("MESSAGE ID")
Emitted when the lib start polling messages from YouTube. Usually after the execution of the connect()
method
chat.on('connected', () => ... )
Emitted when the lib stop polling messages from YouTube. Usually after the execution of the disconnect()
method
chat.on('disconnected', () => ... )
Emitted when the lib reconnects to YouTube. Usually after the execution of the reconnect()
methods.
chat.on('reconnected', () => ... )
Emitted when the lib poll messages from YouTube. /!\ This event is usually issued a lot of times in less than a second: if you perform too many operations, you risk running out of resources!
chat.on('polling', () => ...)
Emitted when the access token is refreshed.
chat.on('tokens', (tokens) => ...)
{
access_token?: string;
token_type?: "Bearer" | string;
expiry_date?: number;
}
Emitted when an error occured.
chat.on('error', (error) => ...)
The error object is very VERY VERY big because it contains all the request and response ! But just a small part can be enough :happy:
chat.on('error', (error) => {
console.log(error.errors); // In this example, I faked the live chat ID to produce an error.
})
Let's take a look at the result:
[
{
domain: 'youtube.liveChat',
reason: 'liveChatNotFound',
message: 'The live chat that you are trying to retrieve cannot be found. Check the value of the requests <code>liveChatId</code> parameter to ensure that it is correct.'
}
]
With this link to help you, I think it's enough to understand how to handle errors 😃
Emitted when an user sent a message. (Pretty obvious...)
chat.on('chat', (message) => ...)
Take a look here : https://developers.google.com/youtube/v3/live/docs/liveChatMessages#resource
A checked item is considered as a work in progress.
- Write unit tests
- Methods should return promises (but still support events)
- Add methods to get a Live Chat ID
Feel free to suggest features !
You're free to contribute by publishing pull requests, issues, ideas, ...
You can also buy me a drink ❤️