有一个积极运行的节点js脚本检测新文件何时进入目录并采取相应行动

问题描述 投票:0回答:1

我有一个简单的 Node js 脚本,它使用 whatsapp web js 向聊天发送消息。

我有另一个Python脚本连接到一个相机,它将运行对象检测,当它检测到一只猫时,它会拍摄一张照片并将所述照片存放在一个文件夹中。

我想要的:我想要的js脚本是检测何时将new照片存入此文件夹,然后消息说聊天。

我已经关闭了消息传递部分,这是脚本已经运行时的检测部分,这就是问题所在。这是我的代码:

正如您所看到的,这个 js 脚本可以读取图像的目录并将其发送出去,但这是一次性的事情。对目录的任何新修改都不会影响脚本。

const { Client, MessageMedia } = require('whatsapp-web.js');
const Client = new Client();
//...
//Logging into whatsapp stuff I'm omitting for brevity
//...
client.on("ready", () => {
    console.log('Client is ready!');
    client.getChats().then((chats) => {
               //grouptarget will be the target chat that the messages are sent to
        const grouptarget = chats.find((chat) => chat.name === '<Chat Name>'); 
        const catpic = MessageMedia.fromFilePath("./output.jpg");
        grouptarget.sendMessage(catpic, {caption: "beep boop cat detected"});
        console.log("Message Sent Successfully");
    });
});

client.initialize();

这对于一次性的事情来说效果很好,但我希望这是一个持续的过程。

我今天基本上开始学习 javascript,所以我希望得到一些指点。

或者:可以使用python脚本(进行图像识别和图像保存)来在保存图像后运行js脚本。但是(a)这需要每次都登录whatsapp web,这意味着(为了避免二维码恶作剧)我必须设置一些让我困惑的身份验证内容,并且(b)我真的不知道该怎么做(但如果这是更简单的选择,我对此持开放态度)。

让我知道我应该做什么!

javascript python node.js network-programming whatsapp
1个回答
0
投票

据我了解您的问题是为了实现对新图像目录的持续监控并在检测到新图像时发送消息,您可以使用 Node.js 中的

fs
(文件系统)模块来监视目录中的更改。以下是如何修改现有脚本来实现此目的的示例:

const { Client, MessageMedia } = require('whatsapp-web.js');
const fs = require('fs');
const path = require('path');

const client = new Client();
const groupTargetName = '<Chat Name>';
const folderPath = './images';  // Change this to the path where your images are saved

client.on("ready", () => {
    console.log('Client is ready!');
    const groupTarget = client.getChats().find(chat => chat.name === groupTargetName);

    // Function to send message with the latest image
    const sendLatestImage = () => {
        const latestImagePath = getLatestImage(folderPath);
        if (latestImagePath) {
            const catPic = MessageMedia.fromFilePath(latestImagePath);
            groupTarget.sendMessage(catPic, { caption: "beep boop cat detected" });
            console.log("Message Sent Successfully");
        }
    };

    // Watch the folder for changes
    fs.watch(folderPath, (event, filename) => {
        if (event === 'change' && filename) {
            console.log(`File ${filename} has been changed.`);
            sendLatestImage();
        }
    });

    // Initial message sending
    sendLatestImage();
});

// Function to get the latest image in the folder
function getLatestImage(folderPath) {
    const files = fs.readdirSync(folderPath);
    const imageFiles = files.filter(file => /\.(jpg|jpeg|png)$/i.test(file));
    if (imageFiles.length > 0) {
        const latestImage = path.join(folderPath, imageFiles[imageFiles.length - 1]);
        return latestImage;
    }
    return null;
}

client.initialize();

此脚本现在使用

fs.watch
监视指定文件夹的更改。当检测到变化(例如添加新图像)时,将触发
sendLatestImage
功能,将文件夹中的最新图像发送到指定的聊天室。

请务必将

'<Chat Name>'
'./images'
分别替换为您的实际群聊名称和图片保存路径。

此外,请记住,WhatsApp Web API 可能有限制或限制,因此请负责任地使用它并遵守 WhatsApp 的服务条款。

© www.soinside.com 2019 - 2024. All rights reserved.