commit 6e8519196fe00efd2052b1039200d4e081fa7f23 Author: Sagi Dayan Date: Fri Aug 18 13:04:14 2017 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2b5f34 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..2119fa8 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + "esnext": true +} diff --git a/config.js b/config.js new file mode 100644 index 0000000..b3a8514 --- /dev/null +++ b/config.js @@ -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 + } +} + diff --git a/index.js b/index.js new file mode 100644 index 0000000..71a8c2a --- /dev/null +++ b/index.js @@ -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); + } +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..1f31714 --- /dev/null +++ b/package.json @@ -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" + } +}