Esempio n. 1
0
        protected async Task <string> sendNotificationEnlishVoca(List <int> listIdWord, string messID)
        {
            //ChatfuelJson jsonMessenger = new ChatfuelJson();
            //MessJson messExplaintion = new MessJson();
            //MessJson messPron = new MessJson();
            //MessJson messVietnamese = new MessJson();
            //AttachmentJson sound = new AttachmentJson();
            // jsonMessenger.recipient.id = messID;
            //
            bool isCheck = false;

            foreach (var idWord in listIdWord)
            {
                isCheck = true;
                JsonMessengerText jsonTextMessenger = new JsonMessengerText();
                MessJson          messExplaintion   = new MessJson();
                jsonTextMessenger.recipient.id = messID;
                JsonMessengerText jsonSoundMessenger = new JsonMessengerText();
                jsonSoundMessenger.recipient.id = messID;

                AttachmentJson sound    = new AttachmentJson();
                var            tempWord = _dictCache.GetById(idWord);
                var            infoVoca = _dictCache.findWordCache(tempWord.VocaID);
                messExplaintion.text         = infoVoca.VocaID;
                messExplaintion.text        += "\r\n" + infoVoca.Pron;
                messExplaintion.text        += "\r\n" + infoVoca.MeanEn;
                messExplaintion.text        += "\r\n" + infoVoca.MeanVi;
                sound.attachment.payload.url = infoVoca.SoundUrl;
                //
                jsonTextMessenger.message  = messExplaintion;
                jsonSoundMessenger.message = sound;
                //
                string temp = JsonConvert.SerializeObject(jsonTextMessenger);
                temp = JsonConvert.SerializeObject(jsonSoundMessenger);
                PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
                PostRaw("", JsonConvert.SerializeObject(jsonSoundMessenger));
            }
            if (isCheck)
            {
                FBPostNoti newNoti = new FBPostNoti();
                newNoti.recipient.id = messID;
                newNoti.message.attachment.payload.text = "Luyện tập nào !";
                NotiButton button = new NotiButton();
                button.title   = "Start";
                button.payload = "LUYENTAP";
                newNoti.message.attachment.payload.buttons.Add(button);

                PostRaw("", JsonConvert.SerializeObject(newNoti));
            }

            //HttpClient client = new HttpClient();

            //client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            //if (isCheck)
            //    await client.PostAsync("https://api.chatfuel.com/bots/59a43f64e4b03a25b73c0ebd/users/" + messID + "/" + "send?chatfuel_token=vnbqX6cpvXUXFcOKr5RHJ7psSpHDRzO1hXBY8dkvn50ZkZyWML3YdtoCnKH7FSjC&chatfuel_block_id=5a3fc60de4b037186c58ec99", null);
            return("");
        }
Esempio n. 2
0
        public async Task <string> ToeicQues(string content, string idmessenger)
        {
            ChatfuelJson result       = new ChatfuelJson();
            MessJson     messPron     = new MessJson();
            MessJson     messPron2    = new MessJson();
            var          userMakeQues = _service.listUserID().Where(x => x.Id_Messenger == idmessenger).ToList();

            if (userMakeQues.Count != 0)
            {
                Post newPost = new Model.Post();
                newPost.Content  = content;
                newPost.DatePost = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString();
                newPost.Id_User  = userMakeQues.FirstOrDefault().Id;
                newPost.Id_Type  = 8; // TOIEC
                //Send lên nhóm fb

                {
                    //post to fb toiec
                    //Gửi cho admin trước khi dc duyệt

                    //
                    //var IdPost = await _fbService.PostingToGroupFB(newPost.Content);
                    //  newPost.Id_PostFB = IdPost.id;
                }
                _postService.Add(newPost);
                _postService.Save();
                await NotifyForAdminToApprove(newPost.Id);

                //Send notify
                List <ApplicationUser> userSubQues = getNormalUserBasedOnType(newPost);
                foreach (var itemUser in userSubQues)
                {
                    await sendNofityToMessenger(newPost, itemUser);

                    HaveSendQuestion haveSendQues = new HaveSendQuestion();
                    haveSendQues.QuesID    = newPost.Id;
                    haveSendQues.UserID    = itemUser.Id;
                    haveSendQues.Status    = false;
                    haveSendQues.Protected = false;
                    _haveSendQuesService.Add(haveSendQues);
                    _haveSendQuesService.Save();
                }
                messPron.text = "Câu hỏi của bạn đã được gửi đi cho mọi người !";
                result.messages.Add(messPron);
                // messPron2.text = "https://www.facebook.com/" + newPost.Id_PostFB;
                //result.messages.Add(messPron2);
                return(JsonConvert.SerializeObject(result));
            }
            else
            {
                messPron.text = "Hình như bạn chưa có tài khoản trên Olympus English";
                result.messages.Add(messPron);
                return(JsonConvert.SerializeObject(result));
            }
        }
Esempio n. 3
0
        public static void sendTextMeg(string idMess, string contain)
        {
            MessJson messExplaintionDict = new MessJson();

            messExplaintionDict.text = contain;
            JsonMessengerText jsonTextMessenger = new JsonMessengerText();

            jsonTextMessenger.recipient.id = idMess;
            jsonTextMessenger.message      = messExplaintionDict;
            sendRequest(JsonConvert.SerializeObject(jsonTextMessenger));
        }
Esempio n. 4
0
        public async Task <string> replayDirectly(string idques, string ketqua, string idmessenger)
        {
            //
            ChatfuelJson result   = new ChatfuelJson();
            MessJson     messPron = new MessJson();
            //
            bool checkGio    = false;
            int  idQuestion  = int.Parse(idques);
            var  currentPost = _postService.GetById(idQuestion);
            var  userId      = _service.listUserID().Where(x => x.Id_Messenger == idmessenger).FirstOrDefault().Id;
            //Kiem tra xem con thoi gian tra loi hay ko
            var sendQues = _haveSendQuesService.GetAll().Where(x => x.QuesID == idQuestion && x.UserID == userId).ToList().FirstOrDefault();

            if (sendQues != null)
            {
                int tempMinute  = TimeSetting.LimitMinuteForPost();
                var timePost    = sendQues.CreatedDate.Value.AddMinutes(tempMinute).Ticks;
                var timeCurrent = DateTime.Now.AddMinutes(-5).Ticks;
                if (timePost < timeCurrent)
                {
                    checkGio = true;
                }
                else
                {
                    currentPost.Post_Status = 10;
                    Comment comment = new Comment();
                    comment.Corrected   = false;
                    comment.Content     = ketqua;
                    comment.Id_User     = userId;
                    comment.Id_Post     = idQuestion;
                    comment.DateComment = DateTime.Now.Ticks.ToString();
                    _commentOfPost.Add(comment);
                    _commentOfPost.Save();
                    _postService.Update(currentPost);
                    _postService.Save();
                    messPron.text = "Cám ơn bạn đã phản hồi";
                    result.messages.Add(messPron);
                    await notifyForUserAboutQues(idQuestion);

                    return(JsonConvert.SerializeObject(result));
                }
            }
            //
            //  currentPost.Post_Status = 1;
            // _postService.Update(currentPost);
            //_postService.Save();
            // DateTime
            messPron.text = "Hết hạn trả lời, cảm ơn bạn đã quan tâm";
            result.messages.Add(messPron);
            return(JsonConvert.SerializeObject(result));
        }
Esempio n. 5
0
 public string verifyToken()
 {
     try
     {
         MessJson messPron = new MessJson();
         var      userInfo = _service.GetUserById(User.Identity.GetUserId());
         messPron.text = userInfo.LastName + " " + userInfo.FirstName;
         return(JsonConvert.SerializeObject(messPron));
     }
     catch
     {
         return("");
     }
 }
Esempio n. 6
0
        public async Task <string> ApprovedQuestion(string idques)
        {
            ChatfuelJson result   = new ChatfuelJson();
            MessJson     messPron = new MessJson();

            result.messages.Add(messPron);

            var currentPost = _postService.GetById(int.Parse(idques));
            var IdPost      = await _fbService.PostingToGroupFB(currentPost.Content);

            currentPost.Id_PostFB = IdPost.id;
            _postService.Update(currentPost);
            _postService.Save();
            //Notify for user, this question is approved by admin
            await NotifyForUser(currentPost.Id);

            //
            messPron.text = "Thành công";
            return(JsonConvert.SerializeObject(result));
        }
Esempio n. 7
0
        //make question directly from Messenger
        public async Task <string> IeltsQues(string content, string messengerid)
        {
            ChatfuelJson result   = new ChatfuelJson();
            MessJson     messPron = new MessJson();

            var userMakeQues = _service.listUserID().Where(x => x.Id_Messenger == messengerid).ToList();

            if (userMakeQues.Count != 0)
            {
                Post newPost = new Model.Post();
                newPost.Content  = content;
                newPost.DatePost = DateTime.Now.Ticks.ToString();
                newPost.Id_User  = userMakeQues.FirstOrDefault().Id;
                newPost.Id_Type  = 9; // TOIEC
                _postService.Add(newPost);
                _postService.Save();
                //Send notify
                List <ApplicationUser> userSubQues = getNormalUserBasedOnType(newPost);
                foreach (var itemUser in userSubQues)
                {
                    await sendNofityToMessenger(newPost, itemUser);

                    HaveSendQuestion haveSendQues = new HaveSendQuestion();
                    haveSendQues.QuesID    = newPost.Id;
                    haveSendQues.UserID    = itemUser.Id;
                    haveSendQues.Status    = false;
                    haveSendQues.Protected = false;
                    _haveSendQuesService.Add(haveSendQues);
                    _haveSendQuesService.Save();
                }
                messPron.text = "Câu hỏi của bạn đã được gửi đi cho mọi người";
                result.messages.Add(messPron);
                return(JsonConvert.SerializeObject(result));
            }
            else
            {
                messPron.text = "Hình như bạn chưa có tài khoản trên Olympus English";
                result.messages.Add(messPron);
                return(JsonConvert.SerializeObject(result));
            }
        }
Esempio n. 8
0
        public string busyNotReplay(string idques, string idmessenger)
        {
            ChatfuelJson result   = new ChatfuelJson();
            MessJson     messPron = new MessJson();

            //
            bool isHetGio    = false;
            int  idQuestion  = int.Parse(idques);
            var  currentPost = _postService.GetById(idQuestion);
            var  userId      = _service.listUserID().Where(x => x.Id_Messenger == idmessenger).FirstOrDefault().Id;
            //Kiem tra xem con thoi gian tra loi hay ko
            var sendQues = _haveSendQuesService.GetAll().Where(x => x.QuesID == idQuestion && x.UserID == userId).ToList().FirstOrDefault();

            if (sendQues != null && currentPost.Post_Status != 10)
            {
                var timePost    = sendQues.CreatedDate.Value.AddMinutes(TimeSetting.LimitMinuteForPost()).Ticks;
                var timeCurrent = DateTime.Now.Ticks;
                if (timePost < timeCurrent)
                {
                    isHetGio = true;
                }
            }
            else
            {
                messPron.text = "Câu hỏi không tồn tại hoặc đã trả lời";
                result.messages.Add(messPron);
                return(JsonConvert.SerializeObject(result));
            }
            if (!isHetGio)
            {
                messPron.text           = "Bạn đã bỏ qua câu hỏi số :" + idques;
                currentPost.CreatedDate = DateTime.Now.AddMinutes(-TimeSetting.LimitMinuteForPost());
                _postService.Update(currentPost);
                _postService.Save();
                result.messages.Add(messPron);
                return(JsonConvert.SerializeObject(result));
            }
            messPron.text = "Hết giờ hoặc đã trả lời";
            result.messages.Add(messPron);
            return(JsonConvert.SerializeObject(result));
        }
        public ActionResult ReceivePost(BotRequest data)
        {
            try
            {
                string strResult = data.entry.FirstOrDefault().messaging.FirstOrDefault().message.quick_reply.payload;
                string idMess    = data.entry.FirstOrDefault().messaging.FirstOrDefault().sender.id;
                if (strResult == "True")
                {
                    try
                    {
                        JsonMessengerText jsonTextMessenger = new JsonMessengerText();
                        jsonTextMessenger.recipient.id = idMess;
                        MessJson messExplaintion = new MessJson();
                        messExplaintion.text      = "Chính xác";
                        jsonTextMessenger.message = messExplaintion;
                        PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
                        //isContinue = 2;
                        //Update status
                        UpdateLiteDb(jsonTextMessenger.recipient.id, 2);
                    }
                    catch
                    {
                    }
                }
                else if (strResult == "False")
                {
                    try
                    {
                        JsonMessengerText jsonTextMessenger = new JsonMessengerText();
                        jsonTextMessenger.recipient.id = idMess;
                        MessJson messExplaintion = new MessJson();
                        messExplaintion.text      = "Sai rồi, bạn vui lòng làm lại nha!";
                        jsonTextMessenger.message = messExplaintion;
                        PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
                        // isContinue = 1;
                        UpdateLiteDb(jsonTextMessenger.recipient.id, 1);
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
                //try
                //{
                //    if (data.entry.FirstOrDefault().messaging.FirstOrDefault().message == null)
                //    {
                //        JsonMessengerText jsonTextMessenger = new JsonMessengerText();
                //        jsonTextMessenger.recipient.id = data.entry.FirstOrDefault().messaging.FirstOrDefault().sender.id;
                //        MessJson messExplaintion = new MessJson();
                //        messExplaintion.text = "Có lỗi xay ra, mong bạn thông cảm !";
                //        jsonTextMessenger.message = messExplaintion;
                //        PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
                //        isContinue = 1;
                //    }
                //}
                //catch
                //{

                //}
            }
            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        public async Task <string> multiplechoiceOnline(string id)
        {
            try
            {
                int      numberTrue      = 0;
                int      numberFalse     = 0;
                MessJson messExplaintion = new MessJson();
                messExplaintion.text = "";
                //create
                using (var db = new LiteDatabase(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/OneData.db")))
                {
                    var items = db.GetCollection <Item>("items");
                    var item  = items.FindOne(i => i.Id_Messenger == id);
                    //Create new item
                    if (item == null)
                    {
                        Item newItem = new Item();
                        newItem.Id_Messenger = id;
                        newItem.Status       = 0;
                        items.Insert(newItem);
                    }
                }
                //
                List <int>        randDomPost    = randomPosition();
                List <TracNghiem> dataTracNghiem = new List <TracNghiem>();

                List <int> listIDWord  = new List <int>();
                var        currentUser = _service.listUserID().Where(x => x.Id_Messenger == id.ToString()).FirstOrDefault();
                listIDWord = _detailOutWordService.listIdOutWord(currentUser.Id, -1);
                foreach (var idWord in listIDWord)
                {
                    dataTracNghiem.Add(RandomTracNghiemChoVoca(idWord));
                }

                int  m_countQues = 0;
                bool isBreak     = false;
                //Gửi đi từng question
                while (true)
                {
                    UpdateLiteDb(id, 0);
                    JsonMessengerText jsonMessenger = new JsonMessengerText();

                    jsonMessenger.recipient.id = id;

                    //tao 1 cau trac nghiem gui cho messenger
                    MessageQuick messQuick = new MessageQuick();
                    try
                    {
                        messQuick.text = dataTracNghiem[m_countQues].Question;
                    }
                    catch
                    {
                        isBreak = true;
                        break;
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        QuickReplyMess replay = new QuickReplyMess();
                        replay.payload = dataTracNghiem[m_countQues].ABCD[i].Checked.ToString();
                        replay.title   = ToTitleCase(dataTracNghiem[m_countQues].ABCD[i].Contain);
                        messQuick.quick_replies.Add(replay);
                    }
                    jsonMessenger.message = messQuick;
                    //send di
                    var temp = JsonConvert.SerializeObject(jsonMessenger);
                    PostRaw("", JsonConvert.SerializeObject(jsonMessenger));
                    int countTime  = 0;
                    int isContinue = 0;
                    while (isContinue == 0)
                    {
                        Task.WaitAll(Task.Delay(1000));
                        using (var db = new LiteDatabase(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/OneData.db")))
                        {
                            // Get a collection (or create, if not exits)
                            var col = db.GetCollection <Item>("items");
                            isContinue = col.FindOne(x => x.Id_Messenger == id).Status;
                        }
                        //Dap an sai
                        if (isContinue == 1)
                        {
                            _detailOutWordService.SetNextTime(dataTracNghiem[m_countQues].IdWord, 15, 5);

                            //m_countQues++;
                            numberFalse++;
                            // isContinue = 0;
                            break;
                        }
                        //Dap an dung
                        else if (isContinue == 2)
                        {
                            m_countQues++;
                            numberTrue++;
                            if (m_countQues > dataTracNghiem.Count())
                            {
                                isBreak = true;
                            }
                            break;
                        }
                        //het gio
                        if (countTime > 10)
                        {
                            //Update status
                            UpdateLiteDb(id, 0);
                            //
                            m_countQues = dataTracNghiem.Count;
                            countTime   = 0;
                            break;
                            //  m_countQues--;
                        }
                        countTime++;
                    }
                    if (isBreak)
                    {
                        break;
                    }
                }
                JsonMessengerText jsonTextMessenger = new JsonMessengerText();
                jsonTextMessenger.recipient.id = id;
                messExplaintion.text           = "Hết giờ! Chúc bạn học tốt!!! " + "\r\n" + "Số câu đúng: " + numberTrue.ToString() + "\r\n" + "Số câu sai: " + numberFalse.ToString();
                jsonTextMessenger.message      = messExplaintion;
                PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
                //
                return("");
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
Esempio n. 11
0
        public async Task <string> searchDictViaBot(string contain, string id = "1554844547918927", string userid = null)
        {
            bool multiLetters = false;

            contain = contain.ToString().ToLower().Trim();
            //Tim trong cache nếu ko có thì request crawler
            var dictTemp = _dictCache.findWordCache(contain);
            //post
            //
            bool        isSaveCached     = true;
            DictCache   cacheDict        = new DictCache();
            OxfordDict  dict             = new OxfordDict();
            GoogleTrans googleTransJson  = new GoogleTrans();
            GoogleTrans detailVietnamese = new GoogleTrans();
            //Explain main of letter
            MessJson messExplaintion = new MessJson();
            //Pron
            MessJson messPron = new MessJson();
            //Vietnamese
            MessJson messVietnamese = new MessJson();
            //
            ChatfuelJson   hello = new ChatfuelJson();
            AttachmentJson sound = new AttachmentJson();

            sound.attachment.type = "audio";
            Attachment2 attach = new Attachment2();
            //Json is returned Customer
            AttachmentJson soundDic            = new AttachmentJson();
            MessJson       messExplaintionDict = new MessJson();
            MessJson       resultGoogle        = new MessJson();
            //
            JsonMessengerText jsonTextMessenger = new JsonMessengerText();

            jsonTextMessenger.recipient.id = id;
            JsonMessengerText jsonSoundMessenger = new JsonMessengerText();

            jsonSoundMessenger.recipient.id = id;
            messExplaintionDict.text       += "*" + ToTitleCase(contain) + "*";

            Payload2 payload = new Payload2();

            //
            if (dictTemp != null)
            {
                messPron.text                = dictTemp.Pron;
                messExplaintion.text         = dictTemp.MeanEn;
                messVietnamese.text          = dictTemp.MeanVi;
                sound.attachment.payload.url = dictTemp.SoundUrl;
                //
                messExplaintionDict.text       += "\r\n" + dictTemp.Pron;
                messExplaintionDict.text       += "\r\n" + dictTemp.MeanEn;
                messExplaintionDict.text       += "\r\n" + dictTemp.MeanVi;
                soundDic.attachment.payload.url = dictTemp.SoundUrl;
            }
            else
            {
                int size = contain.Split(' ').Length;
                if (size > 1)
                {
                    multiLetters    = true;
                    googleTransJson = await _clientDictionaryService.startGoogleTrans(contain);

                    foreach (var sentence in googleTransJson.sentences)
                    {
                        messExplaintion.text += sentence.trans;
                        resultGoogle.text    += sentence.trans;
                    }

                    // resultGoogle.text = googleTransJson.sentences[0].trans;
                    //messExplaintionDict.text +="\r\n" + googleTransJson.sentences[0].trans;
                }
                else
                {
                    try
                    {
                        dict = await _clientDictionaryService.startCrawlerOxford(contain);

                        messPron.text        = dict.m_Pron;
                        messExplaintion.text = dict.m_Explanation.First().m_UseCase;
                        //
                        messExplaintionDict.text += "\r\n" + dict.m_Pron;
                        messExplaintionDict.text += "\r\n" + dict.m_Explanation.First().m_UseCase;
                    }
                    catch
                    {
                        isSaveCached              = false;
                        messExplaintion.text      = contain;
                        messExplaintionDict.text += "\r\n" + contain;
                    }
                    try
                    {
                        detailVietnamese = await _clientDictionaryService.startGoogleDetailTrans(contain);
                    }
                    catch
                    {
                        detailVietnamese = null;
                    }
                    try
                    {
                        messVietnamese.text = detailVietnamese.dict.First().pos + ": ";
                        foreach (var item in detailVietnamese.dict.First().terms)
                        {
                            messVietnamese.text += item + ", ";
                        }
                        messExplaintionDict.text += "\r\n" + messVietnamese.text;
                    }
                    catch
                    {
                        isSaveCached              = false;
                        messVietnamese.text       = detailVietnamese.sentences.FirstOrDefault().trans;
                        messExplaintionDict.text += "\r\n" + detailVietnamese.sentences.FirstOrDefault().trans;
                    }
                }
                sound.attachment.payload.url    = dict.m_SoundUrl;
                soundDic.attachment.payload.url = dict.m_SoundUrl;
                //Add database
                cacheDict          = new DictCache();
                cacheDict.VocaID   = contain;
                cacheDict.Pron     = messPron.text;
                cacheDict.MeanEn   = messExplaintion.text;
                cacheDict.MeanVi   = messVietnamese.text;
                cacheDict.SoundUrl = dict.m_SoundUrl;
                _dictCache.Add(cacheDict);
                if (isSaveCached)
                {
                    _dictCache.Save();
                }
                dictTemp = cacheDict;
            }
            //DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userid, dictTemp.Id);
            //if (detailWord == null)
            //{
            //    hello.Selected = false;
            //}
            //else
            //{
            //    hello.Selected = true;
            //}
            hello.messages.Add(messPron);
            hello.messages.Add(messVietnamese);
            hello.messages.Add(messExplaintion);
            hello.messages.Add(sound);
            try
            {
                hello.strVoca = ToTitleCase(contain);
                ApplicationUser currentUser = _service.listUserID().Where(x => x.Id_Messenger == id).FirstOrDefault();
                NotificationHub.sendNoti(currentUser.Email, JsonConvert.SerializeObject(hello));
            }
            catch
            {
            }

            if (multiLetters)
            {
                jsonTextMessenger.message  = messExplaintionDict;
                jsonSoundMessenger.message = resultGoogle;
                PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
                PostRaw("", JsonConvert.SerializeObject(jsonSoundMessenger));
                return("");
            }

            jsonTextMessenger.message  = messExplaintionDict;
            jsonSoundMessenger.message = soundDic;
            PostRaw("", JsonConvert.SerializeObject(jsonTextMessenger));
            PostRaw("", JsonConvert.SerializeObject(jsonSoundMessenger));
            return(JsonConvert.SerializeObject(hello));
        }
Esempio n. 12
0
        public async Task <string> getDictToExtension(string contain)
        {
            bool   isVN   = false;
            string userid = User.Identity.GetUserId();

            contain = contain.ToString().ToLower().Trim();
            DictCache dictTemp = new DictCache();

            //Tim trong cache nếu ko có thì request crawler
            try
            {
                dictTemp = _dictCache.findWordCache(contain);
            }
            catch (Exception e)
            {
            }
            //post
            //
            bool        isSaveCached = true;
            DictCache   cacheDict;
            OxfordDict  dict             = new OxfordDict();
            GoogleTrans googleTransJson  = new GoogleTrans();
            GoogleTrans detailVietnamese = new GoogleTrans();
            //Explain main of letter
            MessJson messExplaintion = new MessJson();
            //Pron
            MessJson messPron = new MessJson();
            //Vietnamese
            MessJson messVietnamese = new MessJson();
            //
            ChatfuelJson   hello = new ChatfuelJson();
            AttachmentJson sound = new AttachmentJson();

            sound.attachment.type = "audio";
            Attachment2 attach = new Attachment2();

            if (contain.LastOrDefault() == '.')
            {
                messPron.text = "";
                hello.messages.Add(messPron);
                return(JsonConvert.SerializeObject(hello));
            }
            Payload2 payload = new Payload2();

            //
            if (dictTemp != null)
            {
                messPron.text                = dictTemp.Pron;
                messExplaintion.text         = dictTemp.MeanEn;
                messVietnamese.text          = dictTemp.MeanVi;
                sound.attachment.payload.url = dictTemp.SoundUrl;
            }
            else
            {
                int size = contain.Split(' ').Length;
                if (size > 1)
                {
                    isSaveCached    = false;
                    googleTransJson = await _clientDictionaryService.startGoogleTrans(contain);

                    hello.strVietnamese = googleTransJson.src;
                    if (googleTransJson.src == "vi")
                    {
                        isVN = true;
                    }
                    messExplaintion.text = googleTransJson.sentences[0].trans;
                }
                else
                {
                    try
                    {
                        dict = await _clientDictionaryService.startCrawlerOxford(contain);

                        messPron.text        = dict.m_Pron;
                        messExplaintion.text = dict.m_Explanation.First().m_UseCase;
                    }
                    catch
                    {
                        isSaveCached         = false;
                        messExplaintion.text = contain;
                    }
                    detailVietnamese = await _clientDictionaryService.startGoogleDetailTrans(contain);

                    hello.strVietnamese = detailVietnamese.src;
                    if (detailVietnamese.src == "vi")
                    {
                        isVN = true;
                    }
                    try
                    {
                        messVietnamese.text = detailVietnamese.dict.First().pos + ": ";
                        foreach (var item in detailVietnamese.dict.First().terms)
                        {
                            messVietnamese.text += item + ", ";
                        }
                    }
                    catch
                    {
                        isSaveCached        = false;
                        messVietnamese.text = detailVietnamese.sentences.FirstOrDefault().trans;
                    }
                }
                sound.attachment.payload.url = dict.m_SoundUrl;
                //Add database
                cacheDict          = new DictCache();
                cacheDict.VocaID   = contain;
                cacheDict.Pron     = messPron.text;
                cacheDict.MeanEn   = messExplaintion.text;
                cacheDict.MeanVi   = messVietnamese.text;
                cacheDict.SoundUrl = dict.m_SoundUrl;
                _dictCache.Add(cacheDict);
                if (isSaveCached)
                {
                    _dictCache.Save();
                }
                dictTemp = cacheDict;
            }
            DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userid, dictTemp.Id);

            if (detailWord == null)
            {
                hello.Selected = false;
            }
            else
            {
                hello.Selected = true;
            }
            hello.messages.Add(messPron);
            hello.messages.Add(messVietnamese);
            hello.messages.Add(messExplaintion);
            hello.messages.Add(sound);
            if (isVN)
            {
                return(null);
            }
            return(JsonConvert.SerializeObject(hello));
        }
Esempio n. 13
0
        public async Task <string> callChatBot(string contain, string id, string userid)
        {
            contain = contain.ToString().ToLower().Trim();
            //Tim trong cache nếu ko có thì request crawler
            var dictTemp = _dictCache.findWordCache(contain);
            //post
            //
            bool        isSaveCached     = true;
            DictCache   cacheDict        = new DictCache();
            OxfordDict  dict             = new OxfordDict();
            GoogleTrans googleTransJson  = new GoogleTrans();
            GoogleTrans detailVietnamese = new GoogleTrans();
            //Explain main of letter
            MessJson messExplaintion = new MessJson();
            //Pron
            MessJson messPron = new MessJson();
            //Vietnamese
            MessJson messVietnamese = new MessJson();
            //
            ChatfuelJson   hello = new ChatfuelJson();
            AttachmentJson sound = new AttachmentJson();

            sound.attachment.type = "audio";
            Attachment2 attach = new Attachment2();

            if (contain.LastOrDefault() == '.')
            {
                messPron.text = "";
                hello.messages.Add(messPron);
                return(JsonConvert.SerializeObject(hello));
            }
            Payload2 payload = new Payload2();

            //
            if (dictTemp != null)
            {
                messPron.text                = dictTemp.Pron;
                messExplaintion.text         = dictTemp.MeanEn;
                messVietnamese.text          = dictTemp.MeanVi;
                sound.attachment.payload.url = dictTemp.SoundUrl;
            }
            else
            {
                int size = contain.Split(' ').Length;
                if (size > 1)
                {
                    googleTransJson = await _clientDictionaryService.startGoogleTrans(contain);

                    messExplaintion.text = googleTransJson.sentences[0].trans;
                }
                else
                {
                    try
                    {
                        dict = await _clientDictionaryService.startCrawlerOxford(contain);

                        messPron.text        = dict.m_Pron;
                        messExplaintion.text = dict.m_Explanation.First().m_UseCase;
                    }
                    catch
                    {
                        isSaveCached         = false;
                        messExplaintion.text = contain;
                    }
                    detailVietnamese = await _clientDictionaryService.startGoogleDetailTrans(contain);

                    try
                    {
                        messVietnamese.text = detailVietnamese.dict.First().pos + ": ";
                        foreach (var item in detailVietnamese.dict.First().terms)
                        {
                            messVietnamese.text += item + ", ";
                        }
                    }
                    catch
                    {
                        isSaveCached        = false;
                        messVietnamese.text = detailVietnamese.sentences.FirstOrDefault().trans;
                    }
                }
                sound.attachment.payload.url = dict.m_SoundUrl;
                //Add database
                cacheDict          = new DictCache();
                cacheDict.VocaID   = contain;
                cacheDict.Pron     = messPron.text;
                cacheDict.MeanEn   = messExplaintion.text;
                cacheDict.MeanVi   = messVietnamese.text;
                cacheDict.SoundUrl = dict.m_SoundUrl;
                _dictCache.Add(cacheDict);
                if (isSaveCached)
                {
                    _dictCache.Save();
                }
                dictTemp = cacheDict;
            }
            DetailOurWord detailWord = _detailOutWordService.findDetailOurWord(userid, dictTemp.Id);

            if (detailWord == null)
            {
                hello.Selected = false;
            }
            else
            {
                hello.Selected = true;
            }
            hello.messages.Add(messPron);
            hello.messages.Add(messVietnamese);
            hello.messages.Add(messExplaintion);
            hello.messages.Add(sound);
            try
            {
                hello.strVoca = ToTitleCase(contain);
                ApplicationUser currentUser = _service.listUserID().Where(x => x.Id_Messenger == id).FirstOrDefault();
                NotificationHub.sendNoti(currentUser.Email, JsonConvert.SerializeObject(hello));
            }
            catch
            {
            }
            return(JsonConvert.SerializeObject(hello));
        }