Initial commit

This commit is contained in:
Sagi Dayan 2017-08-18 13:04:14 +03:00
commit 6e8519196f
5 changed files with 204 additions and 0 deletions

86
.gitignore vendored Normal file
View file

@ -0,0 +1,86 @@
# Created by https://www.gitignore.io/api/node,linux
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# Yarn Lock file
yarn.lock
# package.json Lock file
package-lock.json
# dotenv environment variables file
.env
# End of https://www.gitignore.io/api/node,linux

3
.jshintrc Normal file
View file

@ -0,0 +1,3 @@
{
"esnext": true
}

18
config.js Normal file
View file

@ -0,0 +1,18 @@
/**
* Configuration
*/
module.exports = {
botId: "443965284:AAEKdvDxwxwbn4eOYJ6wt6lgR2J-hDAwSeA",
slideShowFolderPath: '/media/64C6A9C4724C63E5/slideshow/', //Ends with '/'
permissions: {
allowAll: false, // allow any telegram user to add photos
usernames: [], // strings
userIds: [247153816], //numbers
},
admin: {
username: 'SagiDayan',
notifyActivity: true,
id: 73947511
}
}

83
index.js Normal file
View file

@ -0,0 +1,83 @@
const TelegramBot = require('node-telegram-bot-api');
const config = require('./config');
// Be sure to replace YOUR_BOT_TOKEN with your actual bot token on this line.
let telegram = new TelegramBot(config.botId, {
polling: true
});
let admin_chatId = config.admin.id;
const SLIDESHOW_LOCATION = config.slideShowFolderPath; // Ends with '/'
telegram.on('message', (msg) => {
const chatId = msg.chat.id;
if (userAllowd(msg)) {
handleMessage(msg);
} else {
telegram.sendMessage(chatId, 'Sorry ' + msg.from.username + ' My master says I can\'t talk to strangers...');
console.log('Unauthorized user: ', msg.from.username, 'Message:');
console.log(JSON.stringify(msg, null, 2));
notifyAdmin('Unauthorized user: ' + msg.from.username + ' Message:\n' + JSON.stringify(msg, null, 2));
}
});
// Location Of HardDrive - /media/64C6A9C4724C63E5/slideshow
function storeFile(fileId, callback) {
telegram.downloadFile(fileId, SLIDESHOW_LOCATION)
.then((filePath) => {
callback(null, "YAYYY!!! file saved at: " + filePath);
})
.catch((err) => {
callback(err);
});
}
let storeFileCallback = (err, message, chatId, photo_id) => {
if (err) {
telegram.sendMessage(chatId, 'Failed to store your photo! - ' + err);
if (admin_chatId !== chatId) notifyAdmin('Hi Master!!,\nA Failed attemt to save a file to slideshow', photo_id);
} else {
telegram.sendMessage(chatId, message);
if (admin_chatId !== chatId) notifyAdmin('Hi Master!!,\nA photo was saved to your slideshow', photo_id);
}
};
let handleMessage = (msg) => {
const chatId = msg.chat.id;
if (msg.photo || (msg.document && msg.document.mime_type.indexOf('image/') === 0)) {
if (msg.photo) {
storeFile(msg.photo[msg.photo.length - 1].file_id, (err, message) => {
storeFileCallback(err, message, chatId, msg.photo[msg.photo.length - 1].file_id);
});
} else { // document - uncompressed photo
storeFile(msg.document.file_id, (err, message) => {
storeFileCallback(err, message, chatId, msg.document.file_id);
});
}
} else if(msg.contact) {
notifyAdmin('User with ID of: ' + msg.contact.user_id + ' Should be added/removed!');
}
else {
// send a message to the chat acknowledging receipt of their message
telegram.sendMessage(chatId, 'Hey dude... I can only help you save photos to your home slideshow.... other functions later :)');
}
};
let userAllowd = (msg)=>{
if(msg.from.username === config.admin.username) {
admin_chatId = msg.chat.id;
return true;
}
if(config.permissions.allowAll) return true;
if(config.permissions.usernames.indexOf(msg.from.username) >= 0) return true;
if(config.permissions.userIds.indexOf(msg.from.id) >= 0) return true;
return false;
};
let notifyAdmin = (msg, photo_id) => {
console.log(admin_chatId);
if (admin_chatId != null && config.admin.notifyActivity){
telegram.sendMessage(admin_chatId, msg);
if(photo_id) telegram.sendPhoto(admin_chatId, photo_id);
}
};

14
package.json Normal file
View file

@ -0,0 +1,14 @@
{
"name": "telegram_bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"node-telegram-bot-api": "^0.27.1"
}
}