Slackbot (Node): icon_emoji feature not working

Viewed 5924

My req payload to the Slack API using the icon_emoji feature of this API is not correct. The expected behavior is to have an emoji being :smile: to be displayed as my Slackbot's icon image whenever the bot posts a message to the channel. The current behavior is a default Slack image instead of the :smile:. I don't presently see what I am doing wrong. This is my fourth attempt across months at correcting this possibly so I would appreciate any advice here.

Here is my code, "as_user" has to be set to true for this to work per the documentation for posting messages.

Here is my index.js file:

const fetch = require("node-fetch"),
      config = require("../config.js"),
      icon = ":smile:";


module.exports = {
    postMessage: (message) => {
        if(!config.SLACK_CONFIG.webhook_url) {
            throw Error("Please set SLACK_MEETUP_WEBHOOK_URL");
        }

return fetch(config.SLACK_CONFIG.webhook_url, {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },

   body: JSON.stringify({ text: message, "as-user": false, "icon-emoji": icon })
}).then(res => res.text()).then((text) => {

    if(text !== "ok") {
        throw Error("Failed to post message to slack");
    }
});
}

};
5 Answers

I faced this problem, I needed to add write customize permissions to the slack app (doc)

To add it:

  1. Go to slack apps page and select the app
  2. Go to OAuth & Permissions (under Features section)
  3. In Scope subsection add chat:write.customize permission

I used the slack-node NPM module, and my code looks like

slack = new Slack();
slack.setWebhook('https://hooks.slack.com/services/JUMBLE/JUMBLE/ALONGERJUMBLE');

slack.webhook({
    channel: "#mychannel",
    username: "nametopostunder",
    text: content,
    icon_emoji: ":ship:",
}

My ship emoji posts successfully. If you want to roll your own, you could probably go read the slack-node module's code at https://github.com/clonn/slack-node-sdk#readme and figure out what magic they're using.

I didn't check slack-node, this is just an alternative solution with plain nodejs.

I tried a lot of combinations using 'hooks' endpoint, unfortunately it didn't work. Instead of 'hooks' I used https://slack.com/api/chat.postMessage with slack token.

const https = require('https');
const slackToken = 'xoxb-XXXXX-XXX'

const data = JSON.stringify({
  username: 'someUsername',
  icon_emoji: ':+1:',
  channel: '#your_channel',
  token: slackToken,
  text: 'helloWorld'
});

const options = {
  hostname: 'slack.com',
  port: 443,
  path: '/api/chat.postMessage',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    authorization: `Bearer ${slackToken}`,
  },
};

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode}`);

  res.on('data', d => {
    process.stdout.write(d);
  });
});

req.on('error', error => {
  console.error(error);
});

req.write(data);
req.end();

Did you check your display settings in slack? I encountered the same issue and changed the setting in Preferences => Messages & Media => Theme from Compact to Clean and the icon is displayed as expected.

Sometimes the icon for a message won't change becuase of 2 reasons:

First, the chat:write.customize permission is not granted as defined in the documentation:

https://api.slack.com/methods/chat.postMessage#authorship

In that case you won't get warnings or errors - just nothing happens.

Second - the because the username for the bot remains the same, the icons and messages are collapsed. This is true for both the (android) mobile app and the desktop app. On the mobile app however, you can click a message, see the detailview of the message and see the image that belongs to that particular message.

enter image description here

As we can see in that image, the first round of messages get the :information_source: icon, the icon/usernames of the rest of the messages are collapsed, but with different usernames the icons are visible.

Related