'Slack bot can't send a message to a channel with - or space like "channel-name"

my slack bot can't send a message to a channel with space or - like "channel-name" but works well for a one word channel.

  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "https://slack.com/api/chat.postMessage",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS =>"{ \"channel\": \"channel-withspace\",\"text\": \"hello\"}",
    CURLOPT_HTTPHEADER => array(
      "Content-Type: application/json",
      "Authorization: Bearer <here>"
    ),
  ));

  $response = curl_exec($curl);

  curl_close($curl);
  echo $response;

Here is the error:

{"ok":false,"error":"not_in_channel","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}


Solution 1:[1]

Found the answer, turns out I need to invite the bot by typing a message in channel:

/invite @channel-name

Found the answer here: https://stackoverflow.com/a/61369364/5159914

Solution 2:[2]


You can add following scope to your bot to post messages to public channel without inviting the bot to the channel.

Scope : chat:write.public

Send messages to channels @your_slack_app isn't a member of


https://api.slack.com/scopes/chat:write.public

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 KristCont
Solution 2 Suyash Gaur