Пример #1
0
        public async Task <IActionResult> TestConnection(LineRequestModel request)
        {
            LineResponseModel response = new LineResponseModel();

            LineMessageTemplate.FlexMessageMain flex = new LineMessageTemplate.FlexMessageMain();
            MessageResponseModel message             = new MessageResponseModel();

            LineMessageTemplate.RichMenuMain menu = new LineMessageTemplate.RichMenuMain();
            Task <UserProfile> profile;
            Task <LineMessageTemplate.RichMenuResponse> result;
            DataTable checkHelperCase = new DataTable();
            string    msg             = "";
            string    typeofMenu      = "";

            try
            {
                message = api.SetMessage("กรุณากรอก Secret Code ที่ได้รับ เพื่อเริ่มต้นใช้งาน");
                for (int i = 0; i < request.events.Count; i++)
                {
                    response.replyToken = request.events[i].replyToken;
                    response.messages.Add(message);
                    // await api.CallApi(response);
                    try
                    {
                        using (HttpClient client = new HttpClient())
                        {
                            action.SP_InsertLogRequestMessage(
                                "Call API",
                                "",
                                "request.events[0].mode",
                                "",
                                "Before Call API",
                                DateTime.Now,
                                "request.events[0].source.userId",
                                "request.events[0].source.userId"
                                );
                            StringContent content = new StringContent(func.JsonSerialize(response),
                                                                      System.Text.Encoding.UTF8,
                                                                      "application/json");
                            client.DefaultRequestHeaders.Authorization
                                = new AuthenticationHeaderValue("Bearer", "q281ubFyT1L3Z1gAyrcLdLY4mHv2hXJFqAb/MEUO2OncgbgXdSsR6BDCXsrTZh0I3haZwDDaz1lrKF694gC0fTnp/CnbLma8WkiHW3UXwSf6gHxU5lNJP/IYeb1+KQRFeun9E5jJT8qx9lpQpY1S9AdB04t89/1O/w1cDnyilFU=");
                            var sresult = await client.PostAsync("https://api.line.me/v2/bot/message/reply", content);

                            var contents = await sresult.Content.ReadAsStringAsync();

                            action.SP_InsertLogRequestMessage(
                                "Call API",
                                "",
                                "request.events[0].mode",
                                "",
                                contents[0].ToString(),
                                DateTime.Now,
                                "request.events[0].source.userId",
                                "request.events[0].source.userId"
                                );
                        }
                    }
                    catch (Exception e)
                    {
                        action.SP_InsertLogRequestMessage(
                            "request.events[0].type",
                            "",
                            "Error",
                            "",
                            e.Message,
                            DateTime.Now,
                            "request.events[0].source.userId",
                            "request.events[0].source.userId"
                            );

                        return(Ok());
                    }
                }
                return(Ok());
            }
            catch (Exception e)
            {
                action.SP_InsertLogRequestMessage(
                    "request.events[0].type",
                    "",
                    "request.events[0].mode",
                    "",
                    e.Message,
                    DateTime.Now,
                    "request.events[0].source.userId",
                    "request.events[0].source.userId"
                    );

                return(Ok());
            }
        }
Пример #2
0
        public async Task <IActionResult> WebHook(LineRequestModel request)
        {
            LineResponseModel response = new LineResponseModel();

            LineMessageTemplate.FlexMessageMain flex = new LineMessageTemplate.FlexMessageMain();
            MessageResponseModel message             = new MessageResponseModel();

            LineMessageTemplate.RichMenuMain menu = new LineMessageTemplate.RichMenuMain();
            Task <UserProfile> profile;
            Task <LineMessageTemplate.RichMenuResponse> result;
            DataTable checkHelperCase = new DataTable();
            string    msg             = "";
            string    typeofMenu      = "";

            profile         = api.GetUserProfile(request.events[0].source.userId);
            checkHelperCase = DAC.REST_CheckHelperCase(request.events[0].source.userId);
            // KeepLog
            action.SP_InsertLogRequestMessage(
                request.events[0].type,
                request.destination,
                request.events[0].mode,
                request.events[0].type == "message" ? request.events[0].message.text : "",
                func.JsonSerialize(request),
                DateTime.Now,
                request.events[0].source.userId,
                request.events[0].source.userId
                );

            if (request.events[0].type == "follow")
            {
                if (profile.Result != null)
                {
                    action.SP_InsertUserFollow(
                        request.events[0].source.userId,
                        profile.Result.displayName == null ? "" : profile.Result.displayName,
                        profile.Result.pictureUrl == null ? "" : profile.Result.pictureUrl,
                        profile.Result.statusMessage == null ? "" : profile.Result.statusMessage,
                        profile.Result.language == null ? "" : profile.Result.language
                        );
                }
                msg                 = string.Format(template.FollowMessage(), profile.Result.displayName);
                message             = api.SetMessage(msg);
                response.replyToken = request.events[0].replyToken;
                response.messages.Add(message);
                await api.CallApi(response);
            }
            else if (request.events[0].message.text == "เช็คสถานะ")
            {
                dt      = new DataTable();
                dt      = DAC.REST_CheckStatustoFlexMessage(request.events[0].source.userId);
                flex    = template.SetupFlexMessage(dt);
                flex.to = request.events[0].source.userId;
                await api.CallApi(flex);

                return(Ok());
            }
            else if (request.events[0].message.text == "ช่วยเหลือ")
            {
                message             = api.SetMessage("ต้องการให้เราช่วยอะไรครับ");
                response.replyToken = request.events[0].replyToken;
                response.messages.Add(message);
                await api.CallApi(response);

                action.SP_InsertLogRequestMessage(
                    "reply",
                    request.events[0].source.userId,
                    request.events[0].mode,
                    request.events[0].message.text,
                    "",
                    DateTime.Now,
                    request.destination,
                    "SYSTEM"
                    );
                return(Ok());
            }
            else if (checkHelperCase.Rows.Count > 0) // Helper Case
            {
                if (checkHelperCase.Rows[0]["Log_Message"].ToString() == "ช่วยเหลือ")
                {
                    message             = api.SetMessage("ทางเราได้รับทราบปัญหาแล้ว จะเร่งดำเนินการแก้ไขให้ครับ");
                    response.replyToken = request.events[0].replyToken;
                    response.messages.Add(message);
                    await api.CallApi(response);

                    action.SP_InsertLogRequestMessage(
                        "reply",
                        request.events[0].source.userId,
                        request.events[0].mode,
                        request.events[0].message.text,
                        "",
                        DateTime.Now,
                        request.destination,
                        "SYSTEM"
                        );
                    dt = new DataTable();
                    dt = DAC.REST_GetNeedHelpMessage(request.events[0].source.userId);
                    string alertLine = template.MessageNeedHelp();
                    if (dt.Rows.Count > 0)
                    {
                        alertLine = string.Format(alertLine, dt.Rows[0]["User_Role"].ToString(), dt.Rows[0]["BranchCode"].ToString(), dt.Rows[0]["User_Name"].ToString(), dt.Rows[0]["RequestDate"].ToString(), dt.Rows[0]["message"].ToString());

                        await api.MessageToGroupSupport(alertLine);
                    }

                    return(Ok());
                }
            }
            else
            {
                if (request.events[0].message.text == "เริ่มต้นใช้งาน")
                {
                    message             = api.SetMessage("กรุณากรอก Secret Code ที่ได้รับ เพื่อเริ่มต้นใช้งาน");
                    response.replyToken = request.events[0].replyToken;
                    response.messages.Add(message);
                    await api.CallApi(response);

                    return(Ok());
                }
                // Check is AppNo
                dt = new DataTable();
                dt = DAC.CheckApplicationNo(request.events[0].message.text);
                if (dt.Rows.Count > 0)
                {
                    dtifExists = new DataTable();
                    dtifExists = DAC.REST_CheckAceptTaskExisting(request.events[0].message.text);
                    if (dtifExists.Rows.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(dtifExists.Rows[0]["Application_Responsibility"].ToString()))
                        {
                            message             = api.SetMessage("ไม่สามารถทำรายการได้เนื่องจากมีคนกดรับงานไปแล้ว");
                            response.replyToken = request.events[0].replyToken;
                            response.messages.Add(message);
                            await api.CallApi(response);

                            return(Ok());
                        }
                    }
                    checker.AcceptTask(request.events[0].source.userId, dt.Rows[0]["Application_No"].ToString());
                    message             = api.SetMessage("บันทึกข้อมูลสำเร็จ");
                    response.replyToken = request.events[0].replyToken;
                    response.messages.Add(message);
                    await api.CallApi(response);

                    return(Ok());
                }

                dt = new DataTable();
                dt = DAC.CheckSecretCode(request.events[0].message.text);
                if (dt.Rows.Count == 0)
                {
                    msg                 = string.Format(template.UnAuthorizeMessage(), profile.Result.displayName);
                    message             = api.SetMessage(msg);
                    response.replyToken = request.events[0].replyToken;
                    response.messages.Add(message);
                    await api.CallApi(response);
                }
                else
                {
                    dt = new DataTable();
                    dt = DAC.MatchingUser(request.events[0].source.userId, request.events[0].message.text);

                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["User_Role"].ToString() == "Checker" || dt.Rows[0]["User_Role"].ToString() == "BranchHead")
                        {
                            typeofMenu = "checker";
                            menu       = template.SetupRichMenuChecker(request.events[0].source.userId);
                        }
                        else
                        {
                            typeofMenu = "dealer";
                            menu       = template.SetupRichMenuDealer(request.events[0].source.userId);
                        }

                        if (string.IsNullOrEmpty(dt.Rows[0]["User_RichMenuID"].ToString()))
                        {
                            result = api.SetupMenu(menu, typeofMenu);
                            await api.SetupBackgroundMenu(result.Result.richMenuId, typeofMenu + ".png");

                            if (result != null)
                            {
                                try
                                {
                                    await api.SetDefaultMenu(result.Result.richMenuId, request.events[0].source.userId);
                                }
                                catch (Exception e)
                                {
                                    await api.DeleteMenu(result.Result.richMenuId);

                                    return(BadRequest(e.Message));
                                }
                                finally
                                {
                                    DAC.SetupRichmenubyUser(request.events[0].source.userId, result.Result.richMenuId);
                                }
                            }
                        }

                        dt = new DataTable();
                        dt = DAC.GetUserInformation(request.events[0].source.userId);
                        if (dt.Rows.Count > 0)
                        {
                            msg = string.Format(
                                template.PassAuthorizeMessage()
                                , dt.Rows[0]["User_Name"].ToString()
                                , dt.Rows[0]["User_Code"].ToString()
                                , dt.Rows[0]["User_Area"].ToString()
                                );
                            message             = api.SetMessage(msg);
                            response.replyToken = request.events[0].replyToken;
                            response.messages.Add(message);
                            await api.CallApi(response);
                        }
                    }
                    else
                    {
                    }
                }
            }

            return(Ok());
        }