howdyai / botkit

Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
MIT License
11.39k stars 2.28k forks source link

Multiple slack messages posted #2175

Closed NiklasBorglund closed 2 years ago

NiklasBorglund commented 2 years ago

Hi! I'm writing a super simple bot to take one message that's bloated and reposting a less bloated one in another channel. But I can't seem to get it to work properly.

What was the result you received?

I'm getting multiple messages posted in slack when I use the reply method

What did you expect?

I just want one message in slack.

Screenshots and animated GIFs

I want to take this type of message: image

And make it into this: image

But i get multiple messages and it looks like this: image

Context:

I'm writing a super simple bot just taking a bloated slack message from the Unity 3D Engine Service->rewrite it to be more condensed and post it in another channel.

The original message from unity is posted into a channel named rawbugreports. Depending on the content of the message it till be posted into either a bugreports channel or bugreports-master channel in slack.

I cannot figure out what's wrong here. To me the code looks fine. Anyone knows? Or is it a bug?

The git repo for the bot: https://github.com/NiklasBorglund/UnityUserReportingSlackBot/blob/main/start/bugreportersqueezer.js

I used this guide to create the bot: https://codelabs.developers.google.com/codelabs/cloud-slack-bot#0

Here are the slack scopes: image

Here are the event subscriptions: image

Here's the code for the bot:

async function init () {
    const adapter = new SlackAdapter({
        clientSigningSecret: await accessSecretVersion('client-signing-secret'),
        botToken: await accessSecretVersion('bot-token')
    });

    adapter.use(new SlackEventMiddleware())

    const controller = new Botkit({
        webhook_uri: '/api/messages',
        adapter: adapter
    })

    controller.ready(() =>
    {
        controller.on('bot_message', async(bot, event) => {
            return new Promise(async(resolve, reject) => {
                resolve('resolved!');
                if (event.attachments !== undefined && event.attachments.length > 0) {
                    var attachment = event.attachments[0];
                    if (attachment.author_name === 'Unity User Reporting') {
                        var channel = 'bugreports';
                        var fixedTitle = attachment.title;

                        if (fixedTitle.includes('[MASTER]')) {
                            fixedTitle = fixedTitle.replace('[MASTER]', '');
                            channel = 'bugreports-master';
                        }

                        await bot.changeContext(event.reference);
                        var fullSlackMessage = '<' + attachment.title_link + '|' + fixedTitle + '>';
                        await bot.reply(event, {channel: channel, text: fullSlackMessage, unfurl_links: false});
                    }
                }
            });
        });
    })
}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

benbrown commented 2 years ago

Why are you wrapping your response code in an additional promise? I think that might be the problem.

NiklasBorglund commented 2 years ago

The extra promise was just a desperate attempt to try fix the issue - it was an issue before I had that in. I just forgot to remove it.

I managed to solve the issue by disabling retries like this:

const controller = new Botkit({
        webhook_uri: '/api/messages',
        adapter: adapter,
        webserver_middlewares: [
            (req, res, next) => {
                if (req.headers['x-slack-retry-num']) {
                    res.status(200).send()
                    return
                }
                next()
            }]
   })

And then when I got the message, I immediately set the status to 200 as well with: bot.httpStatus(200). Not sure if this is the best way to solve it. First slack-bot I've ever written, but it solved the issue for me

benbrown commented 2 years ago

Ah, Thanks for adding what worked for you!