diff --git a/lib/src/telegram/telegram.dart b/lib/src/telegram/telegram.dart index 3e7e9057..648be5f4 100644 --- a/lib/src/telegram/telegram.dart +++ b/lib/src/telegram/telegram.dart @@ -844,7 +844,7 @@ class Telegram { /// https://core.telegram.org/bots/api#editmessagelivelocation /// /// [inline bots]: https://core.telegram.org/bots/api#inline-mode - Future editMessageLiveLocation(double latitude, double longitude, + Future editMessageLiveLocation(double latitude, double longitude, {dynamic chatId, int? messageId, String? inlineMessageId, @@ -872,7 +872,10 @@ class Telegram { 'proximity_alert_radius': proximityAlertRadius, 'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup), }; - return Message.fromJson(await HttpClient.httpPost(requestUrl, body: body)); + + final res = await HttpClient.httpPost(requestUrl, body: body); + + return res is bool ? res : Message.fromJson(res); } /// Use this method to stop updating a live location message sent by the bot or via the bot @@ -884,7 +887,7 @@ class Telegram { /// https://core.telegram.org/bots/api#stopmessagelivelocation /// /// [inline bots]: https://core.telegram.org/bots/api#inline-mode - Future stopMessageLiveLocation( + Future stopMessageLiveLocation( {dynamic chatId, int? messageId, String? inlineMessageId, @@ -904,7 +907,10 @@ class Telegram { 'inline_message_id': inlineMessageId, 'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup), }; - return Message.fromJson(await HttpClient.httpPost(requestUrl, body: body)); + + final res = await HttpClient.httpPost(requestUrl, body: body); + + return res is bool ? res : Message.fromJson(res); } /// Use this method to send information about a venue @@ -2260,7 +2266,7 @@ class Telegram { /// https://core.telegram.org/bots/api#editmessagetext /// /// [inline bots]: https://core.telegram.org/bots/api#inline-mode - Future editMessageText(String text, + Future editMessageText(String text, {dynamic chatId, int? messageId, String? inlineMessageId, @@ -2286,12 +2292,7 @@ class Telegram { 'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup), }; var res = await HttpClient.httpPost(requestUrl, body: body); - if (res == true) { - return Future.error( - TelegramException('Edited message is NOT sent by the bot')); - } else { - return Message.fromJson(res); - } + return res == true ? res : Message.fromJson(res); } /// Use this method to edit captions of messages sent by the bot or via the bot @@ -2303,7 +2304,7 @@ class Telegram { /// https://core.telegram.org/bots/api#editmessagecaption /// /// [inline bots]: https://core.telegram.org/bots/api#inline-mode - Future editMessageCaption( + Future editMessageCaption( {dynamic chatId, int? messageId, String? inlineMessageId, @@ -2328,12 +2329,7 @@ class Telegram { 'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup), }; var res = await HttpClient.httpPost(requestUrl, body: body); - if (res == true) { - return Future.error( - TelegramException('Edited message is NOT sent by the bot')); - } else { - return Message.fromJson(res); - } + return res == true ? res : Message.fromJson(res); } /// Use this method to edit audio, document, photo, or video messages @@ -2347,7 +2343,7 @@ class Telegram { /// otherwise *True* is returned. /// /// https://core.telegram.org/bots/api#editMessageMedia - Future editMessageMedia( + Future editMessageMedia( {dynamic chatId, int? messageId, String? inlineMessageId, @@ -2384,12 +2380,7 @@ class Telegram { : await HttpClient.httpMultipartPost(requestUrl, multiPartFiles, body: body); - if (res == true) { - return Future.error( - TelegramException('Edited message is NOT sent by the bot')); - } else { - return Message.fromJson(res); - } + return res == true ? res : Message.fromJson(res); } /// Use this method to edit only the reply markup of messages sent by the bot or via the bot @@ -2401,7 +2392,7 @@ class Telegram { /// https://core.telegram.org/bots/api#editmessagereplymarkup /// /// [inline bots]: https://core.telegram.org/bots/api#inline-mode - Future editMessageReplyMarkup( + Future editMessageReplyMarkup( {dynamic chatId, int? messageId, String? inlineMessageId, @@ -2422,12 +2413,8 @@ class Telegram { 'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup), }; var res = await HttpClient.httpPost(requestUrl, body: body); - if (res == true) { - return Future.error( - TelegramException('Edited message is NOT sent by the bot')); - } else { - return Message.fromJson(res); - } + + return res == true ? res : Message.fromJson(res); } /// Use this method to stop a poll which was sent by the bot @@ -2436,7 +2423,10 @@ class Telegram { /// /// https://core.telegram.org/bots/api#stoppoll Future stopPoll( - dynamic chatId, int messageId, InlineKeyboardMarkup replyMarkup) async { + dynamic chatId, + int messageId, { + InlineKeyboardMarkup? replyMarkup, + }) async { if (chatId is! String && chatId is! int) { return Future.error(TelegramException( 'Attribute \'chatId\' can only be either type of String or int')); @@ -2445,7 +2435,7 @@ class Telegram { var body = { 'chat_id': chatId, 'message_id': messageId, - 'reply_markup': replyMarkup, + 'reply_markup': replyMarkup != null ? jsonEncode(replyMarkup) : null, }; return Poll.fromJson(await HttpClient.httpPost(requestUrl, body: body)); } @@ -2817,7 +2807,7 @@ class Telegram { /// On success, a [SentWebAppMessage] object is returned. /// /// https://core.telegram.org/bots/api#answerwebappquery - /// + /// /// [Web App]: (https://core.telegram.org/bots/webapps) Future answerWebAppQuery( String webAppQueryId, InlineQueryResult result) async { diff --git a/lib/teledart.dart b/lib/teledart.dart index f8633989..f1aaf3c0 100644 --- a/lib/teledart.dart +++ b/lib/teledart.dart @@ -31,3 +31,4 @@ export 'src/teledart/event/event.dart'; export 'src/teledart/fetch/abstract_update_fetcher.dart'; export 'src/teledart/fetch/long_polling.dart'; export 'src/teledart/fetch/webhook.dart'; +export 'src/util/http_client.dart' show HttpClientException; diff --git a/pubspec.yaml b/pubspec.yaml index d1cdfb1b..ad4c8992 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: teledart description: A Dart library interfacing with the latest Telegram Bot API. -version: 0.6.1 +version: 0.6.2 homepage: https://github.com/DinoLeung/TeleDart issue_tracker: https://github.com/DinoLeung/TeleDart/issues diff --git a/test/TeleDart_test.dart b/test/TeleDart_test.dart deleted file mode 100644 index 71a9f599..00000000 --- a/test/TeleDart_test.dart +++ /dev/null @@ -1,12 +0,0 @@ -// import 'package:teledart/teledart.dart'; -import 'package:test/test.dart'; - -void main() { - group('A group of tests', () { - setUp(() {}); - - test('First Test', () { - // TODO: TEST ME PLEASE - }); - }); -} diff --git a/test/teledart_test.dart b/test/teledart_test.dart new file mode 100644 index 00000000..8e1904cf --- /dev/null +++ b/test/teledart_test.dart @@ -0,0 +1,22 @@ +import 'dart:io'; + +import 'package:teledart/teledart.dart'; +import 'package:test/test.dart'; + +void main() { + TeleDart teleDart = TeleDart( + Platform.environment["BOT_TOKEN"]!, + Event(Platform.environment["BOT_USERNAME"] ?? ""), + ); + group('Senders', () { + setUp(() {}); + + test('sendMessage', () async { + final msg = await teleDart.sendMessage( + Platform.environment["CHAT_ID"]!, + "Hello World!", + ); + expect(msg.text, "Hello World!"); + }); + }); +}