'Posting message to slack get TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["User-Agent"]

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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HariKrishnaRajoli-MT