Exemplo n.º 1
0
        private async Task ResetRichMenuAsync(string replyToken, string userId)
        {
            var richMenuId = await Client.GetRichMenuOfUser(userId);

            await Client.UnlinkRichMenuFromUser(userId);

            // This is for testing.
            // Generally, in my opinion, you should reuse rich menus.
            await Client.DeleteRichMenu(richMenuId);

            await Client.ReplyTextAsync(replyToken, $"Deleted your rich menu.\nrichMenuId: {richMenuId}");
        }
        public async Task HandleTextMessage()
        {
            var     textMessage  = JsonConvert.DeserializeObject <TextMessage>(lineEvent.Message.ToString());
            Message replyMessage = null;

            if (textMessage.Text.ToLower() == "buttons")
            {
                List <TemplateAction> actions = new List <TemplateAction>();
                actions.Add(new MessageTemplateAction("Message Label", "sample data"));
                actions.Add(new PostbackTemplateAction("Postback Label", "sample data", "sample data"));
                actions.Add(new UriTemplateAction("Uri Label", "https://github.com/kenakamu"));
                ButtonsTemplate buttonsTemplate = new ButtonsTemplate("https://github.com/apple-touch-icon.png", "Sample Title", "Sample Text", actions);

                replyMessage = new TemplateMessage("Buttons", buttonsTemplate);
            }
            else if (textMessage.Text.ToLower() == "confirm")
            {
                List <TemplateAction> actions = new List <TemplateAction>();
                actions.Add(new MessageTemplateAction("Yes", "yes"));
                actions.Add(new MessageTemplateAction("No", "no"));
                ConfirmTemplate confirmTemplate = new ConfirmTemplate("Confirm Test", actions);
                replyMessage = new TemplateMessage("Confirm", confirmTemplate);
            }
            else if (textMessage.Text.ToLower() == "carousel")
            {
                List <TemplateColumn> columns  = new List <TemplateColumn>();
                List <TemplateAction> actions1 = new List <TemplateAction>();
                List <TemplateAction> actions2 = new List <TemplateAction>();

                // Add actions.
                actions1.Add(new MessageTemplateAction("Message Label", "sample data"));
                actions1.Add(new PostbackTemplateAction("Postback Label", "sample data", "sample data"));
                actions1.Add(new UriTemplateAction("Uri Label", "https://github.com/kenakamu"));

                // Add datetime picker actions
                actions2.Add(new DatetimePickerTemplateAction("DateTime Picker", "DateTime", DatetimePickerMode.Datetime, "2017-07-21T13:00"));
                actions2.Add(new DatetimePickerTemplateAction("Date Picker", "Date", DatetimePickerMode.Date, "2017-07-21"));
                actions2.Add(new DatetimePickerTemplateAction("Time Picker", "Time", DatetimePickerMode.Time, "13:00"));

                columns.Add(new TemplateColumn()
                {
                    Title = "Casousel 1 Title", Text = "Casousel 1 Text", ThumbnailImageUrl = "https://github.com/apple-touch-icon.png", Actions = actions1
                });
                columns.Add(new TemplateColumn()
                {
                    Title = "Casousel 2 Title", Text = "Casousel 2 Text", ThumbnailImageUrl = "https://github.com/apple-touch-icon.png", Actions = actions2
                });
                CarouselTemplate carouselTemplate = new CarouselTemplate(columns);
                replyMessage = new TemplateMessage("Carousel", carouselTemplate);
            }
            else if (textMessage.Text.ToLower() == "imagecarousel")
            {
                List <ImageColumn> columns = new List <ImageColumn>();
                UriTemplateAction  action  = new UriTemplateAction("Uri Label", "https://github.com/kenakamu");

                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));

                ImageCarouselTemplate carouselTemplate = new ImageCarouselTemplate(columns);

                replyMessage = new TemplateMessage("Carousel", carouselTemplate);
            }
            else if (textMessage.Text.ToLower() == "imagemap")
            {
                var url      = HttpContext.Current.Request.Url;
                var imageUrl = $"{url.Scheme}://{url.Host}:{url.Port}/images/githubicon";
                List <ImageMapAction> actions = new List <ImageMapAction>();
                actions.Add(new UriImageMapAction("http://github.com", new ImageMapArea(0, 0, 520, 1040)));
                actions.Add(new MessageImageMapAction("I love LINE!", new ImageMapArea(520, 0, 520, 1040)));
                replyMessage = new ImageMapMessage(imageUrl, "GitHub", new BaseSize(1040, 1040), actions);
            }
            else if (textMessage.Text.ToLower() == "addrichmenu")
            {
                // Create Rich Menu
                RichMenu richMenu = new RichMenu()
                {
                    Size        = new RichMenuSize(1686),
                    Selected    = false,
                    Name        = "nice richmenu",
                    ChatBarText = "touch me",
                    Areas       = new List <RichMenuArea>()
                    {
                        new RichMenuArea()
                        {
                            Action = new PostbackTemplateAction("action=buy&itemid=123"),
                            Bounds = new RichMenuBounds(0, 0, 2500, 1686)
                        }
                    }
                };


                var richMenuId = await lineClient.CreateRichMenu(richMenu);

                var image = new MemoryStream(File.ReadAllBytes(HttpContext.Current.Server.MapPath(@"~\Images\richmenu.PNG")));
                // Upload Image
                await lineClient.UploadRichMenuImage(richMenuId, image);

                // Link to user
                await lineClient.LinkRichMenuToUser(lineEvent.Source.UserId, richMenuId);
            }
            else if (textMessage.Text.ToLower() == "deleterichmenu")
            {
                // Get Rich Menu for the user
                var richMenuId = await lineClient.GetRichMenuIdForUser(lineEvent.Source.UserId);

                await lineClient.UnlinkRichMenuToUser(lineEvent.Source.UserId);

                await lineClient.DeleteRichMenu(richMenuId);
            }
            else if (textMessage.Text.ToLower() == "deleteallrichmenu")
            {
                // Get Rich Menu for the user
                var richMenuList = await lineClient.GetRichMenuList();

                foreach (var richMenu in richMenuList)
                {
                    await lineClient.DeleteRichMenu(richMenu["richMenuId"].ToString());
                }
            }
            else
            {
                replyMessage = new TextMessage(textMessage.Text);
            }
            await Reply(replyMessage);
        }
        public async Task HandleTextMessage()
        {
            var     textMessage  = JsonConvert.DeserializeObject <TextMessage>(lineEvent.Message.ToString());
            Message replyMessage = null;

            if (textMessage.Text.ToLower() == "ボタン")
            {
                List <TemplateAction> actions = new List <TemplateAction>();
                actions.Add(new MessageTemplateAction("Message Label", "sample data"));
                actions.Add(new PostbackTemplateAction("Postback Label", "sample data", "sample data"));
                actions.Add(new UriTemplateAction("Uri Label", "https://github.com/kenakamu"));
                ButtonsTemplate buttonsTemplate = new ButtonsTemplate("https://github.com/apple-touch-icon.png", "Sample Title", "Sample Text", actions);

                replyMessage = new TemplateMessage("Buttons", buttonsTemplate);
            }
            else if (textMessage.Text.ToLower() == "こんにちは")//confirm
            {
                List <TemplateAction> actions = new List <TemplateAction>();
                actions.Add(new MessageTemplateAction("はい", "yes"));
                actions.Add(new MessageTemplateAction("いいえ", "no"));
                ConfirmTemplate confirmTemplate = new ConfirmTemplate("こんにちは!テニスコートを予約しますか?", actions);
                replyMessage = new TemplateMessage("Confirm", confirmTemplate);

                //if (confirmTemplate.ToString() == "yes")
                //{
                //    confirmFlg = true;
                //    //replyMessage = new TextMessage("一覧を表示します♪");
                //}
                //else
                //{
                //    confirmFlg = false;
                //    //replyMessage = new TextMessage("しねーのかよ(# ゚Д゚)");
                //}
            }
            else if (textMessage.Text.ToLower() == "テニスコート予約したい" || textMessage.Text.ToLower() == "yes")
            {
                //replyMessage = new TextMessage("どこのコートを予約しますか?");//最初にメッセを出したかったが動作しない
                string aobayamaimg             = @"~\Images\aobayama.PNG";//青葉山庭球場の画像パスあああああ
                string tyoumeigaokaimg         = @"~\Images\tyoumeigaoka.PNG";
                List <TemplateColumn> columns  = new List <TemplateColumn>();
                List <TemplateAction> actions1 = new List <TemplateAction>();
                List <TemplateAction> actions2 = new List <TemplateAction>();

                actions1.Add(new UriTemplateAction("HP", "http://www.spf-sendai.jp/sisetsu/moshikomi.html"));
                actions1.Add(new UriTemplateAction("場所", "https://www.google.com/maps?ll=38.31376,140.83363&z=12&t=m&hl=ja&gl=US&mapclient=embed&cid=9042741886630709754"));
                actions1.Add(new UriTemplateAction("予約", "https://www.cm2.epss.jp/sendai/web/view/user/homeIndex.html"));
                columns.Add(new TemplateColumn()
                {
                    Title = "長命ヶ丘庭球場", Text = "宮城県仙台市泉区長命ケ丘五丁目14", ThumbnailImageUrl = "https://github.com/apple-touch-icon.png", Actions = actions1
                });

                actions2.Add(new UriTemplateAction("HP", "http://www.spf-sendai.jp/sisetsu/moshikomi.html"));
                actions2.Add(new UriTemplateAction("場所", "https://www.google.com/maps?ll=38.31376,140.83363&z=12&t=m&hl=ja&gl=US&mapclient=embed&cid=9042741886630709754"));
                actions2.Add(new UriTemplateAction("予約", "https://www.cm2.epss.jp/sendai/web/view/user/homeIndex.html"));
                columns.Add(new TemplateColumn()
                {
                    Title = "青葉山庭球場", Text = "宮城県仙台市青葉区花壇1-1", ThumbnailImageUrl = "https://github.com/apple-touch-icon.png", Actions = actions2
                });

                //// Add actions.
                //actions1.Add(new MessageTemplateAction("Message Label", "sample data"));
                //actions1.Add(new PostbackTemplateAction("Postback Label", "sample data", "sample data"));
                //actions1.Add(new UriTemplateAction("Uri Label", "https://github.com/kenakamu"));

                //// Add datetime picker actions
                //actions2.Add(new DatetimePickerTemplateAction("DateTime Picker", "DateTime", DatetimePickerMode.Datetime, "2017-07-21T13:00"));
                //actions2.Add(new DatetimePickerTemplateAction("Date Picker", "Date", DatetimePickerMode.Date, "2017-07-21"));
                //actions2.Add(new DatetimePickerTemplateAction("Time Picker", "Time", DatetimePickerMode.Time, "13:00"));

                //columns.Add(new TemplateColumn() { Title = "Casousel 1 Title", Text = "Casousel 1 Text", ThumbnailImageUrl = "https://github.com/apple-touch-icon.png", Actions = actions1 });
                //columns.Add(new TemplateColumn() { Title = "Casousel 2 Title", Text = "Casousel 2 Text", ThumbnailImageUrl = "https://github.com/apple-touch-icon.png", Actions = actions2 });

                CarouselTemplate carouselTemplate = new CarouselTemplate(columns);
                replyMessage = new TemplateMessage("Carousel", carouselTemplate);
            }
            else if (textMessage.Text.ToLower() == "イメージカルーセル")//imagecarousel
            {
                List <ImageColumn> columns = new List <ImageColumn>();
                UriTemplateAction  action  = new UriTemplateAction("Uri Label", "https://github.com/kenakamu");

                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));
                columns.Add(new ImageColumn("https://github.com/apple-touch-icon.png", action));

                ImageCarouselTemplate carouselTemplate = new ImageCarouselTemplate(columns);

                replyMessage = new TemplateMessage("Carousel", carouselTemplate);
            }
            else if (textMessage.Text.ToLower() == "イメージマップ")//imagemap
            {
                var url      = HttpContext.Current.Request.Url;
                var imageUrl = $"{url.Scheme}://{url.Host}:{url.Port}/images/githubicon";
                List <ImageMapAction> actions = new List <ImageMapAction>();
                actions.Add(new UriImageMapAction("http://github.com", new ImageMapArea(0, 0, 520, 1040)));
                actions.Add(new MessageImageMapAction("I love LINE!", new ImageMapArea(520, 0, 520, 1040)));
                replyMessage = new ImageMapMessage(imageUrl, "GitHub", new BaseSize(1040, 1040), actions);
            }
            else if (textMessage.Text.ToLower() == "アッドリッチメニュー")//addrichmenu
            {
                // Create Rich Menu
                RichMenu richMenu = new RichMenu()
                {
                    Size        = new RichMenuSize(1686),
                    Selected    = false,
                    Name        = "nice richmenu",
                    ChatBarText = "touch me",
                    Areas       = new List <RichMenuArea>()
                    {
                        new RichMenuArea()
                        {
                            Action = new PostbackTemplateAction("action=buy&itemid=123"),
                            Bounds = new RichMenuBounds(0, 0, 2500, 1686)
                        }
                    }
                };


                var richMenuId = await lineClient.CreateRichMenu(richMenu);

                var image = new MemoryStream(File.ReadAllBytes(HttpContext.Current.Server.MapPath(@"~\Images\richmenu.PNG")));
                // Upload Image
                await lineClient.UploadRichMenuImage(richMenuId, image);

                // Link to user
                await lineClient.LinkRichMenuToUser(lineEvent.Source.UserId, richMenuId);
            }
            else if (textMessage.Text.ToLower() == "デリートリッチメニュー")//deleterichmenu
            {
                // Get Rich Menu for the user
                var richMenuId = await lineClient.GetRichMenuIdForUser(lineEvent.Source.UserId);

                await lineClient.UnlinkRichMenuToUser(lineEvent.Source.UserId);

                await lineClient.DeleteRichMenu(richMenuId);
            }
            else if (textMessage.Text.ToLower() == "デリートオールリッチメニュー")//deleteallrichmenu
            {
                // Get Rich Menu for the user
                var richMenuList = await lineClient.GetRichMenuList();

                foreach (var richMenu in richMenuList)
                {
                    await lineClient.DeleteRichMenu(richMenu["richMenuId"].ToString());
                }
            }
            else//どの条件にも合わない物はオウム返し
            {
                //replyMessage = new TextMessage(textMessage.Text);
            }
            await Reply(replyMessage);
        }
Exemplo n.º 4
0
        // 事件:文字
        public async Task HandleTextMessage()
        {
            var     textMessage            = JsonConvert.DeserializeObject <TextMessage>(lineEvent.Message.ToString());
            Message replyMessage           = null;
            List <Models.Message> messages = db.Messages.Where(x => x.KeyWord.Contains(textMessage.Text) && x.Event == 5).ToList();

            if (messages != null)
            {
                Dictionary <int, int> matchMessages    = null;
                List <int>            messageIds       = null;
                List <Models.Message> frequentMessages = null;

                foreach (var message in messages)
                {
                    string[] keywords = message.KeyWord.Split(',');
                    int      count    = 0;
                    foreach (var keyword in keywords)
                    {
                        if (textMessage.Text.Contains(keyword))
                        {
                            matchMessages.Add(message.MessageId, count++);
                        }
                    }
                }
                // 排序
                var sortMessages = from objDic in matchMessages orderby objDic.Value descending select objDic;
                foreach (KeyValuePair <int, int> kvp in sortMessages)
                {
                    messageIds.Add(kvp.Key);
                }
                frequentMessages = db.Messages.Where(x => messageIds.Contains(x.MessageId)).ToList();

                // 文字
                if (frequentMessages.First().Type == 1)
                {
                    replyMessage = new TextMessage(frequentMessages.First().Text);
                }
                // 貼圖
                else if (frequentMessages.First().Type == 2)
                {
                    // https://devdocs.line.me/files/sticker_list.pdf
                    var stickerMessage = JsonConvert.DeserializeObject <StickerMessage>(lineEvent.Message.ToString());
                    replyMessage = new StickerMessage(frequentMessages.First().STKId, frequentMessages.First().STKPKGId);
                }
                // 圖片
                else if (frequentMessages.First().Type == 3)
                {
                    replyMessage = new ImageMessage(frequentMessages.First().ImageUrl, frequentMessages.First().Url);
                }
                // 影片
                else if (frequentMessages.First().Type == 4)
                {
                    replyMessage = new ImageMessage(frequentMessages.First().ImageUrl, frequentMessages.First().Url);
                }
                // 聲音
                else if (frequentMessages.First().Type == 5)
                {
                    replyMessage = new ImageMessage(frequentMessages.First().ImageUrl, frequentMessages.First().Url);
                }
                // 位置
                else if (frequentMessages.First().Type == 6)
                {
                    replyMessage = new LocationMessage(
                        frequentMessages.First().Title,
                        frequentMessages.First().Address,
                        frequentMessages.First().Latitude,
                        frequentMessages.First().Longitude);
                }
                // 圖片地圖
                else if (frequentMessages.First().Type == 7)
                {
                    // 待研究
                    var url      = HttpContext.Current.Request.Url;
                    var imageUrl = $"{url.Scheme}://{url.Host}:{url.Port}/images/githubicon";
                    List <ImageMapAction> actions = new List <ImageMapAction>();
                    actions.Add(new UriImageMapAction("http://github.com", new ImageMapArea(0, 0, 520, 1040)));
                    actions.Add(new MessageImageMapAction("I love LINE!", new ImageMapArea(520, 0, 520, 1040)));
                    replyMessage = new ImageMapMessage(imageUrl, "GitHub", new BaseSize(1040, 1040), actions);
                }
                // 模板:按鈕
                else if (frequentMessages.First().Type == 8)
                {
                    List <TemplateAction> actions = new List <TemplateAction>();
                    if (frequentMessages.First().Msg != "" || frequentMessages.First().Msg != null)
                    {
                        actions.Add(new MessageTemplateAction("傳送訊息", frequentMessages.First().Msg));
                    }
                    if (frequentMessages.First().PostBack != "" || frequentMessages.First().PostBack != null)
                    {
                        actions.Add(new PostbackTemplateAction("執行動作", frequentMessages.First().PostBack));
                    }
                    if (frequentMessages.First().Url != "" || frequentMessages.First().Url != null)
                    {
                        actions.Add(new UriTemplateAction("查看全部", frequentMessages.First().Url));
                    }
                    ButtonsTemplate buttonsTemplate = new ButtonsTemplate(frequentMessages.First().ImageUrl, frequentMessages.First().Title, frequentMessages.First().Text, actions);

                    replyMessage = new TemplateMessage("Buttons", buttonsTemplate);
                }
                // 模板:確認
                else if (frequentMessages.First().Type == 9)
                {
                    List <TemplateAction> actions = new List <TemplateAction>();
                    actions.Add(new MessageTemplateAction("是", frequentMessages.First().Yes));
                    actions.Add(new MessageTemplateAction("否", frequentMessages.First().No));
                    ConfirmTemplate confirmTemplate = new ConfirmTemplate(frequentMessages.First().Title, actions);
                    replyMessage = new TemplateMessage("Confirm", confirmTemplate);
                }
                // 模板:輪播
                else if (frequentMessages.First().Type == 10)
                {
                    List <TemplateColumn> columns = new List <TemplateColumn>();
                    List <TemplateAction> actions = new List <TemplateAction>();

                    foreach (var message in frequentMessages.Where(x => x.Type == 10))
                    {
                        actions = null;
                        if (message.Msg != "" || message.Msg != null)
                        {
                            actions.Add(new MessageTemplateAction("傳送訊息", message.Msg));
                        }
                        if (message.PostBack != "" || message.PostBack != null)
                        {
                            actions.Add(new PostbackTemplateAction("執行動作", message.PostBack));
                        }
                        if (message.Url != "" || message.Url != null)
                        {
                            actions.Add(new UriTemplateAction("查看全部", message.Url));
                        }

                        columns.Add(new TemplateColumn()
                        {
                            Title = message.Title, Text = message.Text, ThumbnailImageUrl = message.ImageUrl, Actions = actions
                        });
                    }

                    CarouselTemplate carouselTemplate = new CarouselTemplate(columns);
                    replyMessage = new TemplateMessage("Carousel", carouselTemplate);
                }
                // 模板:圖片輪播
                else if (frequentMessages.First().Type == 11)
                {
                    List <ImageColumn> columns = new List <ImageColumn>();

                    foreach (var message in frequentMessages.Where(x => x.Type == 11))
                    {
                        UriTemplateAction action = new UriTemplateAction("查看全部", message.Url);
                        columns.Add(new ImageColumn(message.ImageUrl, action));
                    }

                    ImageCarouselTemplate carouselTemplate = new ImageCarouselTemplate(columns);
                    replyMessage = new TemplateMessage("Carousel", carouselTemplate);
                }
                // 模板:新增豐富菜單
                else if (frequentMessages.First().Type == 12)
                {
                    // Create Rich Menu
                    RichMenu richMenu = new RichMenu()
                    {
                        Size        = new RichMenuSize(1686),
                        Selected    = false,
                        Name        = frequentMessages.First().Title,
                        ChatBarText = "touch me",
                        Areas       = new List <RichMenuArea>()
                        {
                            new RichMenuArea()
                            {
                                Action = new PostbackTemplateAction("action=buy&itemid=123"),
                                Bounds = new RichMenuBounds(0, 0, 2500, 1686)
                            }
                        }
                    };


                    var richMenuId = await lineClient.CreateRichMenu(richMenu);

                    var image = new MemoryStream(File.ReadAllBytes(HttpContext.Current.Server.MapPath(@"~\Images\richmenu.PNG")));
                    // Upload Image
                    await lineClient.UploadRichMenuImage(richMenuId, image);

                    // Link to user
                    await lineClient.LinkRichMenuToUser(lineEvent.Source.UserId, richMenuId);
                }
                // 模板:刪除豐富菜單
                else
                {
                    var richMenuId = await lineClient.GetRichMenuIdForUser(lineEvent.Source.UserId);

                    await lineClient.UnlinkRichMenuToUser(lineEvent.Source.UserId);

                    await lineClient.DeleteRichMenu(richMenuId);
                }
            }
            else
            {
                replyMessage = new TextMessage("不好意思!我不懂您的意思...");
            }
            await Reply(replyMessage);
        }