commands.js | |
---|---|
Example of a very simple Express.js web application that exposes all the connFu commands to a very simple REST API To run it, execute this file with the application key as the first parameter: | |
BOOTSTRAP EXPRESS APPLICATION | |
Include dependencies | var connFu = require('connfu');
var sys = require('sys');
var express = require('express'); |
Express Configuration | var app = module.exports = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(app.router);
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}); |
Initialize connFu application | var connfuApp = connFu.createApp(process.argv[2]); |
GENERAL METHODS | |
Get application informationResponse: | app.get('/', function(req, res){
connfuApp.commands.getApp(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
VOICE CHANNEL METHODS | |
Get all voice channelsResponse: | app.get('/voice', function(req, res){
connfuApp.commands.voice().get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Get a voice channel by uidSimilar response as get all voice channels | app.get('/voice/:uid', function(req, res){
connfuApp.commands.voice(req.params.uid).get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Create a voice channelParameters:
| app.post('/voice', function(req, res){
connfuApp.commands.voice().create(
req.body.uid,
req.body.country,
{
privacy: req.body.privacy,
welcome_message: req.body.welcome_message,
rejected_message: req.body.rejected_message,
topic: req.body.topic
},
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Update a voice channelParameters:
| app.put('/voice/:uid', function(req, res){
connfuApp.commands.voice(req.params.uid).update({
privacy: req.body.privacy,
welcome_message: req.body.welcome_message,
rejected_message: req.body.rejected_message,
topic: req.body.topic
},
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Delete a voice channelThis method doesn't return anything | app.delete('/voice/:uid', function(req, res){
connfuApp.commands.voice(req.params.uid).remove(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Whitelist methods | |
Get the full whitelist of a voice channelResponse: | app.get('/voice/:uid/whitelist', function(req, res){
connfuApp.commands.voice(req.params.uid).whitelist().get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Get a whitelist entry of a voice channelSimilar response as get the full whitelist | app.get('/voice/:uid/whitelist/:phone', function(req, res){
connfuApp.commands.voice(req.params.uid).whitelist(req.params.phone).get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Create a whitelist entry in a voice channelParameters:
| app.post('/voice/:uid/whitelist', function(req, res){
connfuApp.commands.voice(req.params.uid).whitelist().create(
req.body.phone,
req.body.name,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Update the name of a whitelist entry in a voice channelParameters:
| app.put('/voice/:uid/whitelist/:phone', function(req, res){
connfuApp.commands.voice(req.params.uid).whitelist(req.params.phone).update(
req.body.phone,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Delete a whitelist entry of a voice channelThis method doesn't return anything | app.delete('/voice/:uid/whitelist/:phone', function(req, res){
connfuApp.commands.voice(req.params.uid).whitelist(req.params.phone).remove(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Delete the full whitelist of a voice channelThis method doesn't return anything | app.delete('/voice/:uid/whitelist', function(req, res){
connfuApp.commands.voice(req.params.uid).whitelist().remove(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Phone allocation methods | |
Get all the associated phones of a voice channelResponse: | app.get('/voice/:uid/phones', function(req, res){
connfuApp.commands.voice(req.params.uid).phones().get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Allocate a phone number in a voice channelParams:
| app.post('/voice/:uid/phones', function(req, res){
connfuApp.commands.voice(req.params.uid).phones().create(
req.body.country,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Deallocate a phone number in a voice channelThis method doesn't return anything | app.delete('/voice/:uid/phones/:phone', function(req, res){
connfuApp.commands.voice(req.params.uid).phones(req.params.phone).remove(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
RSS CHANNEL METHODS | |
Get all the RSS channelsResponse: | app.get('/rss', function(req, res){
connfuApp.commands.rss().get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Get an RSS channel by UIDResponse: Similar to get all RSS channels response | app.get('/rss/:uid', function(req, res){
connfuApp.commands.rss(req.params.uid).get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Create an RSS channel
| app.post('/rss', function(req, res){
connfuApp.commands.rss().create(
req.body.uid,
req.body.url,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Update an RSS channel
| app.put('/rss/:uid', function(req, res){
connfuApp.commands.rss(req.params.uid).update(
req.body.url,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Remove an RSS channelThis method doesn't return anything | app.delete('/rss/:uid', function(req, res){
connfuApp.commands.rss(req.params.uid).remove(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
TWITTER CHANNEL METHODS | |
Get all the Twitter channelsResponse: | app.get('/twitter', function(req, res){
connfuApp.commands.twitter().get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Get a Twitter channel by UIDResponse: Similar to get all Twitter channels response | app.get('/twitter/:uid', function(req, res){
connfuApp.commands.twitter(req.params.uid).get(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Create a Twitter channel(only for tweets mentioning the specified account) Info about parameters:
| app.post('/twitter_mentioned', function(req, res){
connfuApp.commands.twitter().create(
req.body.uid,
"mentioned",
req.body.account,
req.body.filters,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Create a Twitter channel(only for tweets origined from the specified accounts) Info about parameters:
| app.post('/twitter_origin', function(req, res){
connfuApp.commands.twitter().create(
req.body.uid,
"origin",
req.body.accounts,
req.body.filters,
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
Remove a Twitter channelThis method doesn't return anything | app.delete('/twitter/:uid', function(req, res){
connfuApp.commands.twitter(req.params.uid).remove(
function(err, data) {
if (err) { return res.json(err.message, err.code); }
res.json(data);
}
);
}); |
MAKE EXPRESS LISTEN TO PORT 5000 | app.listen(5000);
console.log(
"Express server listening on port %d in %s mode",
app.address().port,
app.settings.env
);
|