private async Task RaiseMessageReceivedEvent(MessageInfoEventArgs messageInfo) { var eventHandler = MessageReceived; if (eventHandler != null) { eventHandler(this, messageInfo); } }
//public async Task Pause() //{ // _state = BotState.Stopped; //} //public async Task Resume() //{ // _state = BotState.Working; // await Start(); //} public async Task Start() { if (_state == BotState.Working) { throw new InvalidOperationException("Бот уже запущен"); } _state = BotState.Working; const string defaultHelpText = "Send any text to generate new GUID."; while (State == BotState.Working) { var updates = await _bot.GetUpdates(_offset); foreach (var update in updates) { var message = update.Message; var messageText = message.Text; var chatId = message.Chat.Id; if (messageText != null) { var replyText = defaultHelpText; if (messageText.ToUpper() != "/HELP" && messageText.ToUpper() != "HELP") { replyText = Guid.NewGuid().ToString(); } await _bot.SendTextMessage(chatId, replyText); } var messageInfo = new MessageInfoEventArgs { ChatId = chatId, Text = messageText, UserName = message.From.Username, }; await RaiseMessageReceivedEvent(messageInfo); _offset = update.Id + 1; } await Task.Delay(1000); } _state = BotState.Stopped; }