Skip to content

Commit a14838a

Browse files
committed
Add CafeController to handle burger orders via Telegram bot
The new CafeController class in CafeController.cs handles Telegram bot commands related to ordering burgers. It includes methods to present order options (/burgers), process orders (inline commands), and notify users when their order is ready (/burgersdone). The class uses attributes and classes from the TelegramBot library, sends order details to an admin, and provides feedback to users about their orders.
1 parent 3b56057 commit a14838a

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Telegram.Bot;
2+
using TelegramBot.Builders;
3+
using TelegramBot.Attributes;
4+
using TelegramBot.Controllers;
5+
using TelegramBot.Abstractions;
6+
7+
namespace TelegramBot.ConsoleTest.Controllers
8+
{
9+
public class CafeController(ITelegramBotClient telegramBotClient) : BotControllerBase
10+
{
11+
const long adminId = 123;
12+
13+
[TextCommand("/burgers")]
14+
public IActionResult HandleBurgers()
15+
{
16+
KeyboardBuilder builder = new();
17+
builder
18+
.WithColumns(2)
19+
.AddButton("🍔", "/burgers/1/drink/false")
20+
.AddButton("🍔🍔", "/burgers/2/drink/false")
21+
.AddButton("🍔🥤", "/burgers/1/drink/true")
22+
.AddButton("🍔🍔🥤", "/burgers/2/drink/true");
23+
return Inline("Choose your order:", builder.Build());
24+
}
25+
26+
[InlineCommand("/burgers/{burgers}/drink/{drink}")]
27+
public async Task<IActionResult> HandleBurgersAsync(int burgers, bool drink)
28+
{
29+
const int burgerPrice = 5;
30+
const int drinkPrice = 2;
31+
int cookTime = Random.Shared.Next(18, 35);
32+
int totalPrice = burgers * burgerPrice + (drink ? drinkPrice : 0);
33+
34+
string text = "🍽🍽🍽🍽🍽🍽🍽🍽🍽🍽🍽🍽\n" +
35+
"Vadim's Burgers got your order!\n\n" +
36+
"Your order:\n" +
37+
$"🍔 {burgers}x Vadim's Burger\n" +
38+
(drink ? "🥤 1x Drink\n" : "No drink\n\n") +
39+
$"Total: ${totalPrice}.00\n\n" +
40+
"The best burgers will be ready in " + cookTime + " minutes\n" +
41+
"🍽🍽🍽🍽🍽🍽🍽🍽🍽🍽🍽🍽";
42+
43+
string sender = Update.Message?.From?.Username ?? "Anonymous";
44+
string orderText = $"Order from {sender}:\n" +
45+
$"🍔 {burgers}x Vadim's Burger\n" +
46+
(drink ? "🥤 1x Drink\n" : "No drink\n") +
47+
$"Total: ${totalPrice}.00\n" +
48+
"Send /burgersdone when you are ready to pick up";
49+
50+
await telegramBotClient.SendTextMessageAsync(adminId, orderText);
51+
SetValue("customerId", User.Id);
52+
await telegramBotClient.DeleteMessageAsync(User.Id, Update.CallbackQuery!.Message!.MessageId);
53+
return Text(text);
54+
}
55+
56+
[TextCommand("/burgersdone")]
57+
public async Task<IActionResult> HandleBurgersDoneAsync()
58+
{
59+
const string text = "🍽🍔🥤🍔🥤🍔🍽\n\n" +
60+
"Your order is ready!\n" +
61+
"Please pick up your order at the living room.\n" +
62+
"Enjoy your meal!\n\n" +
63+
"🍽🍔🥤🍔🥤🍔🍽";
64+
long customerId = GetValue<long>("customerId");
65+
if (customerId == 0)
66+
{
67+
return Text("You have no active orders");
68+
}
69+
await telegramBotClient.SendTextMessageAsync(customerId, text);
70+
return Text(text);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)