ICON Switch 介紹

可以讓使用者依據不同情境更換頭像回答,主要是在Message Object中新增加sender 物件來達成。

{
    "type": "text",
    "text": "Hello, I am Cony!!",
    "sender": {
        "name": "Cony",    // 前端會呈現:Cony from Bot名稱
        "iconUrl": "https://line.me/conyprof.png"  // 圖檔來源必須要是 https
    }
}

範例

//If user already select the sender feedback, prepare user nick name and icon here.
switch {
case strings.EqualFold(message.Text, "Brown"):
	sendr = linebot.NewSender("Brown", BrownImage)
case strings.EqualFold(message.Text, "Cony"):
	sendr = linebot.NewSender("Cony", ConyImage)
case strings.EqualFold(message.Text, "Sally"):
	sendr = linebot.NewSender("Sally", SallyImage)
default:
	//User input other than our provide range, notify user by quick reply.
	if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage("Please select following LINE Friends to reply you: Brown, Cony and Sally.")).Do(); err != nil {
		log.Print(err)
	}
}
if sendr != nil {
	//Send message with switched sender.
	if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage("Hi, this is "+message.Text+", Nice to meet you.").WithSender(sendr)).Do(); err != nil {
		log.Print(err)
	}
}

參考資料