Skip to content

Commit 6dd9168

Browse files
author
Vadim Belov
committed
Add support for sending files by Telegram file ID
FileResult now supports file IDs in addition to streams, with a new constructor and logic in ExecuteResultAsync. Added FileId method to BotControllerBase for convenience. Updated Telegram.Bot package to 22.9.6.
1 parent 2f41e14 commit 6dd9168

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

Sources/TelegramBot/ActionResults/FileResult.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class FileResult : IActionResult, IDisposable
1515
private bool _disposed;
1616
private readonly string _fileName;
1717
private readonly Stream _fileStream;
18+
private readonly InputFileId? _inputFile;
1819
private readonly bool _disposeStream = true;
1920

2021
/// <summary>
@@ -40,6 +41,19 @@ public FileResult(Stream stream, string fileName, bool disposeStream = true)
4041
_disposeStream = disposeStream;
4142
}
4243

44+
/// <summary>
45+
/// Initializes a new instance of the FileResult class using the specified input file identifier.
46+
/// </summary>
47+
/// <remarks>The file name is initialized to an empty string, and the file stream is set to a null
48+
/// stream.</remarks>
49+
/// <param name="inputFileId">The identifier of the input file to be processed. This parameter cannot be null.</param>
50+
public FileResult(InputFileId inputFileId)
51+
{
52+
_inputFile = inputFileId;
53+
_fileName = string.Empty;
54+
_fileStream = Stream.Null;
55+
}
56+
4357
/// <summary>
4458
/// Disposes the file stream if it is applicable.
4559
/// </summary>
@@ -59,7 +73,15 @@ public void Dispose()
5973
/// <returns>The task representing the result of the action.</returns>
6074
public Task ExecuteResultAsync(ActionContext context)
6175
{
62-
InputFile file = InputFile.FromStream(_fileStream, _fileName);
76+
InputFile file;
77+
if (_inputFile != null)
78+
{
79+
file = _inputFile;
80+
}
81+
else
82+
{
83+
file = InputFile.FromStream(_fileStream, _fileName);
84+
}
6385
return context.Bot.SendDocument(context.ChatId, document: file);
6486
}
6587
}

Sources/TelegramBot/Controllers/BotControllerBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public IActionResult File(Stream stream, string fileName, bool disposeStream = t
175175
return new FileResult(stream, fileName, disposeStream);
176176
}
177177

178+
public IActionResult FileId(string fileId)
179+
{
180+
if (string.IsNullOrWhiteSpace(fileId))
181+
{
182+
throw new ArgumentNullException(nameof(fileId));
183+
}
184+
return new FileResult(InputFile.FromFileId(fileId));
185+
}
186+
178187
/// <summary>
179188
/// Sends an image to the sender.
180189
/// </summary>

Sources/TelegramBot/TelegramBot.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
2929
<None Include="icon.png" Pack="true" PackagePath="\" />
3030
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
31-
<PackageReference Include="Telegram.Bot" Version="22.9.5.3" />
31+
<PackageReference Include="Telegram.Bot" Version="22.9.6" />
3232
<PackageReference Include="System.Text.Json" Version="10.0.5" />
3333
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.5" />
3434
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.5" />

0 commit comments

Comments
 (0)