#Turntable API
A simple nodejs wrapper for the turntable API
npm install ttapi
Find your AUTH
, USERID
and ROOMID
informations with that bookmarklet.
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 tcp message.
Triggered when a socket close its connection.
Triggered when the bot receive an http request.
on('roomChanged', function (data) { })
Triggered when the bot enter in a room.
on('registered', function (data) { })
Triggered when a user enter the room.
on('deregistered', function (data) { })
Triggered when a user leave the room.
on('speak', function (data) { })
Triggered when a new message is send via the chat.
Triggered at the end of the song. (Just before the newsong/nosong event)
on('newsong', function (data) { })
Triggered when a new song start.
on('nosong', function (data) { })
Triggered when there is no song.
on('update_votes', function (data) { })
Triggered when a user vote.
Triggered when a user is booted.
Triggered when a user change his name/infos.
on('add_dj', function (data) { })
Triggered when a user take a dj spot.
on('rem_dj', function (data) { })
Triggered when a user leave a dj spot.
on('new_moderator', function (data) { })
Triggered when a user is granted to moderator title.
on('rem_moderator', function (data) { })
Triggered when a user loose his moderator title.
on('snagged', function (data) { })
Triggered when a user queues the currently playing song.
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. Do not include song log if 'extended' is false.
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.
Get a user profile.
Modify your profile. Any missing properties from the 'profile' object will be replaced with the current values.
profile
:obj (required)name
:string (optional)twitter
:string (optional)facebook
:string (optional)website
:string (optional)about
:string (optional)topartists
:string (optional)hangout
:string (optional)
callback
:fn (optional)
bot.modifyProfile({ website:'http://ttdashboard.com/', about:'My bot.' }, callback);
Modify your laptop.
Modify your name.
Set your avatar.
Fan someone.
Unfan someone.
Snag the song.
This function will not add the song into the queue.
Get all informations about a playlist.
playlistName
(optional) default:default
callback
(required)
bot.playlistAll(callback);
bot.playlistAll(playlistName, callback);
Add a song on a playlist.
playlistName
(optional) default:default
songId
(required)index
(optional) default:0
callback
(optional)
bot.playlistAdd(songId);
bot.playlistAdd(songId, idx);
bot.playlistAdd(songId, callback);
bot.playlistAdd(songId, idx, callback);
bot.playlistAdd(playlistName, songId, idx);
bot.playlistAdd(playlistName, songId, callback);
bot.playlistAdd(playlistName, songId, idx, callback);
bot.playlistAdd(false, songId, callback); // Backward compatibility
bot.playlistAdd(false, songId); // Backward compatibility
Remove a song on a playlist.
playlistName
(optional) default:default
index
(optional) default:0
callback
(optional)
bot.playlistRemove();
bot.playlistRemove(index);
bot.playlistRemove(index, callback);
bot.playlistRemove(playlistName, index);
bot.playlistRemove(playlistName, index, callback);
Reorder a playlist. Take the song at index indexFrom
and move it to index indexTo
.
playlistName
(optional) default:default
indexFrom
(required) default:0
indexTo
(required) default:0
callback
(optional)
bot.playlistReorder(indexFrom, indexTo);
bot.playlistReorder(indexFrom, indexTo, callback);
bot.playlistReorder(playlistName, indexFrom, indexTo);
bot.playlistReorder(playlistName, indexFrom, indexTo, callback);