Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post mentions to slack incoming webhooks

The mentions I send to the incoming webhook renders as plain text.

Note: Sending post request using the request package.

Tried the following:

sending mentions as <@userid>

Result: <@userid> // as plain text

request.post(
       `${channels[message.channel.name]}`,
       {
           json: {
               text: 
               'To: ' + mapDiscordToSlackNames(message.mentions.users) + '\n' +
               'Discord channel: #' + message.channel.name + '\n' +
               'Link:  <' + message.url + '|Link to post>' + '\n' +

Result: To: @soda // as plain text not as mention to @soda user

Entire Code

// require the discord.js module
const Discord = require('discord.js');
const devs = require('./devs.json');
const channels = require('./channels.json');
const dotenv = require('dotenv');
const path = require('path');
var request = require('request');

dotenv.load({
  path: path.join(__dirname, `.env`),
  silent: true
});

// create a new Discord client
const client = new Discord.Client();

// Map discord usernames of devs to slack usernames
function mapDiscordToSlackNames(discordUsers) {
    return discordUsers.map( user => { 
        return '@' + devs[user.username];
     })  
}

// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', () => {
    console.log('Discord Connected!');
});

// on message on discord
client.on('message', message => {

    console.log(channels[message.channel.name]);
    request.post(
        `${channels[message.channel.name]}`,
        {
            json: {
                text: 
                'To: ' + mapDiscordToSlackNames(message.mentions.users) + '\n' +
                'Discord channel: #' + message.channel.name + '\n' +
                'Link:  <' + message.url + '|Link to post>' + '\n' + 
                'Original Message: \n' + 
                    '\t"' + message.author.username + ': ' + message.cleanContent + '"\n' + 
                    `Attachements:  ${message.attachments.map(attachment => attachment.url)}` 
             },       
        },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                console.log(body);
            }
        }
    );
});

// login to Discord with app's token
client.login(process.env.DISCORD_TOKEN);

devs is a json object which has returns slack usernames corresponding to discord usernames.

like image 376
soda Avatar asked Jun 27 '19 17:06

soda


People also ask

Is it possible to post files to Slack using the incoming webhook?

No, its is not possible to upload files through an incoming Webhook.

How do I mention Slack webhook?

You can mention users through <@memberID> as text in the Slack webhook. This will post a username as a string. For example, if there is a user named John with memberID UC6B12Z4N , then pass <@UC6B12Z4N> in the Slack webhook. This will mention @John with the correct link.

How do you mention a channel on webhook?

To send a message to a Direct Message channel, add an “@” symbol followed by the username to the channel parameter. You can add your own username to send the webhook posts to a Direct Message channel with yourself.

How do I tag someone in Slack API?

I simply add <@USER_ID> to slack plugin to mention a user in the message.


Video Answer


1 Answers

Turns out I was sending userid by escaping '<' & '>' in string like

'&lt@userid&gt' and so it was passing as plain text.

To mention someone in slack do 'To: <@' + userid + '>'

The userid starts with U and can be found after the team/ in url of your workspace eg: Cxxxxx/team/Uxxxxx/

like image 190
soda Avatar answered Oct 20 '22 02:10

soda