private async Task RunUpdate() { int?offset = null; while (AnyObserver) { try { // if the token already canceled before the first request reset token if (_cancellationTokenSource.IsCancellationRequested) { _cancellationTokenSource = new CancellationTokenSource(); } var getUpdate = new GetUpdate { Offset = offset, Timeout = 60, AllowedUpdates = UpdateTypes }; var result = await _telegramBot.GetUpdate(getUpdate, _cancellationTokenSource.Token); if (!result.Any()) { await Task.Delay(1000); continue; } offset = result.Max(x => x.UpdateId) + 1; DistributeUpdates(result); } catch (TaskCanceledException) { // create new token and check observers offset = null; _cancellationTokenSource = new CancellationTokenSource(); } catch (Exception exception) { // unexpected exception report them to the observers and cancel run update OnException(exception); throw; } } }
public Update[] GetUpdates(GetUpdatesParameter parameter) { var method = new GetUpdate(); return(method.Execute(parameter, HttpClient).Result); }
bool botTrigger = false; //обращение к боту public void DIK_Bot_AI_Start() { while (true) { GetUpdate getUpdate = new GetUpdate(); getUpdate.offset = offset; Request_GetUpdate request_GetUpdate = APIMethods.getUpdates(getUpdate); // Console.Write(updateCount + " "); foreach (Update update in request_GetUpdate.result) { if (update.message != null && update.message.text != null) { Console.WriteLine("Сообщение от " + update.message.from.first_name + " текст сообщения: " + update.message.text); offset = update.update_id + 1; if (firstLaunch || update.message.from.is_bot) { continue; } bool replyTo = false; string replyText = GetReplyText(update.message.text, ref replyTo); if (replyText != "") { OutMessage outMessage = new OutMessage(update.message.chat.id.ToString(), replyText); if (replyTo) { outMessage.reply_to_message_id = update.message.message_id; } APIMethods.SendMessage(outMessage); if (ShutdownCommand) { Environment.Exit(-1); } messagesWithoutAnswer = 0; } else { messagesWithoutAnswer++; } } updateCount = 0; if (messagesWithoutAnswer > 3) { botTrigger = false; } } firstLaunch = false; updateCount++; if (updateCount == 500) { Random rnd = new Random(); string[] replyMas = { "ну и хули молчим тут все?? олло!!", "посоны че заскучали?" }; string mestext = replyMas[rnd.Next(0, replyMas.Length - 1)]; mestext = mestext + "\n@Xo66uT @kirkberez @dmd247\nПо такому случаю - шутеечка!\n\n" + Bash(); OutMessage outMessage = new OutMessage("-1001103776974", mestext); APIMethods.SendMessage(outMessage); } if (updateCount == 5) { botTrigger = false; } } }