Пример #1
0
        protected void Button6_Click(object sender, EventArgs e)
        {
            isRock.LineBot.Bot bot = new isRock.LineBot.Bot("9YwfPyWbdWkYqcs5aistrwDTZaPwLAivy+9vpvKS034TVyF9Cj7UhHcttzo4CJ1+zLH7YadJ7B5U9a9ho/4Kg6mU+Z5u0bHvo8zo7y3+8BwccBpL+4QDGrknX16T3roNmLnxVaOhmwkyXXQ/G2INFwdB04t89/1O/w1cDnyilFU=");
            var Userinfo           = bot.GetUserInfo("U275c68b802e11bb599413ef87dcea051");

            Response.Write(Userinfo.displayName + "<br/>" + Userinfo.pictureUrl + "<br/>" + Userinfo.statusMessage);
        }
Пример #2
0
        public IHttpActionResult POST()
        {
            try
            {
                //設定ChannelAccessToken(或抓取Web.Config)
                this.ChannelAccessToken = channelAccessToken;
                //取得Line Event(範例,只取第一個)
                var LineEvent          = this.ReceivedMessage.events.FirstOrDefault();
                isRock.LineBot.Bot bot = new isRock.LineBot.Bot(channelAccessToken);
                var UserInfo           = bot.GetUserInfo(LineEvent.source.userId);

                //配合Line verify
                //
                if (LineEvent.replyToken == "00000000000000000000000000000000")
                {
                    return(Ok());
                }
                if (LineEvent.type == "postback")
                {
                    var data = LineEvent.postback.data;
                    var dt   = LineEvent.postback.Params.time;
                    this.ReplyMessage(LineEvent.replyToken, $"觸發了 postback \n 資料為:{data}\n 選擇結果:{dt} ");
                }
                if (LineEvent.type == "message")
                {
                    //回覆訊息
                    //if (LineEvent.message.type == "sticker") //收到貼圖
                    //    this.ReplyMessage(LineEvent.replyToken, 1, 2);
                    if (LineEvent.message.type == "location") //GPS
                    {
                        this.ReplyMessage(LineEvent.replyToken, $"你的位置在\n{LineEvent.message.latitude},{LineEvent.message.longitude}\n{LineEvent.message.address}");
                    }

                    if (LineEvent.message.type == "text")
                    {
                        if (LineEvent.message.text == "Hello")
                        {
                            this.ReplyMessage(LineEvent.replyToken, UserInfo.displayName + "您好,今天適合穿短袖上衣");
                        }
                    }

                    if (LineEvent.message.text == "餓了嗎")
                    {
                        var bott = new Bot(channelAccessToken);
                        //建立actions,作為ButtonTemplate的用戶回復行為
                        var actions = new List <isRock.LineBot.TemplateActionBase>();
                        actions.Add(new isRock.LineBot.MessageAction()
                        {
                            label = "Yes", text = "Yes"
                        });
                        actions.Add(new isRock.LineBot.MessageAction()
                        {
                            label = "No", text = "No"
                        });

                        var ConfirmTemplate = new isRock.LineBot.ConfirmTemplate()
                        {
                            text    = "請選擇其中之一",
                            altText = "請在手機上檢視",

                            actions = actions
                        };
                        bott.PushMessage(AdminUserId, ConfirmTemplate);
                    }
                    if (LineEvent.message.text == "Yes")
                    {
                        var bot1 = new Bot(channelAccessToken);
                        //建立actions,作為ButtonTemplate的用戶回復行為
                        var actions = new List <isRock.LineBot.TemplateActionBase>();
                        actions.Add(new isRock.LineBot.MessageAction()
                        {
                            label = "標題-文字回復", text = "回復文字"
                        });
                        actions.Add(new isRock.LineBot.UriAction()
                        {
                            label = "選擇餐廳", uri = new Uri("https://tgifridays.com.tw/locations")
                        });
                        // actions.Add(new isRock.LineBot.PostbackAction() { label = "標題-發生postback", data = "abc=aaa&def=111" });
                        actions.Add(new isRock.LineBot.DateTimePickerAction()
                        {
                            label = "請選擇時間", mode = "date"
                        });
                        var ButtonTemplateMsg = new isRock.LineBot.ButtonsTemplate()
                        {
                            title             = "選項",
                            text              = "請選擇其中之一",
                            altText           = "請在手機上檢視",
                            thumbnailImageUrl = new Uri("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQSfptbc-INs9IUyaBi7xU3_Hr52NbdIEOwOa_gX5xrrQXEd0m7w"),
                            actions           = actions
                        };
                        bot1.PushMessage(AdminUserId, ButtonTemplateMsg);
                    }
                    if (LineEvent.message.type == "image")
                    {
                        //取得圖片Bytes
                        var bytes = this.GetUserUploadedContent(LineEvent.message.id);
                        //儲存為圖檔
                        var guid     = Guid.NewGuid().ToString();
                        var filename = $"{guid}.png";
                        var path     = System.Web.Hosting.HostingEnvironment.MapPath("~/Temps/");

                        System.IO.File.WriteAllBytes(path + filename, bytes);

                        //取得base URL
                        var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
                        //組出外部可以讀取的檔名
                        var url = $"{baseUrl}/Temps/{filename}";
                        // this.ReplyMessage(LineEvent.replyToken, $"你的圖片位於\n{url}");
                        var fs1 = new FileStream(path + filename, FileMode.Open);
                        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fs1);
                        Graphics g = Graphics.FromImage(bmp);
                        fs1.Close();

                        var visionClient = new Microsoft.ProjectOxford.Vision.VisionServiceClient(VisionAPIKey,
                                                                                                  "https://southeastasia.api.cognitive.microsoft.com/vision/v1.0");

                        //分析圖片(從FileUpload1.PostedFile.InputStream取得影像)
                        //分析 Faces & Description

                        var Results = visionClient.AnalyzeImageAsync(url,
                                                                     new VisualFeature[] { VisualFeature.Faces, VisualFeature.Description }).Result;

                        int isM = 0, isF = 0;
                        foreach (var Face in Results.Faces)
                        {
                            //取得人臉位置
                            var faceRect = Face.FaceRectangle;
                            //繪製人臉紅框
                            g.DrawRectangle(
                                new Pen(Brushes.Red, 10),
                                new Rectangle(faceRect.Left, faceRect.Top,
                                              faceRect.Width, faceRect.Height));

                            Font       drawFont   = new Font("Arial", 40);
                            SolidBrush drawBrush  = new SolidBrush(Color.Red);
                            String     drawString = Face.Age.ToString();

                            g.DrawString(drawString + "歲", drawFont, drawBrush, new Point(faceRect.Left - 30, faceRect.Top - 50));

                            //計算幾男幾女
                            if (Face.Gender.StartsWith("M"))
                            {
                                isM += 1;
                            }
                            else
                            {
                                isF += 1;
                            }
                        }
                        var path1       = System.Web.Hosting.HostingEnvironment.MapPath("~/Temps/");
                        var newfilename = Guid.NewGuid().ToString() + ".png";
                        bmp.Save(path1 + newfilename);

                        this.ReplyMessage(LineEvent.replyToken, new Uri($"{baseUrl}/Temps/{newfilename}"));
                    }
                }

                return(Ok());

                //this.ReplyMessage(LineEvent.replyToken, "Hello,你的UserId是:" + LineEvent.source.userId);
            }
            catch (Exception ex)
            {
                //如果發生錯誤,傳訊息給Admin
                this.PushMessage(AdminUserId, "發生錯誤:\n" + ex.Message);
                //response OK
                return(Ok());
            }
        }
        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")
                {
                    isRock.LineBot.Bot bot = new isRock.LineBot.Bot(ChannelAccessToken);
                    var userinfo           = bot.GetUserInfo(LineEvent.source.userId);
                    if (LineEvent.message.type == "text") //收到文字
                    {
                        switch (LineEvent.message.text)
                        {
                        case "/上一頁":
                            SwitchMenuTo("快捷選單1", LineEvent);
                            break;

                        case "/下一頁":
                            SwitchMenuTo("快捷選單2", LineEvent);
                            break;

                        case "秘書告退":
                            if (LineEvent.source.type.ToLower() == "room")
                            {
                                Utility.LeaveRoom(LineEvent.source.roomId, channelAccessToken);
                            }
                            else if (LineEvent.source.type.ToLower() == "group")
                            {
                                Utility.LeaveRoom(LineEvent.source.groupId, channelAccessToken);
                            }
                            else
                            {
                                this.ReplyMessage(LineEvent.replyToken, "你開玩笑嗎?");
                            }
                            break;

                        default:
                            Models.blah rec = new Models.blah();
                            rec.userId      = LineEvent.source.userId;
                            rec.displayName = userinfo.displayName;
                            rec.message     = LineEvent.message.text;
                            rec.createdDate = DateTime.Now;

                            Models.MainDBDataContext db = new Models.MainDBDataContext();
                            db.blah.InsertOnSubmit(rec);
                            db.SubmitChanges();

                            this.ReplyMessage(LineEvent.replyToken, "Hi," + userinfo.displayName + "(" + LineEvent.source.userId + "), 你說了:"
                                              + LineEvent.message.text + "(" + DateTime.Now.ToString() + ")");
                            break;
                        }
                    }
                    if (LineEvent.message.type == "sticker") //收到貼圖
                    {
                        this.ReplyMessage(LineEvent.replyToken, 1, 2);
                    }
                }
                //回覆訊息
                if (LineEvent.type == "image")
                {
                    string path     = System.Web.HttpContext.Current.Request.MapPath("/temp/");
                    string filename = Guid.NewGuid().ToString() + ".png";
                    var    filebody = this.GetUserUploadedContent(LineEvent.message.id);
                    System.IO.File.WriteAllBytes(path + filename, filebody);
                }
                //檢查用戶如果當前沒有任何選單,則嘗試綁定快捷選單1
                var currentMenu = isRock.LineBot.Utility.GetRichMenuIdOfUser(LineEvent.source.userId, channelAccessToken);
                if (currentMenu == null || string.IsNullOrEmpty(currentMenu.richMenuId))
                {
                    SwitchMenuTo("快捷選單1", LineEvent);
                }
                if (isRock.LineBot.Utility.GetRichMenu(currentMenu.richMenuId, channelAccessToken) == null)
                {
                    SwitchMenuTo("快捷選單1", LineEvent);
                }
                //response OK
                return(Ok());
            }
            catch (Exception ex)
            {
                //如果發生錯誤,傳訊息給Admin
                this.PushMessage(AdminUserId, "發生錯誤:\n" + ex.Message);
                //response OK
                return(Ok());
            }
        }
        public static void Handle(int _subState, ReceievedMessage _ReceivedMessage)
        {
            var _LineEvent   = _ReceivedMessage.events.FirstOrDefault();
            var _userId      = _LineEvent.source.userId;
            var ChannelToken = ConfigurationManager.AppSettings["ChannelAccessToken"];

            isRock.LineBot.Bot bot = new isRock.LineBot.Bot(ChannelToken);
            var userInfo           = bot.GetUserInfo(_userId);

            switch (_subState)
            {
            case (int)SubStateEnum.Step0:

                if (!Process_Validate.Handle(_LineEvent, bot))
                {
                    return;
                }

                bot.ReplyMessage(_LineEvent.replyToken, $"哈,'{userInfo.displayName}' 你來了...歡迎,現在開始用藥流程對話");
                azQuery.resetStatus(string.Concat(0, (int)StateEnum.DrugRemind, 0, (int)SubStateEnum.Step1));

                break;

            case (int)SubStateEnum.Step1:

                if (!Process_Validate.Handle(_LineEvent, bot))
                {
                    return;
                }

                bot.ReplyMessage(_LineEvent.replyToken, $"哈,'{userInfo.displayName}' 現在是第一階段對話");
                azQuery.resetStatus(string.Concat(0, (int)StateEnum.DrugRemind, 0, (int)SubStateEnum.Step2));

                break;

            case (int)SubStateEnum.Step2:

                if (!Process_Validate.Handle(_LineEvent, bot))
                {
                    return;
                }

                bot.ReplyMessage(_LineEvent.replyToken, $"哈,'{userInfo.displayName}' 現在是第二階段對話");
                azQuery.resetStatus(string.Concat(0, (int)StateEnum.DrugRemind, 0, (int)SubStateEnum.Step3));

                break;

            case (int)SubStateEnum.Step3:

                if (!Process_Validate.Handle(_LineEvent, bot))
                {
                    return;
                }

                bot.ReplyMessage(_LineEvent.replyToken, $"哈囉,'{userInfo.displayName}' 現在是最後階段對話");
                azQuery.resetStatus(string.Concat(0, (int)StateEnum.DrugRemind, 0, (int)SubStateEnum.StepReset));

                break;

            case (int)SubStateEnum.Step4:


                break;

            case (int)SubStateEnum.Step5:

                break;

            case (int)SubStateEnum.Step6:

                break;

            default:
                break;
            }
        }