Skip to content

Commit

Permalink
fix: Responding to messages when started with alias (--alias) (#26)
Browse files Browse the repository at this point in the history
fixes #25
  • Loading branch information
joeyguerra authored Sep 10, 2023
1 parent eadae1b commit 979ea21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions test/bot.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 979ea21

Please sign in to comment.