我正在尝试使用slack web api向用户发送直接消息,但我认为获取所有可用用户的getSlackUser 方法在我调用 slackId 时没有及时完成;
console.log(slackId) 给出了未定义的意思,它没有用bolt完成我的 api 调用
如何确保 getSlackUser 方法在继续执行其他方法之前完成(使其阻塞)?
const { WebClient } = require('@slack/web-api');
const { App } = require('@slack/bolt')
const rtm = new RTMClient(process.env.SLACK_OAUTH_TOKEN);
const web = new WebClient(process.env.SLACK_OAUTH_TOKEN);
const app = new App({
token: process.env.SLACK_OAUTH_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
exports.handler = async (event) => {
const slackId = await getSlackUser('example_real_name').id;
console.log(slackId);
await sendSlackMessage(slackId, 'Bot message');
}
sendSlackMessage = async (channel, message) => {
await web.chat.postMessage({
channel: channel,
text: message,
as_user: true
});
}
getSlackUser = async(real_name) => {
const result = await app.client.users.list({
token: process.env.SLACK_OAUTH_TOKEN
});
console.log(result);
return result.members.find((user) => user.real_name == real_name);
}
慕标琳琳
相关分类