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.
No, its is not possible to upload files through an incoming 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.
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.
I simply add <@USER_ID> to slack plugin to mention a user in the message.
Turns out I was sending userid by escaping '<' & '>' in string like
'<@userid>'
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With