Created
July 20, 2025 03:19
-
-
Save Mirsalim3635/dfa638bb5c885208c4f77f1e24054410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "Bot":{ | |
| "Token":"" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Extensions.Hosting; | |
| using Microsoft.Extensions.Logging; | |
| using Telegram.Bot; | |
| using Telegram.Bot.Polling; | |
| using Telegram.Bot.Types.Enums; | |
| namespace bot1; | |
| public class BotHostedService(ILogger<BotHostedService> logger, | |
| ITelegramBotClient botClient, | |
| IUpdateHandler updateHandler) : IHostedService | |
| { | |
| public async Task StartAsync(CancellationToken cancellationToken) | |
| { | |
| var me = await botClient.GetMe(cancellationToken); | |
| logger.LogInformation("{bot} has started successfully.", me.FirstName ?? me.Username); | |
| botClient.StartReceiving( | |
| updateHandler, new ReceiverOptions | |
| { | |
| DropPendingUpdates = true, | |
| AllowedUpdates = [UpdateType.Message] | |
| }, | |
| cancellationToken: cancellationToken); | |
| } | |
| public Task StopAsync(CancellationToken cancellationToken) | |
| { | |
| logger.LogInformation("{service} id exiting ...", nameof(BotHostedService)); | |
| return Task.CompletedTask; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Extensions.Logging; | |
| using Telegram.Bot; | |
| using Telegram.Bot.Polling; | |
| using Telegram.Bot.Types; | |
| namespace bot1; | |
| public class BotUpdateHandler(ILogger<BotUpdateHandler> logger) : IUpdateHandler | |
| { | |
| public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken) | |
| { | |
| logger.LogError(exception, "something happend wrong {message}", exception.Message); | |
| return Task.CompletedTask; | |
| } | |
| public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) | |
| { | |
| logger.LogInformation("new message from {client}", update.Message?.Text); | |
| if (update.Type is Telegram.Bot.Types.Enums.UpdateType.Message) | |
| { | |
| logger.LogInformation("new message from {client} with text: {text}", update.Message?.Chat.Username, update.Message?.Text); | |
| if (update.Message!.Text!.Trim().ToLower() == "/start") | |
| { | |
| await botClient.SendMessage(chatId: update.Message.Chat.Id, | |
| text: """ | |
| *📜 Qo'llab-quvvatlanadigan buyruqlar:* | |
| - `/fun-emoji seed` | |
| - `/avataaars seed` | |
| - `/bottts seed` | |
| - `/pixel-art seed` | |
| """, | |
| parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown, | |
| cancellationToken: cancellationToken | |
| ); | |
| } | |
| else | |
| { | |
| List<string> patterns = ["/fun-emoji", "/avataaars", "/bottts", "/pixel-art"]; | |
| var lst = update.Message.Text?.Split(" ", StringSplitOptions.RemoveEmptyEntries); | |
| if (lst is null || lst.Length < 1) | |
| { | |
| return; | |
| } | |
| string emoji = lst[0].Trim().ToLower(); | |
| string? seed = lst.Length > 1 ? lst[1] : null; | |
| if (patterns.Contains(emoji) && seed is not null) | |
| { | |
| var url = $"https://api.dicebear.com/8.x{emoji}/png?seed={seed}"; | |
| await botClient.SendPhoto( | |
| chatId: update.Message.Chat.Id, | |
| photo: url, | |
| cancellationToken: cancellationToken); | |
| } | |
| else if (!patterns.Contains(emoji) && seed is not null) | |
| { | |
| await botClient.SendMessage( | |
| chatId: update.Message.Chat.Id, | |
| text: "❌ Noma'lum buyruq. Quyidagilardan birini ishlating: \n \n `/fun-emoji`,\n` /bottts`,\n` /avataaars`,\n` /pixel-art`", | |
| parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown, | |
| cancellationToken: cancellationToken); | |
| } | |
| else if (patterns.Contains(emoji) && seed is null) | |
| { | |
| await botClient.SendMessage( | |
| chatId: update.Message.Chat.Id, | |
| text: $"❌ Iltimos, buyruqdan keyin (seed) kiriting. \n Misol: {emoji} Ali✔️", | |
| parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown, | |
| cancellationToken: cancellationToken); | |
| } | |
| else | |
| { | |
| await botClient.SendMessage( | |
| chatId: update.Message.Chat.Id, | |
| text: "❌ Iltimos, avatar olish uchun to'g'ri buyruqdan foydalaning.", | |
| parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown, | |
| cancellationToken: cancellationToken); | |
| } | |
| } | |
| } | |
| await Task.CompletedTask; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "profiles": { | |
| "Local": { | |
| "commandName": "Project", | |
| "dotnetRunMessages": true, | |
| "environmentVariables": { | |
| "DOTNET_ENVIRONMENT": "Development" | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Hosting; | |
| using Telegram.Bot; | |
| using Telegram.Bot.Polling; | |
| using bot1; | |
| var builder = Host.CreateApplicationBuilder(); | |
| builder.Services.AddSingleton<IUpdateHandler, BotUpdateHandler>(); | |
| builder.Services.AddSingleton<ITelegramBotClient, TelegramBotClient>(x => | |
| new TelegramBotClient(builder.Configuration["Bot:Token"] | |
| ?? throw new ArgumentException("Telegram Bot Token is not configured."))); | |
| builder.Services.AddHostedService<BotHostedService>(); | |
| var app = builder.Build(); | |
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "Bot":{ | |
| "Token":"7559344242:AAFzC_rUuDeykKqE4lfx3T_uN9-2v5z4iKU" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment