Support multiple chat ids

node-20
skvalex 2023-07-30 23:19:02 +07:00
parent ea5f8ed0c8
commit 152ab4f0d6
1 changed files with 60 additions and 38 deletions

View File

@ -21,20 +21,22 @@ module.exports = function(s,config,lang,getSnapshot){
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
const sendMessageToChat = async function(chatId, files) {
if(bot && bot.sendMessage){
try{
await bot.sendMessage(chatId, `${sendBody.title}${sendBody.description ? '\n' + sendBody.description : ''}`)
if(files){
files.forEach(async (file) => {
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
@ -47,14 +49,25 @@ module.exports = function(s,config,lang,getSnapshot){
duration: duration,
supports_streaming: true
}
await bot.sendVideo(chatId, file.attachment, options)
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':
await bot.sendPhoto(chatId, file.attachment)
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})
@ -69,6 +82,15 @@ module.exports = function(s,config,lang,getSnapshot){
})
}
}
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
}