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

Slack Block Actions keeps repeating #2173

Closed Mattw11486 closed 2 years ago

Mattw11486 commented 2 years ago

I have a weird issue where block actions keep adding an additional repeated message per each conversation. For example I have the bot responding to both the keywords 'vpn' or 'password reset'. After it responds to the message it also then provides a yes or no block action button to select to see if the response was helpful or not.

If I reboot the bot and select 'yes' or 'no' it responds once, however if I then ask the question again and select yes or no it repeats the response twice. If I ask again and select 'yes' or 'no' it repeats it 3 times again 4 times, etc etc. It will continue to add an additional repeat until I reboot the bot again. Is there a way to solve this?

See attached screenshot for example: image

 //Password Reset
    controller.hears('password reset', 'message,direct_message,mention,direct_mention', async(bot, message) => {
        const Messages = new Msg ({heard:message.text});
        await Messages.save();
        await bot.replyEphemeral(message,{
            blocks: [
                {
                    "type": "image",
                    "title": {
                      "type": "plain_text",
                      "text": "Steps for changing your password."
                    },
                    "block_id": "image4",
                    "image_url": "https://www.linkpicture.com/q/index_7.png",
                    "alt_text": "How To Change Your Password."
                  },
                  {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "*Did you find this helpful?*"
                    }
                },
                {
                    "type": "actions",
                     "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Yes",
                        "emoji": true
                    },
                    "value": "Yes"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "No",
                        "emoji": true
                    },
                    "value": "No"
                },
            ]
        }
    ]
        });
        controller.on('block_actions', async (bot, message) => {
            if (message.incoming_message.channelData.actions[0].value === 'No'){
               await bot.replyEphemeral(message, 'If you need further assistance please reach out to us on chat at  or call .');
               await Messages.updateOne({resolved:message.incoming_message.text});
           } else if (message.incoming_message.channelData.actions[0].value === 'Yes'){
               await bot.replyEphemeral(message, 'Great! Is there anything else I can assist you with today?');
               await Messages.updateOne({resolved:message.incoming_message.text});
          }
        });
   });
Mattw11486 commented 2 years ago

One thing to note is that I am trying to link this up with MongoDB. The reason being is so that it can store what the bot heard, along with if the response to the user answered their question. This is why the block actions resides within the controller.hears function. That way when/if a user selects "Yes" or "No" Button it updates to the collection in Mongo. If I remove the controller.on outside of controller.hears I do not get the repeat issue..however, I also do not get the resolution updated to Mongo. At least I haven't found a better way to do this.

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

Sorry for the very slow response here @Mattw11486 ---

the problem is that you are defining the handler for block_actions inside of another handler. That means it gets duplicated each time the handler is fired.

Make sure all your controller.on and controller.hears statements are at the same level of code - never inside each other.