From 979ea21efa70f3e6aeb112a76fef3e74413d6d77 Mon Sep 17 00:00:00 2001 From: Joey Guerra Date: Sun, 10 Sep 2023 13:26:14 -0500 Subject: [PATCH] fix: Responding to messages when started with alias (--alias) (#26) fixes #25 --- src/bot.js | 4 ++++ test/bot.js | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/bot.js b/src/bot.js index 478cff63..dd1cbed7 100644 --- a/src/bot.js +++ b/src/bot.js @@ -379,7 +379,11 @@ class SlackBot extends Adapter { const botName = this.self.user; const text = event.text ?? event.message?.text ?? ''; if(text.includes(`<@${botId}>`)) { + if(this.robot.alias) { + return text.replace(`<@${botId}>`, `${this.robot.alias}`); + } else { return text.replace(`<@${botId}>`, `@${botName}`); + } } return text; } diff --git a/test/bot.js b/test/bot.js index b784df47..030ff7b4 100644 --- a/test/bot.js +++ b/test/bot.js @@ -1,6 +1,7 @@ const {describe, it, beforeEach, before, after} = require('node:test'); const assert = require('node:assert/strict'); const Module = require('module'); +const SlackBot = require('../src/bot.js'); const hookModuleToReturnMockFromRequire = (module, mock) => { const originalRequire = Module.prototype.require; @@ -349,6 +350,18 @@ describe('Handling incoming messages', function() { slackbot.eventHandler({body: { event: { text: 'foo', type: 'message', user: stubs.user.id, channel_type: 'im' }}, event: { text: 'foo', type: 'message', user: stubs.user.id, channel_type: 'im', channel:stubs.DM.id }}); }); + it('Should preprend our alias to a name-lacking message addressed to us in a DM', function(t, done) { + const bot = new SlackBot({alias: '!', logger: {info(){}, debug(){}}}, {appToken: ''}); + bot.self = { + user_id: '1234' + } + const text = bot.replaceBotIdWithName({ + text: '<@1234> foo', + }) + assert.deepEqual(text, '! foo'); + done() + }); + it('Should NOT prepend our name to a name-containing message addressed to us in a DM', function(t, done) { const bot_name = slackbot.robot.name; stubs.receiveMock.onReceived = function(msg) {