diff --git a/libs/notifications/telegram.js b/libs/notifications/telegram.js index c84629d0..2a842262 100644 --- a/libs/notifications/telegram.js +++ b/libs/notifications/telegram.js @@ -10,45 +10,86 @@ module.exports = function(s,config,lang,getSnapshot){ const { getEventBasedRecordingUponCompletion, } = require('../events/utils.js')(s,config,lang) + const { + getStreamDirectory + } = require('../monitor/utils.js')(s,config,lang) + const { + ffprobe + } = require('../ffmpeg/utils.js')(s,config,lang) + //telegram bot if(config.telegramBot === true){ const TelegramBot = require('node-telegram-bot-api'); try{ - const sendMessage = async function(sendBody,files,groupKey){ + const sendMessage = async function(sendBody,attachments,groupKey){ var bot = s.group[groupKey].telegramBot if(!bot){ s.userLog({ke:groupKey,mid:'$USER'},{type:lang.NotifyErrorText,msg:lang.DiscordNotEnabledText}) return } - const chatId = s.group[groupKey].init.telegrambot_channel - if(bot && bot.sendMessage){ - try{ - await bot.sendMessage(chatId, `${sendBody.title}${sendBody.description ? '\n' + sendBody.description : ''}`) - if(files){ - files.forEach(async (file) => { - switch(file.type){ - case'video': - await bot.sendVideo(chatId, file.attachment) - break; - case'photo': - await bot.sendPhoto(chatId, file.attachment) - break; - } - }) + + const sendMessageToChat = async function(chatId, files) { + if(bot && bot.sendMessage){ + try{ + await bot.sendMessage(chatId, `${sendBody.title}${sendBody.description ? '\n' + sendBody.description : ''}`) + if(files){ + await Promise.all(files.map(async (file) => { + switch(file.type){ + case'video': + if(file.hasOwnProperty("file_id") === false) { + const videoFileInfo = (await ffprobe(file.attachment,file.attachment)).result + const duration = Math.floor(videoFileInfo.streams[0].duration) + const width = videoFileInfo.streams[0].width + const height = videoFileInfo.streams[0].height + + const options = { + thumb: file.thumb, + width: width, + height: height, + duration: duration, + supports_streaming: true + } + file.file_id = (await bot.sendVideo(chatId, file.attachment, options)).video.file_id + delete file.attachment + } else { + await bot.sendVideo(chatId, file.file_id) + } + break; + case'photo': + if(file.hasOwnProperty("file_id") === false) { + file.file_id = (await bot.sendPhoto(chatId, file.attachment)).photo[0].file_id + delete file.attachment + } else { + await bot.sendPhoto(chatId, file.file_id) + } + break; + } + return file + })) + } + return files + }catch(err){ + s.debugLog('Telegram Error',err) + s.userLog({ke:groupKey,mid:'$USER'},{type:lang.NotifyErrorText,msg:err}) } - }catch(err){ - s.debugLog('Telegram Error',err) - s.userLog({ke:groupKey,mid:'$USER'},{type:lang.NotifyErrorText,msg:err}) + }else{ + s.userLog({ + ke: groupKey, + mid: '$USER' + },{ + type: lang.NotifyErrorText, + msg: lang["Check the Recipient ID"] + }) } - }else{ - s.userLog({ - ke: groupKey, - mid: '$USER' - },{ - type: lang.NotifyErrorText, - msg: lang["Check the Recipient ID"] - }) } + + const chatIds = s.group[groupKey].init.telegrambot_channel.split(",") + const resolvedFiles = await sendMessageToChat(chatIds[0], attachments) + + chatIds.forEach((chatId, index) => { + if(index < 1) return + sendMessageToChat(chatId, resolvedFiles) + }); } const onEventTriggerBeforeFilterForTelegram = function(d,filter){ filter.telegram = false @@ -98,13 +139,16 @@ module.exports = function(s,config,lang,getSnapshot){ videoName = siftedVideoFileFromRam.filename } if(videoPath){ + const thumbFile = getStreamDirectory(d) + 'thumb.jpg'; + fs.writeFileSync(thumbFile, d.screenshotBuffer) sendMessage({ title: videoName, },[ { type: 'video', attachment: videoPath, - name: videoName + name: videoName, + thumb: thumbFile } ],d.ke) } diff --git a/package-lock.json b/package-lock.json index c7f2f079..f3daef70 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,7 @@ "node-fetch": "^2.6.7", "node-onvif-events": "^2.0.5", "node-ssh": "^12.0.4", - "node-telegram-bot-api": "^0.58.0", + "node-telegram-bot-api": "^0.61.0", "nodemailer": "^6.7.1", "pam-diff": "^1.1.0", "path": "^0.12.7", @@ -5384,15 +5384,13 @@ } }, "node_modules/node-telegram-bot-api": { - "version": "0.58.0", - "resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.58.0.tgz", - "integrity": "sha512-DmP5wBON9stOiunvUw/NvTb1clMYvj+c3NnSqbPZdVd6hNkNRnM97eqPZIH4UsBJ+4n+XFGpU33dCzjqD1sv3A==", + "version": "0.61.0", + "resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.61.0.tgz", + "integrity": "sha512-BZXd8Bh2C5+uBEQuuI3FD7TFJF3alV+6oFQt8CNLx3ldX/hsd+NYyllTX+Y+5X0tG+xtcRQQjbfLgz/4sRvmBQ==", "dependencies": { "array.prototype.findindex": "^2.0.2", "bl": "^1.2.3", - "bluebird": "^3.5.1", - "debug": "^3.1.0", - "depd": "^1.1.1", + "debug": "^3.2.7", "eventemitter3": "^3.0.0", "file-type": "^3.9.0", "mime": "^1.6.0", @@ -12050,15 +12048,13 @@ } }, "node-telegram-bot-api": { - "version": "0.58.0", - "resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.58.0.tgz", - "integrity": "sha512-DmP5wBON9stOiunvUw/NvTb1clMYvj+c3NnSqbPZdVd6hNkNRnM97eqPZIH4UsBJ+4n+XFGpU33dCzjqD1sv3A==", + "version": "0.61.0", + "resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.61.0.tgz", + "integrity": "sha512-BZXd8Bh2C5+uBEQuuI3FD7TFJF3alV+6oFQt8CNLx3ldX/hsd+NYyllTX+Y+5X0tG+xtcRQQjbfLgz/4sRvmBQ==", "requires": { "array.prototype.findindex": "^2.0.2", "bl": "^1.2.3", - "bluebird": "^3.5.1", - "debug": "^3.1.0", - "depd": "^1.1.1", + "debug": "^3.2.7", "eventemitter3": "^3.0.0", "file-type": "^3.9.0", "mime": "^1.6.0", diff --git a/package.json b/package.json index ee579d19..b88c3718 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "node-fetch": "^2.6.7", "node-onvif-events": "^2.0.5", "node-ssh": "^12.0.4", - "node-telegram-bot-api": "^0.58.0", + "node-telegram-bot-api": "^0.61.0", "nodemailer": "^6.7.1", "pam-diff": "^1.1.0", "path": "^0.12.7",