From 67a131d9354f454f922f06f592922e48ee233b72 Mon Sep 17 00:00:00 2001 From: Evan Lin Date: Tue, 30 Jan 2024 14:47:09 +0800 Subject: [PATCH] feat: add new functionality for sending flex messages with a new image - Add constant `LogoImageUrl` with value `"https://raw.githubusercontent.com/kkdai/linebot-smart-namecard/main/img/logo.jpeg"` - Add function `getNewSingleCard` to send a new flex message to the LINE server - Modify `getSingleCard` function to append a new single card to the `cards` slice Signed-off-by: Evan Lin --- flex.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/flex.go b/flex.go index 61996f7..1b45803 100644 --- a/flex.go +++ b/flex.go @@ -1,7 +1,14 @@ package main -import "github.com/line/line-bot-sdk-go/v8/linebot/messaging_api" +import ( + "net/url" + "github.com/line/line-bot-sdk-go/v8/linebot/messaging_api" +) + +const LogoImageUrl = "https://raw.githubusercontent.com/kkdai/linebot-smart-namecard/main/img/logo.jpeg" + +// getSingleCard: Send flex message to LINE server. func getSingleCard(card Person) messaging_api.FlexBubble { return messaging_api.FlexBubble{ @@ -31,7 +38,7 @@ func getSingleCard(card Person) messaging_api.FlexBubble { AspectMode: "cover", AspectRatio: "1:1", Gravity: "center", - Url: "https://raw.githubusercontent.com/kkdai/linebot-smart-namecard/main/img/logo.jpeg", + Url: LogoImageUrl, }, }, }, @@ -103,7 +110,7 @@ func getSingleCard(card Person) messaging_api.FlexBubble { func SendFlexMsg(replyToken string, people []Person, msg string) error { var cards []messaging_api.FlexBubble for _, card := range people { - cards = append(cards, getSingleCard(card)) + cards = append(cards, getNewSingleCard(card)) } contents := &messaging_api.FlexCarousel{ @@ -128,3 +135,84 @@ func SendFlexMsg(replyToken string, people []Person, msg string) error { } return nil } + +// getNewSingleCard: Send flex message to LINE server. +func getNewSingleCard(card Person) messaging_api.FlexBubble { + // Get URL encode for company name and address + companyEncode := url.QueryEscape(card.Company) + addressEncode := url.QueryEscape(card.Address) + + return messaging_api.FlexBubble{ + Size: messaging_api.FlexBubbleSIZE_GIGA, + Body: &messaging_api.FlexBox{ + Layout: messaging_api.FlexBoxLAYOUT_HORIZONTAL, + Spacing: "md", + Contents: []messaging_api.FlexComponentInterface{ + &messaging_api.FlexImage{ + AspectMode: "cover", + AspectRatio: "1:1", + Flex: 1, + Size: "full", + Url: LogoImageUrl, + }, + &messaging_api.FlexBox{ + Flex: 4, + Layout: messaging_api.FlexBoxLAYOUT_VERTICAL, + Contents: []messaging_api.FlexComponentInterface{ + &messaging_api.FlexText{ + Align: "end", + Size: "xxl", + Text: card.Name, + Weight: "bold", + }, + &messaging_api.FlexText{ + Align: "end", + Size: "sm", + Text: card.Title, + }, + &messaging_api.FlexText{ + Align: "end", + Margin: "xxl", + Size: "lg", + Text: card.Company, + Weight: "bold", + Action: &messaging_api.UriAction{ + Uri: "https://www.google.com/maps/search/?api=1&query=" + companyEncode + "&openExternalBrowser=1", + }, + }, + &messaging_api.FlexText{ + Align: "end", + Size: "sm", + Text: card.Address, + Action: &messaging_api.UriAction{ + Uri: "https://www.google.com/maps/search/?api=1&query=" + addressEncode + "&openExternalBrowser=1", + }, + }, + &messaging_api.FlexText{ + Align: "end", + Margin: "xxl", + Text: card.PhoneNumber, + Action: &messaging_api.UriAction{ + Uri: "tel:" + card.PhoneNumber, + }, + }, + &messaging_api.FlexText{ + Align: "end", + Text: card.Email, + Action: &messaging_api.UriAction{ + Uri: "mailto:" + card.Email, + }, + }, + &messaging_api.FlexText{ + Align: "end", + Text: "更多資訊", + Action: &messaging_api.UriAction{ + Uri: "https://github.com/kkdai/linebot-smart-namecard", + }, + }, + }, + }, + }, + }, + } +}