From 54b5a07b97d2eff468f141072ed2c5f128eb711c Mon Sep 17 00:00:00 2001 From: DinoLeung Date: Sat, 30 Mar 2024 20:32:47 +1030 Subject: [PATCH] testing with enum --- example/main.dart | 1 + lib/src/teledart/model/message.dart | 3 ++- lib/src/telegram/model.g.dart | 13 +++++++++++-- lib/src/telegram/models/dice.dart | 23 ++++++++++++++++++++++- lib/src/telegram/telegram.dart | 5 +++-- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/example/main.dart b/example/main.dart index 9207841..9eddded 100644 --- a/example/main.dart +++ b/example/main.dart @@ -31,6 +31,7 @@ Future main() async { // Sick of boilerplates? Reply messages like below, nice and tidy // Short hands also available for answer query methods teledart.onCommand('glory').listen((message) => message.reply('to Ukraine!')); + teledart.onCommand('dice').listen((message) => message.replyDice(emoji: DiceEmoji.slotMachine)); // You can also utilise regular expressions teledart diff --git a/lib/src/teledart/model/message.dart b/lib/src/teledart/model/message.dart index db55a26..ffd6c36 100644 --- a/lib/src/teledart/model/message.dart +++ b/lib/src/teledart/model/message.dart @@ -573,7 +573,8 @@ class TeleDartMessage extends Message { Future replyDice({ int? messageThreadId, bool withQuote = false, - String emoji = Dice.emojiDice, + // String emoji = Dice.emojiDice, + DiceEmoji emoji = DiceEmoji.dice, bool? disableNotification, bool? allowSendingWithoutReply, ReplyMarkup? replyMarkup, diff --git a/lib/src/telegram/model.g.dart b/lib/src/telegram/model.g.dart index d0f8c7f..f30fb65 100644 --- a/lib/src/telegram/model.g.dart +++ b/lib/src/telegram/model.g.dart @@ -859,14 +859,23 @@ Map _$ContactToJson(Contact instance) { Dice _$DiceFromJson(Map json) => Dice( value: json['value'] as int, - emoji: json['emoji'] as String, + emoji: $enumDecode(_$DiceEmojiEnumMap, json['emoji']), ); Map _$DiceToJson(Dice instance) => { 'value': instance.value, - 'emoji': instance.emoji, + 'emoji': _$DiceEmojiEnumMap[instance.emoji]!, }; +const _$DiceEmojiEnumMap = { + DiceEmoji.dice: '🎲', + DiceEmoji.dart: '🎯', + DiceEmoji.bowling: '🎳', + DiceEmoji.basketball: '🏀', + DiceEmoji.football: '⚽', + DiceEmoji.slotMachine: '🎰', +}; + Document _$DocumentFromJson(Map json) => Document( fileId: json['file_id'] as String, fileUniqueId: json['file_unique_id'] as String, diff --git a/lib/src/telegram/models/dice.dart b/lib/src/telegram/models/dice.dart index 5927232..be5216a 100644 --- a/lib/src/telegram/models/dice.dart +++ b/lib/src/telegram/models/dice.dart @@ -21,6 +21,26 @@ part of '../model.dart'; /// This object represents an animated emoji that displays a random value. /// /// https://core.telegram.org/bots/api#dice +@JsonEnum() +enum DiceEmoji { + @JsonValue('🎲') + dice, + @JsonValue('🎯') + dart, + @JsonValue('🎳') + bowling, + @JsonValue('🏀') + basketball, + @JsonValue('⚽') + football, + @JsonValue('🎰') + slotMachine +} + +extension DiceEmojiExtenson on DiceEmoji { + String getEmoji() => _$DiceEmojiEnumMap[this] ?? '🎲'; +} + @JsonSerializable(fieldRename: FieldRename.snake) class Dice { static const emojiDice = '🎲'; @@ -31,7 +51,8 @@ class Dice { static const emojiSlotMachine = '🎰'; int value; - String emoji; + // String emoji; + DiceEmoji emoji; Dice({ required this.value, required this.emoji, diff --git a/lib/src/telegram/telegram.dart b/lib/src/telegram/telegram.dart index a62da83..895e56f 100644 --- a/lib/src/telegram/telegram.dart +++ b/lib/src/telegram/telegram.dart @@ -1051,7 +1051,8 @@ class Telegram { /// On success, the sent [Message] is returned. Future sendDice(dynamic chatId, {int? messageThreadId, - String emoji = Dice.emojiDice, + // String emoji = Dice.emojiDice, + DiceEmoji emoji = DiceEmoji.dice, bool? disableNotification, bool? protectContent, int? replyToMessageId, @@ -1065,7 +1066,7 @@ class Telegram { var body = { 'chat_id': chatId, 'message_thread_id': messageThreadId, - 'emoji': emoji, + 'emoji': emoji.getEmoji(), 'disable_notification': disableNotification, 'protect_content': protectContent, 'reply_to_message_id': replyToMessageId,