C# LINEBOT 綜合小專案( 需加入參考 LINEBOTSDK )

C# LINEBOT 綜合小專案

資料來源 :

http://studyhost.blogspot.com/2018/01/index-clinebot.html

C# LINEBOT 綜合小專案

資料來源 :

http://studyhost.blogspot.com/2018/01/index-clinebot.html

程式內容 : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using isRock.LineBot;

namespace WebApplication1_LINEBOT.Controllers
{
    public class LineChatController : ApiController
    {
        public string ChannelAccessToken = @"BJp2LE+labjH....................lFU="; //TOKEN

        [HttpPost]
        public IHttpActionResult POST()
        {
            try
            {

                //建立Bot instance
                isRock.LineBot.Bot bot = new isRock.LineBot.Bot(ChannelAccessToken);  //傳入Channel access token

                //取得 http Post RawData(should be JSO
                string postData = Request.Content.ReadAsStringAsync().Result;
                //剖析JSON
                var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData);
                //user這一台裝置LINE,的身分證字號
                var UserID = isRock.LineBot.Utility.Parsing(postData).events[0].source.userId;
                //user資訊(ex:帳號)
                var userInfo = bot.GetUserInfo(ReceivedMessage.events.FirstOrDefault().source.userId);

                //user insert
                var UserSays = ReceivedMessage.events[0].message.text;
                //?
                var ReplyToken = ReceivedMessage.events[0].replyToken;

                //依照用戶說的特定關鍵字來回應
                switch (UserSays.ToLower())
                {
                    case "貼圖":
                        //回覆貼圖
                        bot.ReplyMessage(ReplyToken, 1, 11);
                        // 參考貼圖網址 ==> hhttps://devdocs.line.me/files/sticker_list.pdf
                        break;
                    case "圖片":
                        //回覆圖片
                        bot.ReplyMessage(ReplyToken, new Uri("https://external-tpe1-1.xx.fbcdn.net/safe_image.php?d=AQAc_b9uFr5VR0cg&w=476&h=249&url=fbstaging%3A%2F%2Fgraph.facebook.com%2Fstaging_resources%2FMDExMzQ5NDA3ODM1MDk4Nzg2OjI2MDY5NTAzMQ%3D%3D&cfs=1&upscale=1&_nc_hash=AQDJXtISk8IViKtg"));
                        break;
                    case "hi":
                        //回覆訊息
                        string Message = "hi," + userInfo.displayName + " ~ today will be a amazing day for you. " + UserSays;
                        //回覆用戶
                        bot.ReplyMessage(ReplyToken, Message);
                        break;
                    case "carouseltemplate":
                        //建立actions,作為ButtonTemplate的用戶回覆行為
                        var actions1 = new List<isRock.LineBot.TemplateActionBase>();
                        actions1.Add(new isRock.LineBot.MessageAction() { label = "標題1-文字回覆", text = "回覆文字" });
                        actions1.Add(new isRock.LineBot.UriAction() { label = "標題1-開啟URL", uri = new Uri("http://www.google.com") });
                        actions1.Add(new isRock.LineBot.PostbackAction() { label = "標題1-發生postack", data = "abc=aaa&def=111" });

                        var actions2 = new List<isRock.LineBot.TemplateActionBase>();
                        actions2.Add(new isRock.LineBot.MessageAction() { label = "標題2-文字回覆", text = "回覆文字" });
                        actions2.Add(new isRock.LineBot.UriAction() { label = "標題2-開啟URL", uri = new Uri("http://www.google.com") });
                        actions2.Add(new isRock.LineBot.PostbackAction() { label = "標題2-發生postack", data = "abc=aaa&def=111" });

                        var actions3 = new List<isRock.LineBot.TemplateActionBase>();
                        actions3.Add(new isRock.LineBot.MessageAction() { label = "標題3-文字回覆", text = "回覆文字" });
                        actions3.Add(new isRock.LineBot.UriAction() { label = "標題3-開啟URL", uri = new Uri("http://www.google.com") });
                        actions3.Add(new isRock.LineBot.PostbackAction() { label = "標題3-發生postack", data = "abc=aaa&def=111" });

                        var actions4 = new List<isRock.LineBot.TemplateActionBase>();
                        actions4.Add(new isRock.LineBot.MessageAction() { label = "標題4-文字回覆", text = "回覆文字" });
                        actions4.Add(new isRock.LineBot.UriAction() { label = "標題4-開啟URL", uri = new Uri("http://www.google.com") });
                        actions4.Add(new isRock.LineBot.PostbackAction() { label = "標題4-發生postack", data = "abc=aaa&def=111" });


                        List<Column> c = new List<Column>();

                        c.Add(new Column() { title = "標題1", text = "ABC...敘述...", thumbnailImageUrl = new Uri("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcShM4OsAL9Y7-4iGKI0mvP2WsQ3gQeEApOwnjsXdUs90dQe2ph8"), actions = actions1 });
                        c.Add(new Column() { title = "標題2", text = "ABC...敘述...", thumbnailImageUrl = new Uri("https://media.istockphoto.com/photos/book-heart-picture-id503708758?k=6&m=503708758&s=612x612&w=0&h=5_lHyNzJazKgkoyIlDkbMS4s1eFANPqrsqQGY6t8Jwg="), actions = actions2 });
                        c.Add(new Column() { title = "標題3", text = "ABC...敘述...", thumbnailImageUrl = new Uri("https://s3.eu-west-2.amazonaws.com/littlewriter-production/stories/4aKYJcQvuD.jpeg"), actions = actions3 });
                        c.Add(new Column() { title = "標題4", text = "ABC...敘述...", thumbnailImageUrl = new Uri("https://github.com/apple-touch-icon.png/"), actions = actions4 });

                        var CarouselTemplate = new isRock.LineBot.CarouselTemplate()
                        {
                            columns = c
                        };
                        //發送
                        //bot.PushMessage(UserID, CarouselTemplate);
                        bot.ReplyMessage(ChannelAccessToken , new isRock.LineBot.TemplateMessage(CarouselTemplate));
                        break;
                    case "buttonstemplate":
                        //建立actions,作為ButtonTemplate的用戶回覆行為
                        var actions = new List<isRock.LineBot.TemplateActionBase>();
                        actions.Add(new isRock.LineBot.MessageAction() { label = "標題-文字回覆", text = "回覆文字" });
                        actions.Add(new isRock.LineBot.MessageAction() { label = "標題-文字回覆", text = "回覆文字" });
                        actions.Add(new isRock.LineBot.UriAction() { label = "標題-開啟URL", uri = new Uri("http://www.google.com") });

                        //單一Button Template Message
                        var ButtonTemplate = new isRock.LineBot.ButtonsTemplate()
                        {
                            text = "ButtonsTemplate文字訊息",
                            title = "ButtonsTemplate標題",
                            //設定圖片
                            thumbnailImageUrl = new Uri("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcShM4OsAL9Y7-4iGKI0mvP2WsQ3gQeEApOwnjsXdUs90dQe2ph8"),
                            actions = actions //設定回覆動作
                        };

                        //發送
                        bot.PushMessage(UserID, ButtonTemplate);
                        //bot.ReplyMessage(ChannelAccessToken , new isRock.LineBot.TemplateMessage(ButtonTemplate));
                        break;
                }

                return Ok();
            }
            catch (Exception e)
            {
                string err_msg = e.Message;
                return Ok();
            }
        }
    }
}

執行結果