From b0c5e5665838c42ea6737a85a7bcdebb6fe22cf4 Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 14:54:06 +0430 Subject: [PATCH 1/7] Update server.js --- server.js | 117 +++++++----------------------------------------------- 1 file changed, 15 insertions(+), 102 deletions(-) diff --git a/server.js b/server.js index 36dc199..5619d5c 100644 --- a/server.js +++ b/server.js @@ -1,107 +1,20 @@ -// OpenShift sample Node application -var express = require('express'), - fs = require('fs'), - app = express(), - eps = require('ejs'), - morgan = require('morgan'); - -Object.assign=require('object-assign') +var TelegramBot = require('node-telegram-bot-api'); -app.engine('html', require('ejs').renderFile); -app.use(morgan('combined')) +var token = 'YOUR_TELEGRAM_BOT_TOKEN'; +// Setup polling way +var bot = new TelegramBot(token, {polling: true}); -var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080, - ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0', - mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL, - mongoURLLabel = ""; - -if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) { - var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(), - mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'], - mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'], - mongoDatabase = process.env[mongoServiceName + '_DATABASE'], - mongoPassword = process.env[mongoServiceName + '_PASSWORD'] - mongoUser = process.env[mongoServiceName + '_USER']; - - if (mongoHost && mongoPort && mongoDatabase) { - mongoURLLabel = mongoURL = 'mongodb://'; - if (mongoUser && mongoPassword) { - mongoURL += mongoUser + ':' + mongoPassword + '@'; - } - // Provide UI label that excludes user id and pw - mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase; - mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase; - - } -} -var db = null, - dbDetails = new Object(); - -var initDb = function(callback) { - if (mongoURL == null) return; - - var mongodb = require('mongodb'); - if (mongodb == null) return; - - mongodb.connect(mongoURL, function(err, conn) { - if (err) { - callback(err); - return; - } - - db = conn; - dbDetails.databaseName = db.databaseName; - dbDetails.url = mongoURLLabel; - dbDetails.type = 'MongoDB'; - - console.log('Connected to MongoDB at: %s', mongoURL); - }); -}; - -app.get('/', function (req, res) { - // try to initialize the db on every request if it's not already - // initialized. - if (!db) { - initDb(function(err){}); - } - if (db) { - var col = db.collection('counts'); - // Create a document with request IP and current time of request - col.insert({ip: req.ip, date: Date.now()}); - col.count(function(err, count){ - res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails }); - }); - } else { - res.render('index.html', { pageCountMessage : null}); - } +// Matches /echo [whatever] +bot.onText(/\/echo (.+)/, function (msg, match) { + var fromId = msg.from.id; + var resp = match[1]; + bot.sendMessage(fromId, resp); }); -app.get('/pagecount', function (req, res) { - // try to initialize the db on every request if it's not already - // initialized. - if (!db) { - initDb(function(err){}); - } - if (db) { - db.collection('counts').count(function(err, count ){ - res.send('{ pageCount: ' + count + '}'); - }); - } else { - res.send('{ pageCount: -1 }'); - } +// Any kind of message +bot.on('message', function (msg) { + var chatId = msg.chat.id; + // photo can be: a file path, a stream or a Telegram file_id + var photo = 'cats.png'; + bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'}); }); - -// error handling -app.use(function(err, req, res, next){ - console.error(err.stack); - res.status(500).send('Something bad happened!'); -}); - -initDb(function(err){ - console.log('Error connecting to Mongo. Message:\n'+err); -}); - -app.listen(port, ip); -console.log('Server running on http://%s:%s', ip, port); - -module.exports = app ; From 0868bbfc959fad50d0e87cc3864dfe45b461fead Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 14:55:46 +0430 Subject: [PATCH 2/7] Update index.html --- views/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/index.html b/views/index.html index aea7910..8dcc5c4 100644 --- a/views/index.html +++ b/views/index.html @@ -213,7 +213,7 @@ pre { - +dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

Welcome to your Node.js application on OpenShift

From c945de23dfd1b820c9c330148a9f4445df8e1fa4 Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 20:22:11 +0430 Subject: [PATCH 3/7] Update server.js --- server.js | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/server.js b/server.js index 5619d5c..8b13789 100644 --- a/server.js +++ b/server.js @@ -1,20 +1 @@ -var TelegramBot = require('node-telegram-bot-api'); -var token = 'YOUR_TELEGRAM_BOT_TOKEN'; -// Setup polling way -var bot = new TelegramBot(token, {polling: true}); - -// Matches /echo [whatever] -bot.onText(/\/echo (.+)/, function (msg, match) { - var fromId = msg.from.id; - var resp = match[1]; - bot.sendMessage(fromId, resp); -}); - -// Any kind of message -bot.on('message', function (msg) { - var chatId = msg.chat.id; - // photo can be: a file path, a stream or a Telegram file_id - var photo = 'cats.png'; - bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'}); -}); From 3d4829f8e4f8ad02f8f52ad4a661b4307dc6b77d Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 20:27:56 +0430 Subject: [PATCH 4/7] Update server.js --- server.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/server.js b/server.js index 8b13789..c5093df 100644 --- a/server.js +++ b/server.js @@ -1 +1,108 @@ +// OpenShift sample Node application +var express = require('express'), + fs = require('fs'), + app = express(), + eps = require('ejs'), + morgan = require('morgan'); + +Object.assign=require('object-assign') + +app.engine('html', require('ejs').renderFile); +app.use(morgan('combined')) + +var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080, + ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0', + mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL, + mongoURLLabel = ""; + +if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) { + var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(), + mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'], + mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'], + mongoDatabase = process.env[mongoServiceName + '_DATABASE'], + mongoPassword = process.env[mongoServiceName + '_PASSWORD'] + mongoUser = process.env[mongoServiceName + '_USER']; + + if (mongoHost && mongoPort && mongoDatabase) { + mongoURLLabel = mongoURL = 'mongodb://'; + if (mongoUser && mongoPassword) { + mongoURL += mongoUser + ':' + mongoPassword + '@'; + } + // Provide UI label that excludes user id and pw + mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase; + mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase; + + } +} +var db = null, + dbDetails = new Object(); + +var initDb = function(callback) { + if (mongoURL == null) return; + + var mongodb = require('mongodb'); + if (mongodb == null) return; + + mongodb.connect(mongoURL, function(err, conn) { + if (err) { + callback(err); + return; + } + + db = conn; + dbDetails.databaseName = db.databaseName; + dbDetails.url = mongoURLLabel; + dbDetails.type = 'MongoDB'; + + console.log('Connected to MongoDB at: %s', mongoURL); + }); +}; + +app.get('/', function (req, res) { + // try to initialize the db on every request if it's not already + // initialized. + if (!db) { + initDb(function(err){}); + } + if (db) { + var col = db.collection('counts'); + // Create a document with request IP and current time of request + col.insert({ip: req.ip, date: Date.now()}); + col.count(function(err, count){ + res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails }); + }); + } else { + res.render('index.html', { pageCountMessage : null}); + } +}); + +app.get('/pagecount', function (req, res) { + // try to initialize the db on every request if it's not already + // initialized. + if (!db) { + initDb(function(err){}); + } + if (db) { + db.collection('counts').count(function(err, count ){ + res.send('{ pageCount: ' + count + '}'); + }); + } else { + res.send('{ pageCount: -1 }'); + } +}); + +// error handling +app.use(function(err, req, res, next){ + console.error(err.stack); + res.status(500).send('Something bad happened!'); +}); + +initDb(function(err){ + console.log('Error connecting to Mongo. Message:\n'+err); +}); + +app.listen(port, ip); +console.log('Server running on http://%s:%s', ip, port); + +module.exports = app ; From 894d743e81b0967ba526f690b7ac9461fc3618a5 Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 20:31:16 +0430 Subject: [PATCH 5/7] Update server.js --- server.js | 118 +++++++----------------------------------------------- 1 file changed, 15 insertions(+), 103 deletions(-) diff --git a/server.js b/server.js index c5093df..5619d5c 100644 --- a/server.js +++ b/server.js @@ -1,108 +1,20 @@ +var TelegramBot = require('node-telegram-bot-api'); -// OpenShift sample Node application -var express = require('express'), - fs = require('fs'), - app = express(), - eps = require('ejs'), - morgan = require('morgan'); - -Object.assign=require('object-assign') +var token = 'YOUR_TELEGRAM_BOT_TOKEN'; +// Setup polling way +var bot = new TelegramBot(token, {polling: true}); -app.engine('html', require('ejs').renderFile); -app.use(morgan('combined')) - -var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080, - ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0', - mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL, - mongoURLLabel = ""; - -if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) { - var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(), - mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'], - mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'], - mongoDatabase = process.env[mongoServiceName + '_DATABASE'], - mongoPassword = process.env[mongoServiceName + '_PASSWORD'] - mongoUser = process.env[mongoServiceName + '_USER']; - - if (mongoHost && mongoPort && mongoDatabase) { - mongoURLLabel = mongoURL = 'mongodb://'; - if (mongoUser && mongoPassword) { - mongoURL += mongoUser + ':' + mongoPassword + '@'; - } - // Provide UI label that excludes user id and pw - mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase; - mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase; - - } -} -var db = null, - dbDetails = new Object(); - -var initDb = function(callback) { - if (mongoURL == null) return; - - var mongodb = require('mongodb'); - if (mongodb == null) return; - - mongodb.connect(mongoURL, function(err, conn) { - if (err) { - callback(err); - return; - } - - db = conn; - dbDetails.databaseName = db.databaseName; - dbDetails.url = mongoURLLabel; - dbDetails.type = 'MongoDB'; - - console.log('Connected to MongoDB at: %s', mongoURL); - }); -}; - -app.get('/', function (req, res) { - // try to initialize the db on every request if it's not already - // initialized. - if (!db) { - initDb(function(err){}); - } - if (db) { - var col = db.collection('counts'); - // Create a document with request IP and current time of request - col.insert({ip: req.ip, date: Date.now()}); - col.count(function(err, count){ - res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails }); - }); - } else { - res.render('index.html', { pageCountMessage : null}); - } +// Matches /echo [whatever] +bot.onText(/\/echo (.+)/, function (msg, match) { + var fromId = msg.from.id; + var resp = match[1]; + bot.sendMessage(fromId, resp); }); -app.get('/pagecount', function (req, res) { - // try to initialize the db on every request if it's not already - // initialized. - if (!db) { - initDb(function(err){}); - } - if (db) { - db.collection('counts').count(function(err, count ){ - res.send('{ pageCount: ' + count + '}'); - }); - } else { - res.send('{ pageCount: -1 }'); - } +// Any kind of message +bot.on('message', function (msg) { + var chatId = msg.chat.id; + // photo can be: a file path, a stream or a Telegram file_id + var photo = 'cats.png'; + bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'}); }); - -// error handling -app.use(function(err, req, res, next){ - console.error(err.stack); - res.status(500).send('Something bad happened!'); -}); - -initDb(function(err){ - console.log('Error connecting to Mongo. Message:\n'+err); -}); - -app.listen(port, ip); -console.log('Server running on http://%s:%s', ip, port); - -module.exports = app ; From 893a4993a4ce954c36b986c20b05b37467023e2b Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 20:33:06 +0430 Subject: [PATCH 6/7] Update index.html --- views/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/index.html b/views/index.html index 8dcc5c4..aea7910 100644 --- a/views/index.html +++ b/views/index.html @@ -213,7 +213,7 @@ pre { -dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd +

Welcome to your Node.js application on OpenShift

From 30d4f1c81054d82e5b153ffce28f8acf19d2bddc Mon Sep 17 00:00:00 2001 From: manshahi Date: Thu, 25 May 2017 20:34:47 +0430 Subject: [PATCH 7/7] Update server.js --- server.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 103 insertions(+), 15 deletions(-) diff --git a/server.js b/server.js index 5619d5c..c5093df 100644 --- a/server.js +++ b/server.js @@ -1,20 +1,108 @@ -var TelegramBot = require('node-telegram-bot-api'); -var token = 'YOUR_TELEGRAM_BOT_TOKEN'; -// Setup polling way -var bot = new TelegramBot(token, {polling: true}); +// OpenShift sample Node application +var express = require('express'), + fs = require('fs'), + app = express(), + eps = require('ejs'), + morgan = require('morgan'); + +Object.assign=require('object-assign') -// Matches /echo [whatever] -bot.onText(/\/echo (.+)/, function (msg, match) { - var fromId = msg.from.id; - var resp = match[1]; - bot.sendMessage(fromId, resp); +app.engine('html', require('ejs').renderFile); +app.use(morgan('combined')) + +var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080, + ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0', + mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL, + mongoURLLabel = ""; + +if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) { + var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(), + mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'], + mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'], + mongoDatabase = process.env[mongoServiceName + '_DATABASE'], + mongoPassword = process.env[mongoServiceName + '_PASSWORD'] + mongoUser = process.env[mongoServiceName + '_USER']; + + if (mongoHost && mongoPort && mongoDatabase) { + mongoURLLabel = mongoURL = 'mongodb://'; + if (mongoUser && mongoPassword) { + mongoURL += mongoUser + ':' + mongoPassword + '@'; + } + // Provide UI label that excludes user id and pw + mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase; + mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase; + + } +} +var db = null, + dbDetails = new Object(); + +var initDb = function(callback) { + if (mongoURL == null) return; + + var mongodb = require('mongodb'); + if (mongodb == null) return; + + mongodb.connect(mongoURL, function(err, conn) { + if (err) { + callback(err); + return; + } + + db = conn; + dbDetails.databaseName = db.databaseName; + dbDetails.url = mongoURLLabel; + dbDetails.type = 'MongoDB'; + + console.log('Connected to MongoDB at: %s', mongoURL); + }); +}; + +app.get('/', function (req, res) { + // try to initialize the db on every request if it's not already + // initialized. + if (!db) { + initDb(function(err){}); + } + if (db) { + var col = db.collection('counts'); + // Create a document with request IP and current time of request + col.insert({ip: req.ip, date: Date.now()}); + col.count(function(err, count){ + res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails }); + }); + } else { + res.render('index.html', { pageCountMessage : null}); + } }); -// Any kind of message -bot.on('message', function (msg) { - var chatId = msg.chat.id; - // photo can be: a file path, a stream or a Telegram file_id - var photo = 'cats.png'; - bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'}); +app.get('/pagecount', function (req, res) { + // try to initialize the db on every request if it's not already + // initialized. + if (!db) { + initDb(function(err){}); + } + if (db) { + db.collection('counts').count(function(err, count ){ + res.send('{ pageCount: ' + count + '}'); + }); + } else { + res.send('{ pageCount: -1 }'); + } }); + +// error handling +app.use(function(err, req, res, next){ + console.error(err.stack); + res.status(500).send('Something bad happened!'); +}); + +initDb(function(err){ + console.log('Error connecting to Mongo. Message:\n'+err); +}); + +app.listen(port, ip); +console.log('Server running on http://%s:%s', ip, port); + +module.exports = app ;