botkit-docs icon indicating copy to clipboard operation
botkit-docs copied to clipboard

Why does botkit keep repeating itself in Slack?

Open Mattw11486 opened this issue 2 years ago • 0 comments

The issue is with controller.hears('keyword', 'message') If its being used with the message and the bot responds with that keyword it keeps replying back to it's own message. For example, if the bot's reply is bot.reply(message, 'keyword); then it will just keep repeating keyword over and over The only workarounds I have found for this is to either:

  1. user 'direct_message, mention, direct_mention'
  2. use reply messages that don't contain the keywords

The problem with method 1 is that I can't use a common.js file to work for both Teams/Slack/Discord, I have to create separate .js files for each channel to do the same exact things.

Method 2 will work but I am worried that if it responds to another message (like a random message from my ChatGPT integration) that does contain a keyword for some other controller.hears message it will cause an issue of it responding unnecessarily.

I am thinking the only thing to do is to have it listen for it's own bot identity or app ID and then return if it sees itself but I am not really sure how to find the Bot ID. If its the App ID in api.slack that did not work for me.

image .

`	//Stock Quotes
	controller.hears('stock price', 'message,direct_message,direct_mention,mention', async function(bot, message) {
		const stock = message.text.match(/stock price of (\w+)/i)[1];;
		const url = `https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${stock}&apikey=${process.env.STOCKAPI}`;
		try {
			const body = await rp({ uri: url, json: true });
			if (body["Error Message"]){
				bot.reply(message, 'Invalid stock symbol');
				return;
			}
			const stockPrice = body["Global Quote"]["05. price"];
			const roundedStockPrice = Math.round(stockPrice * 100) / 100;
			bot.reply(message, `The latest stock price of ${stock} is $${roundedStockPrice}`);
			return;
		} catch (err) {
			console.log(err);
			bot.reply(message, 'Sorry, I was unable to retrieve the stock price.');
		}
	});`

Mattw11486 avatar Feb 02 '23 05:02 Mattw11486