如何 post 现有 DM 中的消息作为 Slack 机器人
How to post a message in an existing DM as a Slack Bot
我对 Slack Web chat.postMessage 方法的理解 API 是,作为具有机器人令牌的机器人,您可以 post 向 public 频道发送消息.但是,如果您想 post 到直接消息 - 您需要请求用户令牌,然后代表用户 post
但是,我使用了一些能够 post 作为应用程序进入 DM 的应用程序(因此,我假设它们使用的是机器人令牌)。对我来说这是理想的,所以你不会窃听 Slack 工作区中的每个人来获取他们的用户令牌
谁能告诉我这是怎么做到的?
作为参考,这是我用来 post 作为机器人发送消息的代码。它不适用于 DM 或未邀请机器人加入的私人频道。很想解决这个问题。谢谢
function getQueryString(data = {}) {
return Object.entries(data)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
}
function postMessageInSlack(bot_token, channelID, userID, message, send_blocks, endpoint) {
const options = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
};
const data = {
token: bot_token,
channel: channelID,
text: `${message} from <@${userID}>`,
blocks: JSON.stringify(send_blocks),
}
delete axios.defaults.headers.common["Content-Type"];
axios.post(endpoint,
data, {
options,
transformRequest: getQueryString}
)
.then(res => console.log("result is ", res))
.catch(err => console.log("the error is ", err))
}
您需要打开与用户的新对话,前提是 none 打开了。为此,您需要 conversations.open
method. This will return a response that contains the conversation id. You can now use the conversation id in place of the channel id in your chat.postMessage
方法。
我对 Slack Web chat.postMessage 方法的理解 API 是,作为具有机器人令牌的机器人,您可以 post 向 public 频道发送消息.但是,如果您想 post 到直接消息 - 您需要请求用户令牌,然后代表用户 post
但是,我使用了一些能够 post 作为应用程序进入 DM 的应用程序(因此,我假设它们使用的是机器人令牌)。对我来说这是理想的,所以你不会窃听 Slack 工作区中的每个人来获取他们的用户令牌
谁能告诉我这是怎么做到的?
作为参考,这是我用来 post 作为机器人发送消息的代码。它不适用于 DM 或未邀请机器人加入的私人频道。很想解决这个问题。谢谢
function getQueryString(data = {}) {
return Object.entries(data)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
}
function postMessageInSlack(bot_token, channelID, userID, message, send_blocks, endpoint) {
const options = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
};
const data = {
token: bot_token,
channel: channelID,
text: `${message} from <@${userID}>`,
blocks: JSON.stringify(send_blocks),
}
delete axios.defaults.headers.common["Content-Type"];
axios.post(endpoint,
data, {
options,
transformRequest: getQueryString}
)
.then(res => console.log("result is ", res))
.catch(err => console.log("the error is ", err))
}
您需要打开与用户的新对话,前提是 none 打开了。为此,您需要 conversations.open
method. This will return a response that contains the conversation id. You can now use the conversation id in place of the channel id in your chat.postMessage
方法。