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 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());
            }
        }
Exemple #3
0
            public isRock.LineBot.CarouselTemplate 怪物出牌玩家抽卡並暫存(玩家 玩家, 怪物 怪物, ref List <isRock.LineBot.MessageBase> responseMsgs)
            {
                _key = Guid.NewGuid().ToString();

                _key = _key.Replace('-', '=');

                Random rdm1 = new Random(unchecked ((int)DateTime.Now.Ticks));

                //敵人抽牌 (只抽一張)
                int 怪物抽到編號 = rdm1.Next(0, 怪物.牌庫.Count);

                //怪物出牌顯示
                var act1 = new isRock.LineBot.MessageAction()
                {
                    text = " ", label = " "
                };

                var tmp = new isRock.LineBot.ButtonsTemplate()
                {
                    text              = 怪物.稱 + "階級:" + 怪物.階級 + "HP:" + 怪物.血量,
                    title             = "看樣子要使用: " + 怪物.牌庫[怪物抽到編號].卡牌名稱,
                    thumbnailImageUrl = new Uri(怪物.圖片網址),
                };

                tmp.actions.Add(act1);
                responseMsgs.Add(new isRock.LineBot.TemplateMessage(tmp));


                List <卡牌> 玩家抽到 = new List <卡牌>();

                int 牌庫數量;
                int 玩家抽到編號 = 0;

                // 玩家抽牌(抽三張)
                while (玩家抽到.Count < 3)
                {
                    牌庫數量   = 玩家.牌庫.Count - 1;
                    玩家抽到編號 = rdm1.Next(0, 牌庫數量);
                    if (!玩家抽到.Contains(玩家.牌庫[玩家抽到編號]))
                    {
                        玩家抽到.Add(玩家.牌庫[玩家抽到編號]);
                        玩家.牌庫.Remove(玩家.牌庫[玩家抽到編號]);
                    }
                }

                string 抽到卡牌 = "";

                foreach (卡牌 卡牌 in 玩家抽到)
                {
                    if (玩家抽到[玩家抽到.Count - 1] == 卡牌)
                    {
                        抽到卡牌 += 卡牌.順序編號;
                    }
                    else
                    {
                        抽到卡牌 += 卡牌.順序編號 + ",";
                    }
                }

                //玩家出牌顯示
                responseMsgs.Add(new isRock.LineBot.TextMessage("請選擇應對方式"));

                var actions1 = new List <isRock.LineBot.TemplateActionBase>();

                actions1.Add(new isRock.LineBot.MessageAction()
                {
                    label = "使用此張卡牌", text = "RPG-TeM-遭遇戰-出牌-" + _key + "-" + 玩家抽到[0].卡牌名稱
                });
                var actions2 = new List <isRock.LineBot.TemplateActionBase>();

                actions2.Add(new isRock.LineBot.MessageAction()
                {
                    label = "使用此張卡牌", text = "RPG-TeM-遭遇戰-出牌-" + _key + "-" + 玩家抽到[1].卡牌名稱
                });
                var actions3 = new List <isRock.LineBot.TemplateActionBase>();

                actions3.Add(new isRock.LineBot.MessageAction()
                {
                    label = "使用此張卡牌", text = "RPG-TeM-遭遇戰-出牌-" + _key + "-" + 玩家抽到[2].卡牌名稱
                });

                var Column1 = new isRock.LineBot.Column
                {
                    text  = "等級: " + 玩家抽到[0].等級 + " 說明:" + 玩家抽到[0].說明,
                    title = 玩家抽到[0].卡牌名稱,
                    //設定圖片
                    thumbnailImageUrl = new Uri(玩家抽到[0].圖片網址),
                    actions           = actions1 //設定回覆動作
                };

                var Column2 = new isRock.LineBot.Column
                {
                    text  = "等級: " + 玩家抽到[1].等級 + " 說明:" + 玩家抽到[1].說明,
                    title = 玩家抽到[1].卡牌名稱,
                    //設定圖片
                    thumbnailImageUrl = new Uri(玩家抽到[1].圖片網址),
                    actions           = actions2 //設定回覆動作
                };

                var Column3 = new isRock.LineBot.Column
                {
                    text  = "等級: " + 玩家抽到[2].等級 + " 說明:" + 玩家抽到[2].說明,
                    title = 玩家抽到[2].卡牌名稱,
                    //設定圖片
                    thumbnailImageUrl = new Uri(玩家抽到[2].圖片網址),
                    actions           = actions3 //設定回覆動作
                };
                //建立CarouselTemplate
                var CarouselTemplate = new isRock.LineBot.CarouselTemplate();

                CarouselTemplate.columns.Add(Column1);
                CarouselTemplate.columns.Add(Column2);
                CarouselTemplate.columns.Add(Column3);

                儲存暫存(玩家, 怪物, 怪物抽到編號);

                return(CarouselTemplate);
            }
Exemple #4
0
        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());
            }
        }