Skip to content

Commit

Permalink
✨ Added support for sendMediaGroup and editMessageMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal committed Jun 26, 2024
1 parent 770b6b8 commit eae7e55
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
43 changes: 36 additions & 7 deletions example/parse_mode_setter_example.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:parse_mode_setter/parse_mode_setter.dart';
import 'package:televerse/telegram.dart';
import 'package:televerse/televerse.dart';

final bot = Bot(Platform.environment["BOT_TOKEN"]!);
Expand All @@ -12,22 +13,50 @@ void main(List<String> args) {

bot.command('start', (ctx) async {
// Now use HTML text within the methods, don't worry about not passing the parse mode
await ctx.reply(
"Hello <b>World</b>\n\nThis is a <i>great story of the Detective Rajappan</i>. I hope you've heard of Rajappan. "
"Well, if you haven't he's a <tg-spoiler>super detective.</tg-spoiler>",
);
await ctx.reply(htmlText());
});

bot.onInlineQuery((ctx) async {
final results = InlineQueryResultBuilder().article(
"test-id",
"Hello",
(content) => content.text(
"<b>This is a simple</b> bolded text. And this is <tg-spoiler>spoiler</tg-spoiler> hidden text.",
),
(content) => content.text(htmlText()),
);
await ctx.answerInlineQuery(results.build());
});

bot.command('group', (ctx) async {
await ctx.replyWithMediaGroup([
InputMediaPhoto(
media: InputFile.fromUrl(
"https://televerse-space.web.app/example/photo.jpg",
),
caption: htmlText(),
),
InputMediaPhoto(
media: InputFile.fromUrl(
"https://televerse-space.web.app/example/photo.jpg",
),
),
]);
});

bot.start();
}

String htmlText() {
return """<b>bold</b>, <strong>bold</strong>\n
<i>italic</i>, <em>italic</em>\n
<u>underline</u>, <ins>underline</ins>\n
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>\n
<span class="tg-spoiler">spoiler</span>, <tg-spoiler>spoiler</tg-spoiler>\n
<b>bold <i>italic bold <s>italic bold strikethrough <span class="tg-spoiler">italic bold strikethrough spoiler</span></s> <u>underline italic bold</u></i> bold</b>\n
<a href="http://www.example.com/">inline URL</a>\n
<a href="tg://user?id=123456789">inline mention of a user</a>\n
<tg-emoji emoji-id="5368324170671202286">👍</tg-emoji>\n
<code>inline fixed-width code</code>\n
<pre>pre-formatted fixed-width code block</pre>\n
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>\n
<blockquote>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>\n
<blockquote expandable>Expandable block quotation started\nExpandable block quotation continued\nExpandable block quotation continued\nHidden by default part of the block quotation started\nExpandable block quotation continued\nThe last line of the block quotation</blockquote>""";
}
17 changes: 17 additions & 0 deletions lib/src/parse_mode_setter_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class ParseModeSetter implements Transformer {
/// - `APIMethod.sendPoll`
/// - `APIMethod.editMessageText`
/// - `APIMethod.editMessageCaption`
/// - `APIMethod.editMessageMedia`
/// - `APIMethod.sendMediaGroup`
/// - `APIMethod.answerInlineQuery`
///
/// * **[disallowedMethods]:** This is a list of API methods that are explicitly
/// prohibited from having their parse mode set. Defaults to an empty list.
Expand Down Expand Up @@ -114,6 +117,8 @@ class ParseModeSetter implements Transformer {
APIMethod.editMessageCaption,
APIMethod.editMessageCaption,
APIMethod.answerInlineQuery,
APIMethod.editMessageMedia,
APIMethod.sendMediaGroup,
],
this.disallowedMethods = const [],
this.setExplanationParseMode = true,
Expand Down Expand Up @@ -164,6 +169,18 @@ class ParseModeSetter implements Transformer {
}
}
payload['results'] = results;
} else if (method == APIMethod.editMessageMedia) {
if (payload["media"]["caption"] != null) {
payload["media"][kParseMode] = parseMode.value;
}
} else if (method == APIMethod.sendMediaGroup) {
final List<dynamic> media = payload["media"];
for (int i = 0; i < media.length; i++) {
if (media[i]["caption"] != null) {
media[i][kParseMode] = parseMode.value;
}
}
payload["media"] = media;
} else {
payload[kParseMode] = parseMode.value;
}
Expand Down

0 comments on commit eae7e55

Please sign in to comment.