SlackBot重复事件而无需控制

发布于 2025-02-10 20:26:23 字数 2509 浏览 0 评论 0原文

我正在尝试在Slack中创建一个名为“ Chochon”的机器人,问题是当我收到事件“ App_mention”时,Chochon连续几次响应一次,而不是发送1个消息并停止直到他们再次提及它。

这是我的代码,chochon函数:

    socketModeClient.on('app_mention', async ({ event }) => {
        try {
            console.log(event);
            let userBox = await Chochon.users.info({ user: event.user });
            let userProfile = userBox.user.profile;
            console.log(cli.green(`Event received : [ ${event.type} ] from [ ${userProfile.display_name} ]`));

            // Respond to the event
            Chochon.chat.postMessage({
                channel: event.channel,
                text: `Hello <@${event.user}>, I'm Chochon!, I'm a bot that can help you to manage your team.`
            });

        } catch (error) {
            console.error(error);
        }
    });

slack client:

“

完整代码:

// Dependencies :
const dotenv = require('dotenv').config();
const path = require('path');
const cli = require('cli-color');

// Web client [CLI]
const { WebClient } = require('@slack/web-api');
const Chochon = new WebClient(process.env.SLACK_BOT_TOKEN.trim());  

// Socket IO
const { SocketModeClient } = require('@slack/socket-mode');
const appToken = process.env.SLACK_APP_TOKEN;
const socketModeClient = new SocketModeClient({ appToken });
socketModeClient.start();

// Internal functions
//const eventManager = require(path.resolve(__dirname, './utils/events/manager'));

socketModeClient.on('app_mention', async ({ event }) => {
    try {
        console.log(event);
        let userBox = await Chochon.users.info({ user: event.user });
        let userProfile = userBox.user.profile;
        console.log(cli.green(`Event received : [ ${event.type} ] from [ ${userProfile.display_name} ]`));

        // Respond to the event
        Chochon.chat.postMessage({
            channel: event.channel,
            text: `Hello <@${event.user}>, I'm Chochon!, I'm a bot that can help you to manage your team.`
        });

    } catch (error) {
        console.error(error);
    }
});

socketModeClient.on('slash_commands', async ({ body, ack }) => {
    if (body.command === "/ping") {
        console.log(cli.green(`Event received : [ ${body.command} ]`));
        await ack({"text": "I got it, pong!"});
    }
});

I am trying to create a bot in Slack called "Chochon", the problem is that when I receive the event "app_mention", chochon responds more than once several times in a row, instead of sending 1 message and stopping until they mention it again.

This is my code, chochon function:

    socketModeClient.on('app_mention', async ({ event }) => {
        try {
            console.log(event);
            let userBox = await Chochon.users.info({ user: event.user });
            let userProfile = userBox.user.profile;
            console.log(cli.green(`Event received : [ ${event.type} ] from [ ${userProfile.display_name} ]`));

            // Respond to the event
            Chochon.chat.postMessage({
                channel: event.channel,
                text: `Hello <@${event.user}>, I'm Chochon!, I'm a bot that can help you to manage your team.`
            });

        } catch (error) {
            console.error(error);
        }
    });

The slack client:

Slack channel

Full code:

// Dependencies :
const dotenv = require('dotenv').config();
const path = require('path');
const cli = require('cli-color');

// Web client [CLI]
const { WebClient } = require('@slack/web-api');
const Chochon = new WebClient(process.env.SLACK_BOT_TOKEN.trim());  

// Socket IO
const { SocketModeClient } = require('@slack/socket-mode');
const appToken = process.env.SLACK_APP_TOKEN;
const socketModeClient = new SocketModeClient({ appToken });
socketModeClient.start();

// Internal functions
//const eventManager = require(path.resolve(__dirname, './utils/events/manager'));

socketModeClient.on('app_mention', async ({ event }) => {
    try {
        console.log(event);
        let userBox = await Chochon.users.info({ user: event.user });
        let userProfile = userBox.user.profile;
        console.log(cli.green(`Event received : [ ${event.type} ] from [ ${userProfile.display_name} ]`));

        // Respond to the event
        Chochon.chat.postMessage({
            channel: event.channel,
            text: `Hello <@${event.user}>, I'm Chochon!, I'm a bot that can help you to manage your team.`
        });

    } catch (error) {
        console.error(error);
    }
});

socketModeClient.on('slash_commands', async ({ body, ack }) => {
    if (body.command === "/ping") {
        console.log(cli.green(`Event received : [ ${body.command} ]`));
        await ack({"text": "I got it, pong!"});
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文