コード例 #1
0
        public PartialViewResult Comment(CommentViewModel data)
        {
            _viewModel = new CommentViewModel();
            Comment comment = new Comment();

            FieldHelper.CopyNotNullValue(comment, data);
            _commentOfPost.Add(comment);
            _commentOfPost.Save();
            //Determine user of post, send noti via mess bot
            var currentPost   = _postService.GetById(comment.Id_Post);
            var userOfPost    = _service.GetUserById(currentPost.Id_User);
            var userOfComment = _service.GetUserById(comment.Id_User);

            if (userOfComment.Id_Messenger != userOfPost.Id_Messenger)
            {
                ChatBotMessenger.sendTextMeg(userOfPost.Id_Messenger, "🔥 *Câu hỏi:* " + currentPost.Content + "\r\n" + "✎ *Đáp án:* " + comment.Content);
            }
            //
            FieldHelper.CopyNotNullValue(CommentViewModel, comment);
            if (data.Id_Comment == 0)
            {
                return(PartialView("_Comment", CommentViewModel));
            }
            else
            {
                return(PartialView("_ChildComment", CommentViewModel));
            }
        }
コード例 #2
0
        public async Task <PartialViewResult> Comment(CommentViewModel data, bool IsFromBot = false)
        {
            _viewModel = new CommentViewModel();
            Comment comment = new Comment();

            FieldHelper.CopyNotNullValue(comment, data);
            comment.Corrected = false;
            //  comment.CreatedBy =
            comment.DateComment = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString();
            _commentOfPost.Add(comment);
            _commentOfPost.Save();
            Post post = _postService.GetById(comment.Id_Post);

            post.Comment++;
            _postService.Update(post);
            _postService.Save();
            ApplicationUser user = _service.GetUserById(comment.Id_User);

            FieldHelper.CopyNotNullValue(CommentViewModel, user);
            FieldHelper.CopyNotNullValue(CommentViewModel, comment);
            CommentViewModel.Id_UserPost = post.Id_User;
            if (data.Id_Comment == 0)
            {
                if (CommentViewModel.Id_UserPost != User.Identity.GetUserId())
                {
                    Notification notification = new Notification();
                    notification.Id_User     = data.Id_User;
                    notification.Id_Friend   = CommentViewModel.Id_UserPost;
                    notification.Id_Post     = data.Id_Post;
                    notification.Id_Comment  = comment.Id;
                    notification.DateComment = data.DateComment;
                    var userOfPost = _service.GetUserById(post.Id_User);
                    //Send noti to chrome
                    //
                    _notificationService.Add(notification);
                    _notificationService.Save();
                    //Send noti via chatbot
                    ChatBotMessenger.sendTextMeg(userOfPost.Id_Messenger, "🔥 *Câu hỏi:* " + post.Content + "\r\n" + "✎ *Đáp án:* " + comment.Content);
                    //Send noti for chrome
                    ChromeNotiJson postJson = new ChromeNotiJson();
                    postJson.title       = "Bạn có câu trả lời: ";
                    postJson.text        = post.Content + "<br/>" + comment.Content;
                    postJson.urlQuestion = "http://olympusenglish.azurewebsites.net/Post?id=" + post.Id;
                    if (IsFromBot)
                    {
                        ChromeNotification.sendNoti(userOfPost.Email, post.Id, comment.Id);
                    }
                    await NotificationHub.sendNoti(userOfPost.Email, JsonConvert.SerializeObject(postJson));
                }
                return(PartialView("_Comment", CommentViewModel));
            }
            else
            {
                return(PartialView("_ChildComment", CommentViewModel));
            }
        }
コード例 #3
0
        public async System.Threading.Tasks.Task <PartialViewResult> Post(PostViewModel data)
        {
            Post newPost = new Post();

            newPost.Post_Status = 0;
            _viewModel          = new PostViewModel();
            FieldHelper.CopyNotNullValue(newPost, data);
            newPost.CreatedDate = DateTime.Now;
            if (data.Option == 0 && newPost.Id_Type == 8)
            {
                //post to fb toiec
                var IdPost = await _fbService.PostingToGroupFB(newPost.Content);

                newPost.Id_PostFB = IdPost.id;
            }
            _postService.Add(newPost);
            _postService.Save();
            // string url = MaHoaMD5.Encrypt(newPost.Id + "#" + newPost.UpdatedDate);
            //Send notify
            ApplicationUser userPost = _service.GetUserById(newPost.Id_User);

            FieldHelper.CopyNotNullValue(PostViewModel, userPost);
            FieldHelper.CopyNotNullValue(PostViewModel, newPost);
            PostViewModel.TypeToString = _postTypeService.GetById(newPost.Id_Type).Name;
            PostVoteDetail vote = _postVoteDetailService.getVoteByIdUser(newPost.Id_User, newPost.Id);

            if (vote != null)
            {
                PostViewModel.Vote = vote.Vote;
            }
            //Send noti for all user register
            var userIDList = getUserIDListBasedOnType(newPost.Id_Type);

            foreach (var userID in userIDList)
            {
                var sendPostToUser = _service.GetUserById(userID);
                if (sendPostToUser.Id_Messenger != userPost.Id_Messenger && sendPostToUser.Id_Messenger != null && _eventService.IsFreeTime(sendPostToUser.Email))
                {
                    //Create json send............
                    FBPostNoti newNoti = new FBPostNoti();
                    newNoti.recipient.id = sendPostToUser.Id_Messenger;
                    newNoti.message.attachment.payload.text = "```\r\n" + "💬 Bạn có một câu hỏi: " + "\r\n" + '"' + newPost.Content + '"' + "\r\n```";
                    NotiButton button = new NotiButton();
                    button.payload = "REPLAY_" + newPost.Id;
                    newNoti.message.attachment.payload.buttons.Add(button);
                    //
                    ChatBotMessenger.sendRequest(JsonConvert.SerializeObject(newNoti));
                }
            }
            return(PartialView("_Post", PostViewModel));
        }
コード例 #4
0
        public async Task <string> createNewPostViaFB(string messID, string containQues, int typeQues)
        {
            //Determine user
            var userMakeQues = _service.listUserID().Where(x => x.Id_Messenger == messID).ToList();

            if (userMakeQues.Count != 0)
            {
                Post newPost = new Model.Post();
                newPost.Content  = containQues;
                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  = typeQues; // TOIEC
                //Post to fb group
                if (newPost.Id_Type == 8)
                {
                    try
                    {
                        //post to fb toiec
                        var IdPost = await _fbService.PostingToGroupFB(newPost.Content);

                        newPost.Id_PostFB = IdPost.id;
                    }
                    catch (Exception e)
                    {
                    }
                }
                _postService.Add(newPost);
                _postService.Save();
                //Send noti for all user register
                var userIDList = getUserIDListBasedOnType(typeQues);
                foreach (var userID in userIDList)
                {
                    var sendToUser = _service.GetUserById(userID);
                    if (sendToUser.Id_Messenger != messID && sendToUser.Id_Messenger != null && _eventService.IsFreeTime(sendToUser.Email))
                    {
                        //Create json send............
                        FBPostNoti newNoti = new FBPostNoti();
                        newNoti.recipient.id = sendToUser.Id_Messenger;
                        newNoti.message.attachment.payload.text = "```\r\n" + "💬 Bạn có một câu hỏi: " + "\r\n" + '"' + newPost.Content + '"' + "\r\n```";
                        NotiButton button = new NotiButton();
                        button.payload = "REPLAY_" + newPost.Id;
                        newNoti.message.attachment.payload.buttons.Add(button);
                        //
                        ChatBotMessenger.sendRequest(JsonConvert.SerializeObject(newNoti));
                    }
                }
            }
            return("");
        }
コード例 #5
0
        public void registerChatBot(string idUser, string idmessenger)
        {
            var user = _applicationUserService.GetUserById(idUser);

            if (user == null)
            {
                ChatBotMessenger.sendTextMeg(idmessenger, "ID không tồn tại vui lòng kiểm tra lại");
                return;
            }
            user.Id_Messenger = idmessenger;
            _applicationUserService.Update(user);
            _applicationUserService.Save();
            ChatBotMessenger.sendTextMeg(idmessenger, "Chúc mừng bạn đã liên kết thành công");
            return;
        }
コード例 #6
0
        public void initChatBotMenu()
        {
            ChatbotMenu menu = new ChatbotMenu();
            //
            PersistentMenu infoMenu = new PersistentMenu();

            //
            infoMenu.locale          = "default";
            infoMenu.call_to_actions = new List <CallToAction>();
            //\
            CallToAction parentMenu3 = new CallToAction();

            parentMenu3.title           = "🔍 Đặt câu hỏi nhanh ";
            parentMenu3.type            = "nested";
            parentMenu3.call_to_actions = new List <CallToAction2>();
            //Child menu
            foreach (var typeObj in _postTypeService.GetAll())
            {
                CallToAction2 childMenu = new CallToAction2();
                childMenu.payload = "POST_TYPE_" + typeObj.Id;
                childMenu.title   = "🚩." + typeObj.Name;
                childMenu.type    = "postback";
                parentMenu3.call_to_actions.Add(childMenu);
            }
            infoMenu.call_to_actions.Add(parentMenu3);
            //
            CallToAction parentMenu = new CallToAction();

            parentMenu.title   = "🎓 Xác thực token với Olympus";
            parentMenu.type    = "postback";
            parentMenu.payload = "XACTHUC";
            infoMenu.call_to_actions.Add(parentMenu);
            //
            CallToAction parentMenu2 = new CallToAction();

            parentMenu2.title   = "💡 Luyện tập";
            parentMenu2.type    = "postback";
            parentMenu2.payload = "LUYENTAP";
            infoMenu.call_to_actions.Add(parentMenu2);
            //

            menu.persistent_menu.Add(infoMenu);
            ChatBotMessenger.sendRequest(JsonConvert.SerializeObject(menu, Newtonsoft.Json.Formatting.None,
                                                                     new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            }), true);
        }
コード例 #7
0
        public async Task CheckAnswerOnFB()
        {
            //Lay danh sách cac bài post chưa có câu trả lời
            //  var listPost = _postService.GetAll().Where(x => x.Id_PostFB != null && x.Post_Status == 0);
            var listPost = _postService.checkPostOnGroup();

            foreach (var post in listPost)
            {
                var dapAn = await _fbService.DetectedAnswOfPost(post.Id_PostFB);

                if (dapAn != "")
                {
                    post.Post_Status = 10;
                    Comment comment = new Comment();
                    comment.Corrected = false;
                    comment.Content   = dapAn;
                    //Admin
                    comment.Id_User     = post.Id_User;
                    comment.Id_Post     = post.Id;
                    comment.DateComment = DateTime.Now.Ticks.ToString();
                    _commentOfPost.Add(comment);
                    _commentOfPost.Save();
                    _postService.Update(post);
                    _postService.Save();

                    //Send noti via chatbot
                    var userOfPost = _service.GetUserById(post.Id_User);
                    //   var userOfCmt = _service.GetUserById(comment.Id_User);
                    ChatBotMessenger.sendTextMeg(userOfPost.Id_Messenger, "🔥 *Câu hỏi:* " + post.Content + "\r\n" + "✎ *Đáp án:* " + comment.Content);
                    //Send noti for chrome
                    ChromeNotiJson postJson = new ChromeNotiJson();
                    postJson.title       = "Bạn có câu trả lời: ";
                    postJson.text        = post.Content + "<br/>" + comment.Content;
                    postJson.urlQuestion = "http://olympusenglish.azurewebsites.net/Post?id=" + post.Id;
                    //if (IsFromBot)
                    {
                        ChromeNotification.sendNoti(userOfPost.Email, post.Id, comment.Id);
                    }
                    await NotificationHub.sendNoti(userOfPost.Email, JsonConvert.SerializeObject(postJson));
                }
            }
        }
コード例 #8
0
        protected async System.Threading.Tasks.Task <string> sendNofityToMessenger(Post post, ApplicationUser user)
        {
            ChromeNotiJson postJson = new ChromeNotiJson();

            postJson.title       = "Bạn có một câu hỏi: ";
            postJson.text        = post.Content;
            postJson.urlQuestion = "http://olympusenglish.azurewebsites.net/Post?id=" + post.Id;
            NotificationHub.sendNoti(user.Email, JsonConvert.SerializeObject(postJson));
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            var paramChatfuel = "https://api.chatfuel.com/bots/59a43f64e4b03a25b73c0ebd/users/" + user.Id_Messenger + "/" + "send?chatfuel_token=vnbqX6cpvXUXFcOKr5RHJ7psSpHDRzO1hXBY8dkvn50ZkZyWML3YdtoCnKH7FSjC&chatfuel_block_id=5a294932e4b0d0e603f29776";

            paramChatfuel += "&title=Bạn có 1 câu hỏi :" + post.Content;
            //MaHoaMD5.Encrypt()
            paramChatfuel += "&url=http://olympusenglish.azurewebsites.net/Post?id=" + post.Id;
            paramChatfuel += "&idques=" + post.Id;
            paramChatfuel += ChatBotMessenger.getVocaNull();
            var response2 = await client.PostAsync(paramChatfuel, null);

            return("");
        }
コード例 #9
0
        protected async Task <string> sendNotificationEnlish()
        {
            string     paramChatfuel = "";
            HttpClient client        = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            var            responseVoca = notifyMessenger().Result;
            ListUserNofity listNofity   = JsonConvert.DeserializeObject <ListUserNofity>(responseVoca);

            foreach (var userNoti in listNofity.reminduser)
            {
                paramChatfuel = "https://api.chatfuel.com/bots/59a43f64e4b03a25b73c0ebd/users/" + userNoti.IdMess + "/" + "send?chatfuel_token=vnbqX6cpvXUXFcOKr5RHJ7psSpHDRzO1hXBY8dkvn50ZkZyWML3YdtoCnKH7FSjC&chatfuel_block_id=5a28393be4b0d0e6fdab76b3";
                for (int i = 1; i <= 5; i++)
                {
                    paramChatfuel += "&word" + i.ToString() + "=" + ((i <= userNoti.vocainfo.Count) ? userNoti.vocainfo[i - 1].voca : "");
                    paramChatfuel += "&pron" + i.ToString() + "=" + ((i <= userNoti.vocainfo.Count) ? userNoti.vocainfo[i - 1].pron : "");
                    paramChatfuel += "&meanvn" + i.ToString() + "=" + ((i <= userNoti.vocainfo.Count) ? userNoti.vocainfo[i - 1].meanVN : "");
                    paramChatfuel += "&meanen" + i.ToString() + "=" + ((i <= userNoti.vocainfo.Count) ? userNoti.vocainfo[i - 1].usecase : "");
                }
                paramChatfuel += ChatBotMessenger.getNotiNull();
                var response = await client.PostAsync(paramChatfuel, null);
            }
            return("");
        }
コード例 #10
0
        public async Task <ActionResult> ReceivePost(BotRequest data)
        {
            try
            {
                //If fast replay
                if (data.entry[0].messaging[0].message.quick_reply != null)
                {
                    var ctrlr = DependencyResolver.Current.GetService <LearningController>();
                    ctrlr.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlr);
                    ctrlr.ReceivePost(data);
                    //Update again
                    ItemMessenger previousMess = new ItemMessenger();
                    previousMess.Id_Messenger = data.entry[0].messaging[0].sender.id;
                    UpdatePreviousMsg(previousMess);
                    return(null);
                }
            }
            catch
            {
            }
            //
            ItemMessenger previousMsg = getPreviousMsg(data.entry[0].messaging[0].sender.id);

            if (previousMsg != null)
            {
                //Tn trước đó có chứa payload => đang trời cho 1 câu hỏi gì đó
                if (previousMsg.IsPayload == true)
                {
                    //Menu payload xac thuc
                    if (previousMsg.PayLoad == "XACTHUC")
                    {
                        var ctrlrDict = DependencyResolver.Current.GetService <AccountController>();
                        ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                        ctrlrDict.registerChatBot(data.entry[0].messaging[0].message.text, data.entry[0].messaging[0].sender.id);
                    }
                    else if (previousMsg.PayLoad == "LUYENTAP")
                    {
                    }
                    else if (previousMsg.PayLoad.IndexOf(@"POST_TYPE_") != -1)
                    {
                        var postType = int.Parse(previousMsg.PayLoad.Substring(10));
                        ChatBotMessenger.sendTextMeg(data.entry[0].messaging[0].sender.id, "🎉 Câu hỏi bạn đã được gửi đi 🎉 ");
                        var ctrlrDict = DependencyResolver.Current.GetService <PostController>();
                        ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                        await ctrlrDict.createNewPostViaFB(data.entry[0].messaging[0].sender.id, data.entry[0].messaging[0].message.text, postType);
                    }
                    else if (previousMsg.PayLoad.IndexOf(@"REPLAY_") != -1)
                    {
                        ChatBotMessenger.sendTextMeg(data.entry[0].messaging[0].sender.id, "🎉 Thank you 🎉 ");
                        //
                        var postID = int.Parse(previousMsg.PayLoad.Substring(7));
                        //
                        ApplicationUser currentUser = _service.GetUserByMessID(data.entry[0].messaging[0].sender.id);
                        //
                        CommentViewModel dataCmt = new CommentViewModel();
                        dataCmt.Id_User    = currentUser.Id;
                        dataCmt.Id_Comment = 0;
                        dataCmt.Id_Post    = postID;
                        dataCmt.Name       = data.entry[0].messaging[0].message.text;
                        dataCmt.Content    = data.entry[0].messaging[0].message.text;
                        var ctrlrDict = DependencyResolver.Current.GetService <PostController>();
                        ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                        ctrlrDict.Comment(dataCmt, true);
                    }
                    //Update again
                    ItemMessenger previousMess = new ItemMessenger();
                    previousMess.Id_Messenger = data.entry[0].messaging[0].sender.id;
                    UpdatePreviousMsg(previousMess);
                    return(null);
                }
            }
            string imageToText = "";

            try
            {
                if (data.entry[0].messaging[0].postback != null && data.entry[0].messaging[0].message == null)
                {
                    ItemMessenger previousMess = new ItemMessenger();
                    previousMess.PayLoad      = data.entry[0].messaging[0].postback.payload;
                    previousMess.Id_Messenger = data.entry[0].messaging[0].sender.id;
                    previousMess.Previous_Msg = data.entry[0].messaging[0].postback.title;
                    previousMess.Status       = 1;
                    previousMess.IsPayload    = true;
                    UpdatePreviousMsg(previousMess);
                    //xac thu token
                    if (data.entry[0].messaging[0].postback.payload.ToString() == "XACTHUC")
                    {
                        ChatBotMessenger.sendTextMeg(data.entry[0].messaging[0].sender.id, "🔑 Vui lòng nhập mã token sau tin nhắn này 🔑");
                        //var ctrlrDict = DependencyResolver.Current.GetService<AccountController>();
                        //ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                        // await ctrlrDict.registerChatBot(data.entry[0].messaging[0].message.text, data.entry[0].messaging[0].sender.id, "");
                    }
                    else if (data.entry[0].messaging[0].postback.payload.ToString() == "LUYENTAP")
                    {
                        var ctrlrDict = DependencyResolver.Current.GetService <LearningController>();
                        ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                        await ctrlrDict.multiplechoiceOnline(data.entry[0].messaging[0].sender.id);
                    }
                    else if (data.entry[0].messaging[0].postback.payload.ToString().IndexOf(@"POST_TYPE_") != -1)
                    {
                        ChatBotMessenger.sendTextMeg(data.entry[0].messaging[0].sender.id, "❓ Mời bạn nhập câu hỏi ❓");
                    }
                    else if (data.entry[0].messaging[0].postback.payload.ToString().IndexOf(@"REPLAY_") != -1)
                    {
                        ChatBotMessenger.sendTextMeg(data.entry[0].messaging[0].sender.id, "✍️ Mời bạn nhập câu trả lời ✍️");
                    }

                    return(null);
                }

                //Tra tu dien
                if (data.entry[0].messaging[0].message.attachments != null && data.entry[0].messaging[0].postback == null)
                {
                    ItemMessenger previousMess = new ItemMessenger();
                    previousMess.Id_Messenger = data.entry[0].messaging[0].sender.id;
                    UpdatePreviousMsg(previousMess);
                    //
                    imageToText = data.entry[0].messaging[0].message.attachments[0].payload.url;
                    string textResult = await ConvertImageURLToBase64(data.entry[0].messaging[0].message.attachments[0].payload.url);

                    var ctrlrDict = DependencyResolver.Current.GetService <DictionaryController>();
                    ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                    await ctrlrDict.searchDictViaBot(textResult, data.entry[0].messaging[0].sender.id, "");

                    return(null);
                }
                if (data.entry[0].messaging[0].message.text != "" && data.entry[0].messaging[0].message.quick_reply == null && data.entry[0].messaging[0].postback == null)
                {
                    ItemMessenger previousMess = new ItemMessenger();
                    previousMess.Id_Messenger = data.entry[0].messaging[0].sender.id;
                    UpdatePreviousMsg(previousMess);
                    //
                    var ctrlrDict = DependencyResolver.Current.GetService <DictionaryController>();
                    ctrlrDict.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrlrDict);
                    await ctrlrDict.searchDictViaBot(data.entry[0].messaging[0].message.text, data.entry[0].messaging[0].sender.id, "");

                    return(null);
                }
            }
            catch
            {
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }