import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:json_annotation/json_annotation.dart'; import 'package:http/http.dart'; // ignore: depend_on_referenced_packages, implementation_imports import 'package:http_parser/src/media_type.dart'; part 'slack_data.g.dart'; enum SlackBlockType { header, section } enum SlackTextType { mrkdwn, plain_text } enum SlackActionType { button, select } enum SlackActionStyle { danger, primary } @JsonSerializable() class DataSlack { String? message; List blocks = []; List attachments = []; String getBlocks() { var list = []; for (var item in blocks) { list.add(item.toJson()); } return jsonEncode(list); } String getAttachments() { var list = []; for (var item in attachments) { list.add(item.toJson()); } return jsonEncode(list); } DataSlack(); factory DataSlack.fromJson(Map json) => _$DataSlackFromJson(json); Map toJson() => _$DataSlackToJson(this); } @JsonSerializable() class SlackBlock { late SlackBlockType type; SlackBlock(); factory SlackBlock.fromJson(Map json) => _$SlackBlockFromJson(json); Map toJson() => _$SlackBlockToJson(this); } @JsonSerializable() class SlackBlockHeader extends SlackBlock { late SlackBlockText text; SlackBlockHeader() { super.type = SlackBlockType.header; text = SlackBlockText(); text.type = SlackTextType.plain_text; } factory SlackBlockHeader.fromJson(Map json) => _$SlackBlockHeaderFromJson(json); @override Map toJson() => _$SlackBlockHeaderToJson(this); } @JsonSerializable() class SlackBlockSection extends SlackBlock { List fields = []; SlackBlockSection() { super.type = SlackBlockType.section; } factory SlackBlockSection.fromJson(Map json) => _$SlackBlockSectionFromJson(json); @override Map toJson() => _$SlackBlockSectionToJson(this); } @JsonSerializable() class SlackBlockSectionField { SlackTextType type = SlackTextType.mrkdwn; String? text; SlackBlockSectionField(); factory SlackBlockSectionField.fromJson(Map json) => _$SlackBlockSectionFieldFromJson(json); Map toJson() => _$SlackBlockSectionFieldToJson(this); } @JsonSerializable() class SlackBlockText { SlackTextType type = SlackTextType.mrkdwn; String? text; SlackBlockText(); factory SlackBlockText.fromJson(Map json) => _$SlackBlockTextFromJson(json); Map toJson() => _$SlackBlockTextToJson(this); } @JsonSerializable() class SlackAttachment { String? fallback; String? color; String? author_name; String? author_link; String? author_icon; String? title; String? title_link; String? pretext; String? text; String? image_url; String? thumb_url; String? footer; String? footer_icon; String? ts; List? fields; List? actions; SlackAttachment(); factory SlackAttachment.fromJson(Map json) => _$SlackAttachmentFromJson(json); Map toJson() => _$SlackAttachmentToJson(this); } @JsonSerializable() class SlackField { String? title; String? value; SlackField(); SlackField.withParam(this.title, this.value); factory SlackField.fromJson(Map json) => _$SlackFieldFromJson(json); Map toJson() => _$SlackFieldToJson(this); } @JsonSerializable() class SlackAction { SlackActionType? type; String? name; String? text; String? url; String? value; SlackActionStyle? style; /*SlackField options; SlackConfirm confirm;*/ SlackAction(); SlackAction.withParam( this.name, this.text, this.url, this.value, this.type, this.style); factory SlackAction.fromJson(Map json) => _$SlackActionFromJson(json); Map toJson() => _$SlackActionToJson(this); } @JsonSerializable() class SlackConfirm { String? title; String? text; // ignore: non_constant_identifier_names String? ok_text; // ignore: non_constant_identifier_names String? dismiss_text; } class SlackFile { String? title; // ignore: non_constant_identifier_names String? initial_comment; String? fileName; String? mimeType; late File file; Future setFileData(MultipartRequest form) async { // var bytes = await readFileByte(filePath!); if (title != null) { form.fields['title'] = title!; } if (initial_comment != null) { form.fields['initial_comment'] = initial_comment!; } form.fields['filetype'] = "auto"; //참조 - https://api.slack.com/types/file#file_types if (fileName != null && mimeType != null) { form.files.add(await MultipartFile.fromPath('file', file.path, contentType: MediaType(HttpHeaders.contentTypeHeader, mimeType!))); } var c = Completer(); c.complete(form); return c.future; } }