Skip to content

Commit ad92812

Browse files
committed
Add receipt generation and file handling methods
Updated CafeController.cs to include System.Text and modified HandleReceipt to generate a receipt using StringBuilder, converting it to a MemoryStream, and returning it as "receipt.txt". Added System.IO to BotControllerBase.cs and introduced a new File method to return a FileResult object.
1 parent 8471367 commit ad92812

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

Sources/TelegramBot.ConsoleTest/Controllers/CafeController.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using TelegramBot.Attributes;
44
using TelegramBot.Controllers;
55
using TelegramBot.Abstractions;
6+
using System.Text;
67

78
namespace TelegramBot.ConsoleTest.Controllers
89
{
@@ -73,7 +74,15 @@ public async Task<IActionResult> HandleBurgersDoneAsync()
7374
[TextCommand("/receipt")]
7475
public IActionResult HandleReceipt()
7576
{
76-
77+
StringBuilder stringBuilder = new();
78+
stringBuilder.AppendLine("Date: 2021-09-01");
79+
stringBuilder.AppendLine("Time: 12:00");
80+
stringBuilder.AppendLine("Order: 2x Vadim's Burger");
81+
stringBuilder.AppendLine("Total: $10.00");
82+
stringBuilder.AppendLine("Payment method: Cash");
83+
stringBuilder.AppendLine("Thank you for your order!");
84+
MemoryStream fileStream = new(Encoding.UTF8.GetBytes(stringBuilder.ToString()));
85+
return File(fileStream, "receipt.txt");
7786
}
7887
}
7988
}

Sources/TelegramBot/Controllers/BotControllerBase.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using Telegram.Bot;
45
using Telegram.Bot.Types;
@@ -111,6 +112,17 @@ public IActionResult File(string filePath)
111112
return new FileResult(filePath);
112113
}
113114

115+
/// <summary>
116+
/// Sends a file to the sender.
117+
/// </summary>
118+
/// <param name="stream">Stream with file content.</param>
119+
/// <param name="fileName">File name.</param>
120+
/// <param name="disposeStream">Dispose the stream after sending the file.</param>
121+
public IActionResult File(Stream stream, string fileName, bool disposeStream = true)
122+
{
123+
return new FileResult(stream, fileName, disposeStream);
124+
}
125+
114126
/// <summary>
115127
/// Deletes message from the current update, or does nothing if the message identifier is not found.
116128
/// <br/>

0 commit comments

Comments
 (0)