#Turntable API
A simple nodejs wrapper for the turntable API
ttapi will not work with the latest release of NodeJs (v0.5.8)
.
It work well on v0.4.8 (tested).
npm install ttapi
This bot respond to anybody who write "/hello" on the chat.
var Bot = require('ttapi');
var bot = new Bot(AUTH, USERID, ROOMID);
bot.on('speak', function (data) {
// Respond to "/hello" command
if (data.text.match(/^\/hello$/)) {
bot.speak('Hey! How are you '+data.name+' ?');
}
});
This bot create an http server and give his version number when we ask for "http://127.0.0.1:8080/version/" this page.
var Bot = require('ttapi');
var bot = new Bot(AUTH, USERID, ROOMID);
bot.listen(8080, '127.0.0.1');
var myScriptVersion = '0.0.0';
bot.on('httpRequest', function (req, res) {
var method = req.method;
var url = req.url;
switch (url) {
case '/version/':
if (method == 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end('{"version":"'+myScriptVersion+'"}');
} else {
res.writeHead(500);
res.end();
}
break;
default:
res.writeHead(500);
res.end();
break;
}
});
This bot open a tcp server. That will allow you to easily communicate with the bot via a terminal.
var Bot = require('ttapi');
var bot = new Bot(AUTH, USERID, ROOMID);
bot.tcpListen(8080, '127.0.0.1');
var myScriptVersion = 'V0.0.0';
bot.on('tcpConnect', function (socket) { });
bot.on('tcpMessage', function (socket, msg) {
if (msg == 'version') {
socket.write('>> '+myScriptVersion+'\n');
}
});
bot.on('tcpEnd', function (socket) { });
You can communicate with the bot like this:
nc 127.0.0.1 8080
And then type:
version
var Bot = require('ttapi');
var AUTH = 'auth+live+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var USERID = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var ROOMID = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var bot = new Bot(AUTH, USERID);
bot.on('ready', function (data) { bot.roomRegister(ROOMID); });
bot.on('roomChanged', function (data) { console.log('The bot has changed room.', data); });
bot.on('speak', function (data) { console.log('Someone has spoken', data); });
bot.on('update_votes', function (data) { console.log('Someone has voted', data); });
bot.on('registered', function (data) { console.log('Someone registered', data); });
bot.debug = true;
That will print on the terminal all the data that you get and all the data that you send.
Here are some examples of the data that you'll receive from those events.
Triggered when a socket open a connection.
Triggered when the bot receive a message.
Triggered when a socket close its connection.
Triggered when the bot receive an http request.
Triggered when a user register in the room.
Triggered when a user leave the room.
Triggered when a new message is send via the chat.
Triggered when a new song start.
Triggered when there is no song.
Triggered when a user vote.
Triggered when a user is booted.
Triggered when a user change his name/infos.
Triggered when a user take a dj spot.
Triggered when a user leave a dj spot.
Triggered when a user is granted to moderator title.
Triggered when a user loose his moderator title.
Start a tcp server.
Start a http server.
Get the turntable server time.
Get 20 rooms.
Register in a room.
Deregister from the current room.
Get the current room informations.
Broadcast a message on the chat.
Boot a user.
Add a moderator.
Remove a moderator.
Add yourself as a Dj.
Remove a Dj.
Skip the current song.
Vote for the current song.
Authenticate the user.
Get the current user informations.
Modify your laptop.
Modify your name.
Set your avatar.
Fan someone.
Unfan someone.
Get all informations about a playlist.
Add a song on a playlist.
Remove a song on a playlist.