public IHttpActionResult POST() { string ChannelAccessToken = "iNNBsIPiNof4jbpItGP+ypxTkKManP+E3hnBxXwi19ocbv3hc0SkKwo0AzBof1FIGtfwU2JhwlKliADbZ7R0zeCOPj2FNCnXpEjh41fDanX+1d59gZEOhMPF+AIiOfe3J1R+n/viuS50u3/iFocdhwdB04t89/1O/w1cDnyilFU="; isRock.LineBot.Bot bot = new isRock.LineBot.Bot(ChannelAccessToken); //取得 http Post RawData(should be JSON) string postData = Request.Content.ReadAsStringAsync( ).Result; var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData); var item = ReceivedMessage.events[0]; //var userinfo = isRock.LineBot.Utility.GetUserInfo ( ReceivedMessage.events[0].source.userId, ChannelAccessToken ); //var userInfo = bot.GetUserInfo ( ReceivedMessage.events.FirstOrDefault ( ).source.userId ); //var userInfo = bot.GetUserInfo ( ReceivedMessage.events.FirstOrDefault ( ).source.userId ); //var UserSays = ReceivedMessage.events[0].message.text; var ReplyToken = ReceivedMessage.events[0].replyToken; //string Message = ""; bool Trigger = false; //String CurrMsg = ReceivedMessage.events[0].message.text.Substring ( 4 ).ToString ( ); if (ReceivedMessage.events[0].message.type.ToLower( ) == "sticker") { bot.ReplyMessage(ReceivedMessage.events[0].replyToken, " 我的主人還沒教我怎麼使用貼圖!><'''"); return(Ok( )); } else { try { UserID = ReceivedMessage.events[0].source.userId; string RoomID = ""; if (item.source.type.ToLower( ) == "room") { RoomID = item.source.roomId; } else if (item.source.type.ToLower( ) == "group") { RoomID = item.source.groupId; } else { } AddMsg(UserID, RoomID, ReceivedMessage.events[0].message.text); } catch (Exception ex) { bot.ReplyMessage(ReceivedMessage.events[0].replyToken, "add ERR:" + ex.ToString( )); } Message = REPLAYMSG(ReceivedMessage.events[0].message.text); if (Message == "") { if (ReceivedMessage.events[0].message.text.Length <15 && ReceivedMessage.events[0].message.text.ToLower( ).IndexOf("google", 0)> -1) { string searchStr = ""; searchStr = ReceivedMessage.events[0].message.text; searchStr = searchStr.Replace("GOOGLE", ""); searchStr = searchStr.Replace("google", ""); searchStr = searchStr.Replace(" ", ""); Message = "google大神是你的好朋友" + "\n" + "https://www.google.com/search?q=" + searchStr; Trigger = true; } else { //Message = "啦啦啦,不想理你!"; //Trigger = true; } } else { Trigger = true; } try { if (Trigger) { bot.ReplyMessage(ReceivedMessage.events[0].replyToken, Message); } return(Ok( )); } catch (Exception ex) { //bot.ReplyMessage ( ReceivedMessage.events[0].replyToken, "POST ERR:" + ex.ToString ( ) ); return(Ok( )); } } }
public IHttpActionResult POST() { //設定你的Channel Access Token string ChannelAccessToken = "FRIVkZ1ddfC90UObByPBHb/RfaotMELEVzQ4YPlKJOzSPrDCJ2uzeewA1mVaSX7+e3Qip3deQ5xjpHU9ut8v+HRpt4xwmxefP8O8GlUb8ynT7v6gmErTDz5+Xl1kyI1+YGpu3DdE24G1oQ/om/gi3QdB04t89/1O/w1cDnyilFU="; isRock.LineBot.Bot bot; //如果有Web.Config app setting,以此優先 if (System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("LineChannelAccessToken")) { ChannelAccessToken = System.Configuration.ConfigurationManager.AppSettings["LineChannelAccessToken"]; } //create bot instance bot = new isRock.LineBot.Bot(ChannelAccessToken); try { //取得 http Post RawData(should be JSON) string postData = Request.Content.ReadAsStringAsync().Result; //剖析JSON var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData); var UserSays = ReceivedMessage.events[0].message.text; var ReplyToken = ReceivedMessage.events[0].replyToken; string Message = ""; MysqlLineID mysqlLineID = new MysqlLineID(); var item = ReceivedMessage.events.FirstOrDefault(); //依照用戶說的特定關鍵字來回應 switch (UserSays.ToLower()) { case "/teststicker": //回覆貼圖 bot.ReplyMessage(ReplyToken, 1, 1); break; case "/testimage": //回覆圖片 bot.ReplyMessage(ReplyToken, new Uri("https://scontent-tpe1-1.xx.fbcdn.net/v/t31.0-8/15800635_1324407647598805_917901174271992826_o.jpg?oh=2fe14b080454b33be59cdfea8245406d&oe=591D5C94")); break; case "b": if (mysqlLineID.DBConnectionInsert(@"INSERT INTO `linemessage`(`Msg`, `ID`) VALUES ('" + UserSays + "','" + ReceivedMessage.events[0].source.userId + "');") == "true") { Message += "預約成功,您預約資料為:" + UserSays; } else { Message += "輸入錯誤!!,請重新輸入!!"; } bot.ReplyMessage(ReplyToken, Message + "!"); break; default: //回覆訊息 Message += "哈囉, 歡迎您加入,預約請輸入 b :"; //+ UserSays; //+"ID:"+ReceivedMessage.events[0].source.userId; //測試方法: 紀錄 if (!mysqlLineID.DBConnectionjudge("lineid", ReceivedMessage.events[0].source.userId)) { Message += mysqlLineID.DBConnectionInsert(@"INSERT INTO `lineid`(`Name`, `ID`) VALUES ('Test','" + ReceivedMessage.events[0].source.userId + "');"); //Message += mysqlLineID.DBConnectionInsert(@"INSERT INTO `linemessage`(`Msg`, `ID`) VALUES ('" + UserSays + "','" + ReceivedMessage.events[0].source.userId + "');"); } //回覆用戶 bot.ReplyMessage(ReplyToken, Message + "!"); break; } //回覆API OK return(Ok()); } catch (Exception ex) { 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; } }
public void ReplyMessage(string ReplyToken, string Message) { _RockBot.ReplyMessage(ReplyToken, Message); }