Update server.js
This commit is contained in:
parent
3d4829f8e4
commit
894d743e81
1 changed files with 15 additions and 103 deletions
118
server.js
118
server.js
|
@ -1,108 +1,20 @@
|
||||||
|
var TelegramBot = require('node-telegram-bot-api');
|
||||||
|
|
||||||
// OpenShift sample Node application
|
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
|
||||||
var express = require('express'),
|
// Setup polling way
|
||||||
fs = require('fs'),
|
var bot = new TelegramBot(token, {polling: true});
|
||||||
app = express(),
|
|
||||||
eps = require('ejs'),
|
|
||||||
morgan = require('morgan');
|
|
||||||
|
|
||||||
Object.assign=require('object-assign')
|
|
||||||
|
|
||||||
app.engine('html', require('ejs').renderFile);
|
// Matches /echo [whatever]
|
||||||
app.use(morgan('combined'))
|
bot.onText(/\/echo (.+)/, function (msg, match) {
|
||||||
|
var fromId = msg.from.id;
|
||||||
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
var resp = match[1];
|
||||||
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
|
bot.sendMessage(fromId, resp);
|
||||||
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) {
|
// Any kind of message
|
||||||
// try to initialize the db on every request if it's not already
|
bot.on('message', function (msg) {
|
||||||
// initialized.
|
var chatId = msg.chat.id;
|
||||||
if (!db) {
|
// photo can be: a file path, a stream or a Telegram file_id
|
||||||
initDb(function(err){});
|
var photo = 'cats.png';
|
||||||
}
|
bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
|
||||||
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 ;
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue