【问题标题】:How to post mentions to slack incoming webhooks如何发布提及以松弛传入的 webhook
【发布时间】:2019-11-09 18:45:15
【问题描述】:

我发送到传入 webhook 的提及呈现为纯文本。

注意:使用请求包发送post请求。

尝试了以下方法:

<@userid> 发送提及

结果:<@userid> // 纯文本格式

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' +

结果:收件人:@soda // 作为纯文本而不是@soda 用户提及

完整代码

// 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 是一个 json 对象,它返回与不和谐用户名对应的松弛用户名。

【问题讨论】:

  • 请添加函数 mapDiscordToSlackNames() 以及生成的 JSON 的样子
  • 当然也提到了传入 webhook 的一般工作。问题很可能出在代码中。
  • 函数mapDiscordToSlackNames()返回@username字符串。在这个例如。它返回“@soda”,因此 json 变为 { text: 'To: @soda' }
  • 用户名不起作用。仅用户 ID
  • 是的。我也尝试以“”的形式返回 userId,但它只是以纯文本形式呈现“”

标签: javascript webhooks slack


【解决方案1】:

原来我是通过在字符串中转义 '' 来发送用户 ID,例如

'&amp;lt@userid&amp;gt',所以它作为纯文本传递。

要提及某人在 slack 中,请执行 'To: '

userid以U开头,可以在url的team/后面找到 您的工作区,例如:Cxxxxx/team/Uxxxxx/

【讨论】:

  • 更准确地说,应该在消息文本中提及&lt;@ULA15K66M&gt;,其中ULA15K66M 是用户ID。
  • 请注意,我刚刚测试过, 现在也可以了!不仅仅是用户 ID。
  • 是的,它有效。但是,最好使用 id。如果有人更改用户名,则根据用例,您可能会遇到问题。 ID 独立于用户名,即使更改用户名也不会改变。此外,对于频道提及,id 更有意义,因为在不同的范围内可以有多个同名的频道。
猜你喜欢
  • 2020-06-23
  • 1970-01-01
  • 2020-11-14
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多