//ConfirmTemplate
        protected void Button2_Click(object sender, EventArgs e)
        {
            var bot = new isRock.LineBot.Bot(channelAccessToken);

            var CarouselTmpMsg = new isRock.LineBot.ConfirmTemplate()
            {
                text    = "文字",
                altText = "替代文字",
            };
            //add actions
            var action1 = new isRock.LineBot.MessageAction()
            {
                label = "OK", text = "呈現的文字"
            };

            CarouselTmpMsg.actions.Add(action1);

            var action2 = new isRock.LineBot.UriAction()
            {
                label = "NO", uri = new Uri("http://www.google.com")
            };

            CarouselTmpMsg.actions.Add(action2);

            bot.PushMessage(AdminUserId, CarouselTmpMsg);
        }
        //ButtonsTemplate
        protected void Button1_Click(object sender, EventArgs e)
        {
            var bot = new isRock.LineBot.Bot(channelAccessToken);

            var ButtonTmpMsg = new isRock.LineBot.ButtonsTemplate()
            {
                text              = "文字",
                title             = "標題",
                altText           = "替代文字",
                thumbnailImageUrl = new Uri("https://66.media.tumblr.com/2e37eafc9b6b715ed99b31fb6f72e6a5/tumblr_inline_pjjzfnFy7a1u06gc8_640.jpg")
            };
            //add actions
            var action1 = new isRock.LineBot.MessageAction()
            {
                label = "顯示的標題", text = "呈現的文字"
            };

            ButtonTmpMsg.actions.Add(action1);

            var action2 = new isRock.LineBot.UriAction()
            {
                label = "顯示的標題", uri = new Uri("http://www.google.com")
            };

            ButtonTmpMsg.actions.Add(action2);

            bot.PushMessage(AdminUserId, ButtonTmpMsg);
        }
Esempio n. 3
0
        public IHttpActionResult POST()
        {
            try
            {
                //設定ChannelAccessToken(或抓取Web.Config)
                this.ChannelAccessToken = channelAccessToken;
                //取得Line Event(範例,只取第一個)
                var LineEvent = this.ReceivedMessage.events.FirstOrDefault();
                //配合Line verify
                if (LineEvent.replyToken == "00000000000000000000000000000000")
                {
                    return(Ok());
                }
                //回覆訊息
                if (LineEvent.type == "message")
                {
                    var repmsg = "";
                    if (LineEvent.message.type == "text") //收到文字
                    {
                        //建立 MsQnAMaker Client
                        var helper = new isRock.MsQnAMaker.Client(
                            new Uri(QnAEndpoint), QnAKey);
                        var QnAResponse = helper.GetResponse(LineEvent.message.text.Trim());
                        var ret         = (from c in QnAResponse.answers
                                           orderby c.score descending
                                           select c
                                           ).Take(1);

                        var responseText = UnknowAnswer;
                        if (ret.FirstOrDefault().score > 0)
                        {
                            responseText = ret.FirstOrDefault().answer;
                        }
                        //回覆
                        this.ReplyMessage(LineEvent.replyToken, responseText);
                    }
                    if (LineEvent.message.type == "sticker") //收到貼圖
                    {
                        this.ReplyMessage(LineEvent.replyToken, 1, 2);
                    }

                    if (LineEvent.message.text.ToLower().Contains("template message"))
                    {
                        //define actions
                        var act1 = new isRock.LineBot.MessageAction()
                        {
                            text = "test action1", label = "test action1"
                        };
                        var act2 = new isRock.LineBot.MessageAction()
                        {
                            text = "test action2", label = "test action2"
                        };

                        var tmp = new isRock.LineBot.ButtonsTemplate()
                        {
                            text              = "Button Template text",
                            title             = "Button Template title",
                            thumbnailImageUrl = new Uri("https://i.imgur.com/wVpGCoP.png"),
                        };

                        tmp.actions.Add(act1);
                        tmp.actions.Add(act2);
                        //add TemplateMessage into responseMsgs
                        this.ReplyMessage(new isRock.LineBot.TemplateMessage(tmp));
                    }
                }
                //response OK
                return(Ok());
            }
            catch (Exception ex)
            {
                //如果發生錯誤,傳訊息給Admin
                this.PushMessage(AdminUserId, "發生錯誤:\n" + ex.Message);
                //response OK
                return(Ok());
            }
        }
        //命令列處理
        private List <isRock.LineBot.MessageBase> ProcessCommand(isRock.LineBot.Event lineEvent)
        {
            //把多格空白換成一個
            var cmdLine = MergeSpace(lineEvent.message.text);
            //取消  /  ,以空白做分隔符號,切出
            var words    = cmdLine.ToLower().Replace("/", "").Split(' ').ToList();
            var idOrName = "";
            //回覆訊息物件集合
            var retMessages = new List <isRock.LineBot.MessageBase>();

            //執行api必須的參數
            var AzureDevOpsProjectName      = "";
            var AzureDevOpsOrganizationName = "";
            var AzureDevOpsUserPAT          = "";
            var AzureDevOpsUserName         = "";

            //如果命令中有 setup
            if (words.Contains("setup") && words[0] == "setup")
            {
                //keep data;
                System.Web.HttpContext.Current.Application[lineEvent.source.userId] =
                    $"{words[1]},{words[2]},{words[3]},{words[4]}";
                //System.Web.HttpContext.Current.Application["AzureDevOpsOrganizationName"] = words[1];
                //System.Web.HttpContext.Current.Application["AzureDevOpsProjectName"] = words[2];
                //System.Web.HttpContext.Current.Application["AzureDevOpsUserName"] = words[3];
                //System.Web.HttpContext.Current.Application["AzureDevOpsUserPAT"] = words[4];

                retMessages.Add(new TextMessage("環境參數設定完成!"));
                return(retMessages);
            }


            //取得環境設定
            if (System.Web.HttpContext.Current.Application[lineEvent.source.userId] == null)
            {
                //缺資料,要求輸入
                retMessages.Add(new TextMessage("請先使用 /setup 指令設定環境參數. \n ex. /setup [OrgName] [ProjectName] [UserName] [PAT]"));
                return(retMessages);
            }
            else
            {
                var para = System.Web.HttpContext.Current.Application[lineEvent.source.userId].ToString().Split(',');
                //取得資料
                AzureDevOpsProjectName      = para[1].Trim();
                AzureDevOpsOrganizationName = para[0].Trim();
                AzureDevOpsUserPAT          = para[3].Trim();
                AzureDevOpsUserName         = para[2].Trim();
            }

            //取得approvers
            if (words.Contains("get") && (words.Contains("approver") || words.Contains("approvers")))
            {
                // 建立 AzureDevOpsRestApiClient
                AzureDevOpsRestApiClient client = new AzureDevOpsRestApiClient(
                    AzureDevOpsUserName, AzureDevOpsUserPAT, AzureDevOpsOrganizationName, AzureDevOpsProjectName);
                var ret = client.GetApprovers();
                if (ret != null & ret.count != 0)
                {
                    var ButtonsTemplateMsg = new isRock.LineBot.ButtonsTemplate();
                    var msg = $"共有 {  ret.count} 個等待簽核項目...";

                    ButtonsTemplateMsg.text              = msg;
                    ButtonsTemplateMsg.title             = "等待簽核項目...";
                    ButtonsTemplateMsg.thumbnailImageUrl = new Uri("https://i.imgur.com/pAiJpHg.png");
                    foreach (var item in ret.value)
                    {
                        var action = new isRock.LineBot.MessageAction()
                        {
                            label = $"確定簽核 [編號:{ item.id }]", text = "/Make Approve " + item.id
                        };
                        ButtonsTemplateMsg.actions.Add(action);
                        msg += $"NO.{ item.id } ";
                    }
                    retMessages.Add(new isRock.LineBot.TextMessage(msg));
                    retMessages.Add(new isRock.LineBot.TemplateMessage(ButtonsTemplateMsg));
                }
                else
                {
                    retMessages.Add(new isRock.LineBot.TextMessage($"找不到等待簽核的項目..."));
                }

                return(retMessages);
            }

            //如果命令中有 start & build
            if (words.Contains("start") && words.Contains("build"))
            {
                //找出build後面的元素
                var n = words.FindIndex(c => c == "build");
                if (words.Count() >= n)
                {
                    idOrName = words[n + 1];
                }


                //如果沒有 id, return
                if (string.IsNullOrEmpty(idOrName))
                {
                    return(null);
                }

                // 建立 AzureDevOpsRestApiClient
                AzureDevOpsRestApiClient client = new AzureDevOpsRestApiClient(
                    AzureDevOpsUserName, AzureDevOpsUserPAT, AzureDevOpsOrganizationName, AzureDevOpsProjectName);

                var BuildId = -1;
                //如果id不是數字
                if (int.TryParse(idOrName, out BuildId) == false)
                {
                    //取得id(文字)所對應的build definition
                    var Definitions = client.GetDefinitions();
                    //如果成功取回Definitions
                    if (Definitions != null && Definitions.count > 0)
                    {
                        //找對應名稱的 denifition
                        var Definition = from d in Definitions.value
                                         where d.name.ToLower() == idOrName.ToLower()
                                         select d;
                        if (Definition != null && Definition.Count() >= 1)
                        {
                            //如果有找到對應名稱的 denifition
                            BuildId = Definition.FirstOrDefault().id;
                        }
                        else
                        {
                            retMessages.Add(new isRock.LineBot.TextMessage($"找不到build {idOrName}"));
                            return(retMessages);
                        }
                    }
                    else
                    {
                        retMessages.Add(new isRock.LineBot.TextMessage($"找不到build Definitions"));
                        return(retMessages);
                    }
                }

                //如果找不到build
                if (BuildId <= -1)
                {
                    return(null);
                }

                var ret = client.QueueNewBuild(BuildId);
                if (ret != null)
                {
                    retMessages.Add(new isRock.LineBot.TextMessage($"Build {BuildId} 啟動中... \n Build Name : {ret.definition.name} \n Queue ID : {ret.queue.id} \n buildNumber : {ret.buildNumber}"));
                }
                else
                {
                    retMessages.Add(new isRock.LineBot.TextMessage($"Build {BuildId} 啟動失敗..."));
                }
                return(retMessages);
            }

            if (words.Contains("make") && words.Contains("approve"))
            {
                //找出build後面的元素
                var n = words.FindIndex(c => c == "approve");
                if (words.Count() >= n)
                {
                    idOrName = words[n + 1];
                }

                //如果沒有 id, return
                if (string.IsNullOrEmpty(idOrName))
                {
                    return(null);
                }

                // 建立 AzureDevOpsRestApiClient
                AzureDevOpsRestApiClient client = new AzureDevOpsRestApiClient(
                    AzureDevOpsUserName, AzureDevOpsUserPAT, AzureDevOpsOrganizationName, AzureDevOpsProjectName);


                var ret = client.MakeApprove(int.Parse(idOrName), "ok");

                retMessages.Add(new isRock.LineBot.TextMessage($"單據編號 : {ret.id} {ret.status}"));
                return(retMessages);
            }

            //最後沒有任何指令被執行
            if (retMessages.Count <= 0)
            {
                retMessages.Add(new isRock.LineBot.TextMessage("指令錯誤! 什麼事情都沒發生!"));
                retMessages.Add(new isRock.LineBot.StickerMessage(1, 2));
            }
            return(retMessages);
        }