sendTemplate method

Future<Request> sendTemplate(
  1. String phoneNumber,
  2. String template,
  3. String language,
  4. List<Map<String, dynamic>>? placeholder,
)

Implementation

Future<Request> sendTemplate(String phoneNumber, String template,
    String language, List<Map<String, dynamic>>? placeholder) async {
  final Map<String, String> headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer $accessToken',
  };

  final Map<String, dynamic> body = {
    'messaging_product': 'whatsapp',
    'to': phoneNumber,
    'type': 'template',
    'template': {
      'name': template,
      'language': {'code': language},
      'components': []
    },
  };

  if (placeholder != null && placeholder.isNotEmpty) {
    body['template']['components'] = [
      {
        'type': 'body',
        'parameters': placeholder,
      }
    ];
  }

  await request.post('$fromNumberId/messages', headers, body);
  return request;
}