将消息发布到 slack get TypeError [ERR_INVALID_CHAR]:标头内容中的字符无效 [“User-Agent”]
我有一个使用 NodeJS 作为 Azure 函数运行的工作 Slack 应用程序。它正在将有效负载很好地发布到我的频道。我还希望该函数通过 client.chat.postMessage 将消息发布到频道。据我所知,我已经正确设置了所有内容,但是每当我尝试发布消息时,我都会收到错误:
TypeError [ERR_INVALID_CHAR]:标头内容中的无效字符[“User-Agent”]
发布消息的代码是:
const { WebClient, LogLevel } = require("@slack/web-api");
const client = new WebClient(process.env['BOT_USER_OAUTH_TOKEN'], {
// LogLevel can be imported and used to make debugging simpler
logLevel: LogLevel.DEBUG
});
const channelId = "C0319MTLHB8";
try {
// Call the chat.postMessage method using the WebClient
const result = await client.chat.postMessage({
channel: channelId,
text: "Hello world"
});
console.log(result);
} catch (error) {
context.res = {
// status: 200, /* Defaults to 200 */
body: "error: " + error
};
}
这段代码位于 module.exports 中。
我猜有些东西不喜欢 BOT_USER_OAUTH_TOKEN 的内容,但这是 xoxb 机器人用户 oauth 令牌的直接副本。其形式为:
xoxb-999999999999999-9999999999999-aBunchOfUpperAndLowerCaseCharacters
关于我做错了什么有什么建议吗?
I have a working slack app running as an Azure Function using NodeJS. It is posting payloads fine to my channel. I would also like the Function to post a message to the channel via client.chat.postMessage. As far as I can see I have set everything up correctly but whenever I attempt to post the message I get an error:
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["User-Agent"]
The code to post the message is:
const { WebClient, LogLevel } = require("@slack/web-api");
const client = new WebClient(process.env['BOT_USER_OAUTH_TOKEN'], {
// LogLevel can be imported and used to make debugging simpler
logLevel: LogLevel.DEBUG
});
const channelId = "C0319MTLHB8";
try {
// Call the chat.postMessage method using the WebClient
const result = await client.chat.postMessage({
channel: channelId,
text: "Hello world"
});
console.log(result);
} catch (error) {
context.res = {
// status: 200, /* Defaults to 200 */
body: "error: " + error
};
}
and this piece of code sits within module.exports.
I guess something doesn't like the contents of BOT_USER_OAUTH_TOKEN but this is a direct copy of the xoxb bot user oauth token. And is of the form:
xoxb-999999999999999-9999999999999-aBunchOfUpperAndLowerCaseCharacters
Any suggestions as to what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢我也是 Joe 提出的宝贵的讨论决议。发布为帮助其他社区成员的答案:
Thank you I'm Joe Too for your valuable discussed resolution. Posting as an answer to help other community members:
这不是 Node.js 16 LTS 吗?
我遇到了类似的问题,但我制作了 Node.js 14 LTS 并且它有效
Isn't it Node.js 16 LTS?
I had a similar problem, But I made Node.js 14 LTS and it worked