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") { if (LineEvent.message.type == "text") //收到文字 { if (LineEvent.message.text.Contains("RPG-")) { //進到RPG指令區 if (LineEvent.message.text.Contains("TeM")) { List <isRock.LineBot.MessageBase> RetuenMessage = MessageCommand.RPGTeMCommand(LineEvent.message.text, LineEvent.source.roomId, LineEvent.source.userId); this.ReplyMessage(LineEvent.replyToken, RetuenMessage); } else { string RetuenMessage = MessageCommand.RPGCommand(LineEvent.message.text, LineEvent.source.roomId, LineEvent.source.userId); this.ReplyMessage(LineEvent.replyToken, RetuenMessage); } } else if (LineEvent.message.text.Contains("多圖片問答-")) { isRock.LineBot.Bot bot; bot = new isRock.LineBot.Bot(channelAccessToken); //取得 http Post RawData(should be JSO string postData = Request.Content.ReadAsStringAsync().Result; var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData); //建立actions,作為ButtonTemplate的用戶回覆行為 var actions = new List <isRock.LineBot.TemplateActionBase>(); actions.Add(new isRock.LineBot.MessageActon() { label = "標題-文字回覆", text = "回覆文字" }); actions.Add(new isRock.LineBot.UriActon() { label = "標題-Google", uri = new Uri("http://www.google.com") }); actions.Add(new isRock.LineBot.PostbackActon() { label = "標題-發生postack", data = "abc=aaa&def=111" }); //單一Column var Column = new isRock.LineBot.Column { text = "ButtonsTemplate文字訊息", title = "ButtonsTemplate標題", //設定圖片 thumbnailImageUrl = new Uri("https://arock.blob.core.windows.net/blogdata201706/22-124357-ad3c87d6-b9cc-488a-8150-1c2fe642d237.png"), actions = actions //設定回覆動作 }; //建立CarouselTemplate var CarouselTemplate = new isRock.LineBot.CarouselTemplate(); //這是範例,所以用一組樣板建立三個 CarouselTemplate.columns.Add(Column); CarouselTemplate.columns.Add(Column); CarouselTemplate.columns.Add(Column); this.ReplyMessage(LineEvent.replyToken, new isRock.LineBot.TemplateMessage(CarouselTemplate)); } else if (LineEvent.message.text.Contains("圖片問答-")) { isRock.LineBot.Bot bot; bot = new isRock.LineBot.Bot(channelAccessToken); //取得 http Post RawData(should be JSO string postData = Request.Content.ReadAsStringAsync().Result; var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData); //建立actions,作為ButtonTemplate的用戶回覆行為 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); //var UserID = isRock.LineBot.Utility.Parsing(postData).events[0].source.userId; //bot.PushMessage(UserID, ButtonTemplate); this.ReplyMessage(LineEvent.replyToken, new isRock.LineBot.TemplateMessage(tmp)); } else if (LineEvent.message.text.Contains("圖-")) { //isRock.LineBot.ImagemapMessage img = new isRock.LineBot.ImagemapMessage(new Uri("http://")); this.ReplyMessage(LineEvent.replyToken, new Uri("https://i.imgur.com/QqjmONg.png")); } else if (LineEvent.message.text.Contains("相關代號")) { string 回覆訊息 = ""; 回覆訊息 += "房間編號: " + LineEvent.source.roomId + "\n"; 回覆訊息 += "玩家編號: " + LineEvent.source.userId + "\n"; this.ReplyMessage(LineEvent.replyToken, 回覆訊息); } else if (LineEvent.message.text.Contains("測試")) { List <isRock.LineBot.MessageBase> responseMsgs = new List <isRock.LineBot.MessageBase>(); isRock.LineBot.MessageBase responseMsg = null; //add text response responseMsg = new isRock.LineBot.TextMessage($"you said : {LineEvent.message.text}"); responseMsgs.Add(responseMsg); //add ButtonsTemplate if user say "/Show ButtonsTemplate" if (LineEvent.message.text.ToLower().Contains("show buttonstemplate")) { //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 responseMsgs.Add(new isRock.LineBot.TemplateMessage(tmp)); } this.ReplyMessage(LineEvent.replyToken, responseMsgs); } } //else //{ // this.ReplyMessage(LineEvent.replyToken, "你說了:" + LineEvent.message.text); //} if (LineEvent.message.type == "sticker") //收到貼圖 { this.ReplyMessage(LineEvent.replyToken, 1, 2); } } //response OK return(Ok()); } catch (Exception ex) { var LineEvent = this.ReceivedMessage.events.FirstOrDefault(); this.ReplyMessage(LineEvent.replyToken, "發生錯誤:\n" + ex.Message); //如果發生錯誤,傳訊息給Admin this.PushMessage(AdminUserId, "發生錯誤:\n" + ex.Message); //response OK return(Ok()); } }
public IActionResult POST() { //get configuration from appsettings.json var token = _config.GetSection("LINE-Bot-Setting:channelAccessToken"); var AdminUserId = _config.GetSection("LINE-Bot-Setting:adminUserID"); var body = ""; //for JSON Body //create vot instance var bot = new isRock.LineBot.Bot(token.Value); isRock.LineBot.MessageBase responseMsg = null; //message collection for response multi-message List <isRock.LineBot.MessageBase> responseMsgs = new List <isRock.LineBot.MessageBase>(); try { //get JSON Body using (StreamReader reader = new StreamReader(Request.Body, System.Text.Encoding.UTF8)) { body = reader.ReadToEndAsync().Result; } //parsing JSON var ReceivedMessage = isRock.LineBot.Utility.Parsing(body); //Get LINE Event var LineEvent = ReceivedMessage.events.FirstOrDefault(); //prepare reply message if (LineEvent.type.ToLower() == "message") { switch (LineEvent.message.type.ToLower()) { case "text": //add text response responseMsg = new isRock.LineBot.TextMessage($"you said : {LineEvent.message.text}"); responseMsgs.Add(responseMsg); //add ButtonsTemplate if user say "/Show ButtonsTemplate" if (LineEvent.message.text.ToLower().Contains("/show buttonstemplate")) { //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 responseMsgs.Add(new isRock.LineBot.TemplateMessage(tmp)); } break; case "sticker": responseMsg = new isRock.LineBot.StickerMessage(1, 2); responseMsgs.Add(responseMsg); break; default: responseMsg = new isRock.LineBot.TextMessage($"None handled message type : { LineEvent.message.type}"); responseMsgs.Add(responseMsg); break; } } else { responseMsg = new isRock.LineBot.TextMessage($"None handled event type : { LineEvent.type}"); responseMsgs.Add(responseMsg); } //回覆訊息 bot.ReplyMessage(LineEvent.replyToken, responseMsgs); //response OK return(Ok()); } catch (Exception ex) { //如果有錯誤,push給admin bot.PushMessage(AdminUserId.Value, "Exception : \n" + ex.Message); //response OK return(Ok()); } }
public async Task <IActionResult> POST() { StickerList = Array.ConvertAll(_config.GetSection("Sticker:StickerList").Get <string> ().Split(","), int.Parse).ToList(); PackageId = _config.GetSection("Sticker:PackageId").Get <int> (); //get configuration from appsettings.json var token = _config.GetSection("channelAccessToken"); var AdminUserId = _config.GetSection("adminUserID"); var body = ""; //for JSON Body //create vot instance var bot = new isRock.LineBot.Bot(token.Value); isRock.LineBot.MessageBase responseMsg = null; //message collection for response multi-message List <isRock.LineBot.MessageBase> responseMsgs = new List <isRock.LineBot.MessageBase> (); try { //get JSON Body using (StreamReader reader = new StreamReader(Request.Body, System.Text.Encoding.UTF8)) { body = reader.ReadToEndAsync().Result; } //parsing JSON var ReceivedMessage = isRock.LineBot.Utility.Parsing(body); //Get LINE Event var LineEvent = ReceivedMessage.events.FirstOrDefault(); //prepare reply message if (LineEvent.type.ToLower() == "message") { switch (LineEvent.message.type.ToLower()) { case "text": Random random = new Random(); //add text response responseMsg = new isRock.LineBot.StickerMessage(PackageId, StickerList[random.Next(StickerList.Count - 1)]); responseMsgs.Add(responseMsg); //add ButtonsTemplate if user say "/Show ButtonsTemplate" if (LineEvent.message.text.ToLower().Contains("/show buttonstemplate")) { //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 responseMsgs.Add(new isRock.LineBot.TemplateMessage(tmp)); } else if (LineEvent.message.text.Contains("股票")) { string stockNo = Regex.Split(LineEvent.message.text, "股票.")[1]; var webCrawlerHelper = new WebCrawerHelper(_config); string data = webCrawlerHelper.getDatas(stockNo).Result; responseMsg = new isRock.LineBot.TextMessage(data); responseMsgs.Add(responseMsg); } break; case "sticker": responseMsg = new isRock.LineBot.StickerMessage(1, 2); responseMsgs.Add(responseMsg); break; default: var random2 = new Random(); // responseMsg = new isRock.LineBot.TextMessage($"None handled message type : { LineEvent.message.type}"); responseMsg = new isRock.LineBot.StickerMessage(PackageId, StickerList[random2.Next(StickerList.Count - 1)]); responseMsgs.Add(responseMsg); break; } } else { responseMsg = new isRock.LineBot.TextMessage($"None handled event type : { LineEvent.type}"); responseMsgs.Add(responseMsg); } await Task.CompletedTask; //回覆訊息 bot.ReplyMessage(LineEvent.replyToken, responseMsgs); //response OK return(Ok()); } catch (Exception ex) { //如果有錯誤,push給admin bot.PushMessage(AdminUserId.Value, "Exception : \n" + ex.Message); //response OK return(Ok()); } }