예제 #1
0
        public void ProcessUpdate(DataBaseManager dbManager, long chatId, Update upd, int offset, out ReplytRequest replytRequest, out DeleteKeyboardRequest editRequest)
        {
            lastUpdateTime   = DateTime.Now;
            lastUpdateOffset = offset;
            editRequest      = null;
            replytRequest    = null;
            switch (upd.Type)
            {
            case UpdateType.CallbackQuery:
                if (currentQuestion == null)
                {
                    questionsIds.Clear();
                    currentQuestion = dbManager.GetQuestionData(beginOfNode, upd.CallbackQuery.From.LanguageCode);
                    replytRequest   = PrepareReplytRequest();
                    break;
                }
                editRequest = new DeleteKeyboardRequest(chatId, upd.CallbackQuery.Message.MessageId);
                try
                {
                    ProcessingCallbackQuery(dbManager, upd);
                    replytRequest = PrepareReplytRequest();
                }
                catch (Exception ex)
                {
                    if (ex.Message == "Invalid option")
                    {
                        replytRequest = null;
                        editRequest   = null;
                    }
                    else
                    {
                        throw ex;
                    }
                }

                break;

            case UpdateType.Message:
                if (currentQuestion == null || upd.Message.Text == "/start")
                {
                    questionsIds.Clear();
                    currentQuestion = dbManager.GetQuestionData(beginOfNode, upd.Message.From.LanguageCode);
                    replytRequest   = PrepareReplytRequest();
                }
                break;

            default:
                break;
            }
        }
예제 #2
0
        private void ProcessingCallbackQuery(DataBaseManager dbManager, Update upd)
        {
            if (upd.CallbackQuery.Data == ReplytRequest.backButton.shortName + currentQuestion.nodeId.ToString())
            {
                if (questionsIds.Count == 0)
                {
                    return;
                }
                else
                {
                    // предыдущий вопрос
                    QuestionData prevQuestion = dbManager.GetQuestionData(questionsIds.Pop(), upd.CallbackQuery.From.LanguageCode);
                    if (prevQuestion == null)
                    {
                        throw new Exception("Prev question DataBase error");
                    }
                    currentQuestion = prevQuestion;
                    return;
                }
            }

            Option foundOption = null;

            foreach (var option in currentQuestion.optionList)
            {
                if (upd.CallbackQuery.Data == option.shortName)
                {
                    foundOption = option;
                    break;
                }
            }
            if (foundOption == null)
            {
                throw new Exception("Invalid option");
            }
            questionsIds.Push(currentQuestion.nodeId);
            QuestionData newQuestion = dbManager.GetQuestionData(foundOption.nextNodeId, upd.CallbackQuery.From.LanguageCode);

            if (newQuestion == null)
            {
                throw new Exception("DataBase error");
            }
            currentQuestion = newQuestion;
        }
        public QuestionData GetQuestionData(int nodeId, string languageCode)
        {
            switch (languageCode)
            {
            case "ru":
                languageCode = "RUS";
                break;

            case "en":
                languageCode = "ENG";
                break;

            default:
                languageCode = "RUS";
                break;
            }
            QuestionData questionData             = new QuestionData(nodeId);
            string       nodeDataSqlCommandString =
                "SELECT `node`.`short_name`, `node_multiling_text`.`text` FROM `node` LEFT JOIN `node_multiling_text` ON `node_multiling_text`.`node_id` = `node`.`id` WHERE `node`.`id` = @nodeId AND `node_multiling_text`.`language` = @languageCode";
            MySqlCommand nodeDataSqlCommand        = new MySqlCommand(nodeDataSqlCommandString, connection);
            string       nodeLinksSqlCommandString =
                "SELECT `node`.`id`, `article_links`.`short_name`, `article_links`.`link_text` " +
                "FROM `node` " +
                "LEFT JOIN `article_links` ON `article_links`.`node_id` = `node`.`id` " +
                "WHERE `node`.`id` = @nodeId";
            MySqlCommand nodeLinksSqlCommand         = new MySqlCommand(nodeLinksSqlCommandString, connection);
            string       nodeOptionsSqlCommandString =
                "SELECT `node`.`id`, `node_option`.`short_name`, `option_multiling_text`.`text`, `node_option`.`next_node_id` " +
                "FROM `node` " +
                "LEFT JOIN `node_option` ON `node_option`.`node_id` = `node`.`id` " +
                "LEFT JOIN `option_multiling_text` ON `option_multiling_text`.`option_id` = `node_option`.`id` " +
                "WHERE `node`.`id` = @nodeId AND `option_multiling_text`.`language` = @languageCode";
            MySqlCommand    nodeOptionsSqlCommand = new MySqlCommand(nodeOptionsSqlCommandString, connection);
            MySqlDataReader dataReader;

            try
            {
                nodeDataSqlCommand.Parameters.AddWithValue("@nodeId", nodeId);
                nodeDataSqlCommand.Parameters.AddWithValue("@languageCode", languageCode);
                OpenConnection();
                dataReader = nodeDataSqlCommand.ExecuteReader();
                if (!dataReader.HasRows)
                {
                    throw new Exception("Node not found.");
                }
                dataReader.Read();
                questionData.shortName = dataReader.GetString("short_name");
                questionData.nodeText  = dataReader.GetTextReader(dataReader.GetOrdinal("text")).ReadToEnd();
                CloseConnection();

                nodeLinksSqlCommand.Parameters.AddWithValue("@nodeId", nodeId);
                nodeLinksSqlCommand.Parameters.AddWithValue("@languageCode", languageCode);
                OpenConnection();
                dataReader = nodeLinksSqlCommand.ExecuteReader();
                while (dataReader.Read())
                {
                    questionData.linkList.Add(dataReader.GetTextReader(dataReader.GetOrdinal("link_text")).ReadToEnd());
                }
                CloseConnection();

                nodeOptionsSqlCommand.Parameters.AddWithValue("@nodeId", nodeId);
                nodeOptionsSqlCommand.Parameters.AddWithValue("@languageCode", languageCode);
                OpenConnection();
                dataReader = nodeOptionsSqlCommand.ExecuteReader();
                while (dataReader.Read())
                {
                    Option option = new Option
                    {
                        shortName  = dataReader.GetString("short_name"),
                        text       = dataReader.GetTextReader(dataReader.GetOrdinal("text")).ReadToEnd(),
                        nextNodeId = dataReader.GetInt32("next_node_id")
                    };
                    questionData.optionList.Add(option);
                }
                CloseConnection();
            }
            catch (Exception ex)
            {
                if (ex.Message == "Node not found.")
                {
                    questionData = null;
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                CloseConnection();
            }

            return(questionData);
        }