C# LINEBOT ButtonsTemplate ( 需加入參考 LINEBOTSDK )

C# LINEBOT ButtonsTemplate ( 需加入參考 LINEBOTSDK )

資料來源 :  http://studyhost.blogspot.com/2017/01/linebot8-template-message.html?showComment=1490674308124#c3807205767368064874

C# LINEBOT ButtonsTemplate ( 需加入參考 LINEBOTSDK )

1.建立專案 http://studyhost.blogspot.com/2016/12/linebot4-aspnetlinewebhook.html  ( 等於 ==> 實作 WebHook )

2.下載 LINEBOTSDK

1

2

 

3.程式碼

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 = @"BJ...........=";//(輸入TOKEN)

        [HttpPost]
        public IHttpActionResult POST()
        {
            try
            {
                isRock.LineBot.Bot bot;
                bot = new isRock.LineBot.Bot(ChannelAccessToken);

                //取得 http Post RawData(should be JSO
                string postData = Request.Content.ReadAsStringAsync().Result;
                //剖析JSON
                var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData);

                //////////////////////////////////////////////////////////////////////////////////////////////
                //建立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 //設定回覆動作
                };
                
                //發送
                var UserID = isRock.LineBot.Utility.Parsing(postData).events[0].source.userId;                
                bot.PushMessage(UserID, ButtonTemplate);

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

    }
}

4.結果

資料來源 :  http://studyhost.blogspot.com/2017/01/linebot8-template-message.html?showComment=1490674308124#c3807205767368064874