第12 屆iT邦幫忙鐵人賽系列文章 (Day8)

籌備婚禮期間甚至婚禮當天(暈),常常被問的就是問地點在哪了,Line 本身能直接傳送地圖資訊,可以直接引導到 Google Map 進行導航,在 Line 可以透過 LocationMessage 發送

定義 LocationMessage.cs

public class LocationMessage : IMessage
{

    public LineMessageType Type => LineMessageType.location;

    public string Title { get; set; }

    public string Address { get; set; }

    public decimal Latitude { get; set; }

    public decimal Longitude { get; set; }

}

假設我們今天辦在東方文華 (當然沒那麼有錢)

地址為 台灣台北市敦化北路 158 號

經緯度可以在 google map 看到為 @25.0557416,121.5481485

實作 WeddingLocation.cs

public async Task ReplyAsync(string replyToken)
{

    var locationName = "臺北文華東方酒店";

    var msg = new TextMessage()

    {

    Text = $"我們在 {locationName} 舉辦唷!"

    };

    var locationMessage = new LocationMessage()

    {

    Title = $"Kyle's Wedding - {locationName}",

    Address = "台灣台北市敦化北路 158 號",

    Latitude = Convert.ToDecimal(25.0557416),

    Longitude = Convert.ToDecimal(121.5481485)

    };

    await lineMessageUtility.ReplyMessageAsync(replyToken, new List<IMessage> {

    msg,

    locationMessage

    });

}

OnMessageAsync

protected virtual async Task OnMessageAsync(Event ev)
{

    if (ev.message.Type.Equals(LineMessageType.text))

    {

    // 完整比對使用者輸入的訊息,決定要回傳什麼

    var msg = ev.message.Text;

    var intents = new Dictionary<string, IReplyIntent>()

    {

    { "電子喜帖" ,new WeddingInvitation(lineMessageUtility,lineProfileUtility)},

    { "前導影片" ,new WeddingTrailer(lineMessageUtility,lineProfileUtility)},

    { "婚宴地點" ,new WeddingLocation(lineMessageUtility,lineProfileUtility)},

    { "Default" ,new DefaultIntent(lineMessageUtility,lineProfileUtility)},

    };

    var intent = intents.ContainsKey(ev.message.Text) ? intents[msg] : intents["Default"];

    await intent.ReplyAsync(ev.replyToken);

}

}

實作效果

懶人包,本次學到了什麼?