send method

Future<String> send(
  1. String text, {
  2. String? channel,
  3. String? iconEmoji,
  4. String? iconUrl,
  5. String? username,
  6. List<Block>? blocks,
  7. List<Attachment>? attachments,
  8. Map<String, String>? httpHeaders,
})

Sends a message to a channel.

One of these arguments is required to describe the content of the message. If attachments or blocks are included, text will be used as fallback text for notifications only.

Implementation

Future<String> send(
  String text, {
  String? channel,
  String? iconEmoji,
  String? iconUrl,
  String? username,
  List<Block>? blocks,
  List<Attachment>? attachments,
  Map<String, String>? httpHeaders,
}) async {
  var webhookUrl = token.startsWith('https')
      ? token
      : 'https://hooks.slack.com/services/$token';

  var body = <String, dynamic>{'text': text, 'link_names': true};
  if (channel != null) body['channel'] = channel;
  if (iconEmoji != null) body['icon_emoji'] = iconEmoji;
  if (iconUrl != null) body['icon_url'] = iconUrl;
  if (username != null) body['username'] = username;
  if (blocks != null) body['blocks'] = blocks;
  if (attachments != null) body['attachments'] = attachments;

  final response = await http.post(
    Uri.parse(webhookUrl),
    headers:
        httpHeaders ?? {'Content-Type': 'application/x-www-form-urlencoded'},
    body: jsonEncode(body),
  );
  return response.body;
}