sendLinkAndQr method

Future<void> sendLinkAndQr({
  1. required String slackBotToken,
  2. required int fileSizeInBytes,
  3. required File file,
  4. required String channelId,
  5. required String message,
  6. required String downloadLink,
  7. bool? isShareQR,
  8. bool? isShareDownloadLink,
  9. List<String>? mentions,
})

Implementation

Future<void> sendLinkAndQr({
  required String slackBotToken,
  required int fileSizeInBytes,
  required File file,
  required String channelId,
  required String message,
  required String downloadLink,
  bool? isShareQR,
  bool? isShareDownloadLink,
  List<String>? mentions,
}) async {
  if (isShareQR == true) {
    await _uploadQrCode(
      slackBotToken: slackBotToken,
      fileSizeInBytes: fileSizeInBytes,
      file: file,
      channelId: channelId,
      message: message,
      downloadLink: downloadLink,
    );
  }
  try {
    // Step 4: Send the message with link and mentions
    final formattedMentions = mentions?.map((id) => '<@$id>').join(' ') ?? '';
    final finalMessage = '$message\n$formattedMentions';

    final messageResponse = await dio.post(
      'https://slack.com/api/chat.postMessage',
      options: Options(
        headers: {
          'Authorization': 'Bearer $slackBotToken',
          'Content-Type': 'application/json',
        },
      ),
      data: jsonEncode(
        {
          'channel': channelId,
          'text':
              '$finalMessage ${isShareDownloadLink == true ? "<$downloadLink|Download>" : ""}',
          'blocks': [
            {
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text":
                    "🚀 *$finalMessage ${isShareDownloadLink == true ? "<$downloadLink|Download>" : ""}*"
              }
            },
            if (isShareDownloadLink == true)
              {
                "type": "actions",
                "elements": [
                  {
                    "type": "button",
                    "text": {"type": "plain_text", "text": "Download Now"},
                    "style": "primary",
                    "url": downloadLink.toString()
                  }
                ]
              }
          ]
        },
      ),
    );

    if (messageResponse.data['ok']) {
      Helpers.highlight('Message sent successfully to Slack.');
    } else {
      Helpers.showHighlight(
        firstMessage: 'Error sending message:',
        highLightmessage: messageResponse.data['error'].toString(),
      );
    }
  } catch (e) {
    Helpers.showHighlight(
      firstMessage: 'Error while uploading QR to slack:',
      highLightmessage: e.toString(),
    );
    exit(0);
  }
}