コード例 #1
0
        public async Task ProcessMessageAsync(Update update)
        {
            string chatId = null;

            try
            {
                chatId = update.Message.Chat.Id.ToString();

                string text = _textParser.ParseArgs(update)?.Trim();

                if (string.IsNullOrWhiteSpace(text))
                {
                    await ProvideHelpAsync(update);
                }
                else
                {
                    string fromName = $"{update.Message.From.FirstName} {update.Message.From.LastName}".Trim();

                    await _spreadsheetDriver.Note(chatId,
                                                  new Message(text, update.Message.Date.ToString("dd-MMM-yyyy"), fromName));


                    _logger.Debug("sending success message back to chat {chatId}", chatId);

                    await _telegramBotClient.SendTextMessageAsync(update.Message.Chat.Id, "Noted");

                    _logger.Debug("success message successfully sent back to chat {chat}", chatId);
                }
            }

            catch (TokenResponseException ex)
            {
                _logger.Error(ex, "Error in chat {chatId}", chatId);

                //permissions may have been revoked, clear access codes and access tokens
                await _accessCodeStore.DeleteCodeAsync(chatId);

                await _accessTokenStore.DeleteAsync <TokenResponse>(chatId);

                //cleaned codes

                await _telegramBotClient.SendTextMessageAsync(update.Message.Chat.Id, "I am having difficulties accessing the spreadsheet. Try authorizing again and make sure you have permissions to make changes to the spreadsheet");

                await _authorize.ProcessMessageAsync(update);
            }
            catch (GoogleApiException ex) when(ex.Error.Code == 401)
            {
                _logger.Error(ex, "Error in chat {chatId}", chatId);

                //permissions may have been revoked, clear access codes and access tokens
                await _accessCodeStore.DeleteCodeAsync(chatId);

                await _accessTokenStore.DeleteAsync <TokenResponse>(chatId);

                //cleaned codes

                await _telegramBotClient.SendTextMessageAsync(update.Message.Chat.Id, "I am having difficulties accessing the spreadsheet. Try authorizing again and make sure you have permissions to make changes to the spreadsheet");

                await _authorize.ProcessMessageAsync(update);
            }
            catch (SpreadSheetNotSetException)
            {
                await _telegramBotClient.SendTextMessageAsync(update.Message.Chat.Id, "You have not yet set the spreadsheet. Check /spreadsheet for more info");
            }
            catch (UnauthorizedChatException)
            {
                await _telegramBotClient.SendTextMessageAsync(update.Message.Chat.Id, "I am not yet authorized to access your spreadsheets");

                await _authorize.ProcessMessageAsync(update);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error in chat {chatId}", chatId);
                await _telegramBotClient.SendTextMessageAsync(update.Message.Chat.Id, "Something went wrong");
            }
        }