C# LINEBOT ButtonsTemplate ( 需加入參考 LINEBOTSDK )
資料來源 :
程式碼 :
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.....ilFU="; //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);
var UserID = isRock.LineBot.Utility.Parsing(postData).events[0].source.userId;
var userInfo = bot.GetUserInfo(ReceivedMessage.events.FirstOrDefault().source.userId);
//建立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));
return Ok();
}
catch (Exception e)
{
string err_msg = e.Message;
return Ok();
}
}
}
}
呈現結果
1 | 2 | 3 | |