将消息发布到 slack get TypeError [ERR_INVALID_CHAR]:标头内容中的字符无效 [“User-Agent”]

发布于 2025-01-11 10:33:34 字数 1041 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日浅笑〃 2025-01-18 10:33:34

感谢我也是 Joe 提出的宝贵的讨论决议。发布为帮助其他社区成员的答案:

您错过了 const result = wait client.chat.postMessage(

中的左括号

很高兴@JimBurke,您已经通过更正语法/转录解决了自己的问题。

Thank you I'm Joe Too for your valuable discussed resolution. Posting as an answer to help other community members:

You missed an open bracket in const result = await client.chat.postMessage(

Glad @JimBurke, that you have solved yourself by correcting the syntax/transcription.

南巷近海 2025-01-18 10:33:34

这不是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文