Last active
March 27, 2021 17:24
-
-
Save immmdreza/907cea150cd4520f9b80d3c495239768 to your computer and use it in GitHub Desktop.
C# Telegram.Bot ImBusy code to answer only if you send message with more than 10s wait
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Telegram.Bot; | |
using Telegram.Bot.Types; | |
namespace RateLimiter | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
var bot = new TelegramBotClient("1054404274:AAEzYjlN5HlOdNrrCyid34gr-yRJZOzctTk"); | |
bot.OnUpdate += async (sender, e) => await OnUpdate((TelegramBotClient)sender, e.Update); | |
bot.StartReceiving(); | |
Console.ReadLine(); | |
} | |
private static Dictionary<int, DateTime> UsersLastMessage = new Dictionary<int, DateTime>(); | |
private static int ToWaitSeconds = 10; | |
private static async Task OnUpdate(TelegramBotClient client, Update update) | |
{ | |
switch(update) | |
{ | |
case { Message: { Text: {} text } message}: | |
{ | |
var senderId = message.From.Id; | |
if(UsersLastMessage.TryGetValue(senderId, out DateTime lastMessage)) | |
{ | |
if((message.Date - lastMessage).TotalSeconds < ToWaitSeconds) | |
{ | |
await client.SendTextMessageAsync( | |
message.Chat.Id, | |
$"Wait {ToWaitSeconds - (message.Date - lastMessage).TotalSeconds}s"); | |
return; | |
} | |
UsersLastMessage[senderId] = message.Date; | |
} | |
else | |
{ | |
UsersLastMessage.Add(senderId, message.Date); | |
} | |
// Handle here ... | |
await client.SendTextMessageAsync(message.Chat.Id, "Ok."); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment