Example #1
0
        private string Learn(string userSay, isRock.LineBot.Event LineEvent)
        {
            //預設回覆文字
            var respMsg = "我沒法依照你的指令學習...\n";

            respMsg += "你必須跟我說:\n";
            respMsg += "看到 OO 回覆 XX \n";
            respMsg += "這樣以後當我看到  OO 就會回覆 XX 囉...";
            respMsg += "(別忘了OO和XX的前後要有空白唷!)";

            //全形空白轉半形
            userSay = userSay.Replace(" ", " ");
            //拆解教學語句
            var words = userSay.Split(' ');

            //去除空元素(可能是兩個空白造成的)
            words = words.Where(c => !string.IsNullOrEmpty(c)).ToArray();
            //去除空白
            for (int i = 0; i < words.Length; i++)
            {
                //去除空白
                words[i] = words[i].Trim();
            }
            //判斷指令是否正確
            if (words[0] != "看到")
            {
                return(respMsg);
            }
            if (words[2] != "回覆" && words[2] != "回復")
            {
                return(respMsg);
            }
            if (words.Length < 4)
            {
                respMsg += $"\n\n你的教學指令:\n{userSay}\n格式不正確喔~";
                return(respMsg);
            }

            //新增教學到資料庫
            Models.blah rec = new Models.blah();
            rec.LineUserId  = LineEvent.source.userId;
            rec.UserSay     = words[1].Trim();
            rec.isValid     = true;
            rec.ResponseMsg = words[3];

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

            respMsg  = "懂了...\n";
            respMsg += $"以後當我看到包含 '{rec.UserSay}' 的句子,\n就會回覆 '{rec.ResponseMsg}' 囉...";
            return(respMsg);
        }
        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());
            }
        }