sendLinkAndQr method

Future<void> sendLinkAndQr({
  1. required String webhookUrl,
  2. required File qrFile,
  3. required String message,
  4. required String downloadLink,
  5. bool? isShareQR,
  6. bool? isShareDownloadLink,
  7. List<String>? mentions,
})

Implementation

Future<void> sendLinkAndQr({
  required String webhookUrl,
  required File qrFile,
  required String message,
  required String downloadLink,
  bool? isShareQR,
  bool? isShareDownloadLink,
  List<String>? mentions,
}) async {
  try {
    // Note: Teams webhooks don't support @mentions directly
    // We'll include user names as plain text
    final formattedMentions = mentions?.isNotEmpty == true
        ? '\n\nMentioning: ${mentions!.join(', ')}'
        : '';
    final finalMessage = '$message$formattedMentions';

    // Create adaptive card message
    final cardBody = [
      {
        'type': 'TextBlock',
        'text': finalMessage,
        'wrap': true,
        'size': 'Medium',
        'weight': 'Bolder',
      },
    ];

    // Add download button if sharing link
    if (isShareDownloadLink == true && downloadLink.isNotEmpty) {
      cardBody.add({
        'type': 'ActionSet',
        'actions': [
          {
            'type': 'Action.OpenUrl',
            'title': 'Download Now',
            'url': downloadLink,
            'style': 'positive',
          }
        ]
      });
    }

    final messageCard = {
      'type': 'message',
      'attachments': [
        {
          'contentType': 'application/vnd.microsoft.card.adaptive',
          'content': {
            'type': 'AdaptiveCard',
            r'$schema': 'http://adaptivecards.io/schemas/adaptive-card.json',
            'version': '1.4',
            'body': cardBody,
          }
        }
      ]
    };

    // Send message first
    final messageResponse = await http.post(
      Uri.parse(webhookUrl),
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode(messageCard),
    );

    if (messageResponse.statusCode == 200) {
      FlutterReleaseXHelpers.showHighlight(
        firstMessage: '✅',
        highLightmessage: 'Message sent successfully to Microsoft Teams.',
      );
    } else {
      FlutterReleaseXHelpers.showHighlight(
        firstMessage: '⚠️ Teams webhook response:',
        highLightmessage:
            '${messageResponse.statusCode} ${messageResponse.body}',
      );
    }

    // Send QR code separately if enabled
    if (isShareQR == true) {
      await _uploadQrCodeToTeams(
        webhookUrl: webhookUrl,
        qrFile: qrFile,
        message: 'QR Code for quick access:',
        downloadLink: downloadLink,
      );
    }
  } catch (e) {
    FlutterReleaseXHelpers.showHighlight(
      firstMessage: 'Error while sending message to Teams:',
      highLightmessage: e.toString(),
    );
  }
}