line / line-bot-sdk-go

LINE Messaging API SDK for Go

Home Page:https://developers.line.biz/en/docs/messaging-api/overview/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please add Message.Type() method (text, image, video, flex, ...)

cjtim opened this issue · comments

I'm making LineBot webhook, after using linebot.ParseRequest
I can only get Event Type by using

event, err := linebot.ParseRequest(os.Getenv("LINE_CHANNEL_SECRET"), httpReqest)
event[0].Type

but cannot get the message type,
Please consider including it in the future update.

This is my code to check message type, may useful

// some part of code from func (e *Event) MarshalJSON()
fmt.Println(EventMessageType(event[0]))  //  text, image, ...

func EventMessageType(e *linebot.Event) linebot.MessageType {
	switch e.Message.(type) {
	case *linebot.TextMessage:
		return linebot.MessageTypeText
	case *linebot.ImageMessage:
		return linebot.MessageTypeImage
	case *linebot.VideoMessage:
		return linebot.MessageTypeVideo
	case *linebot.AudioMessage:
		return linebot.MessageTypeAudio
	case *linebot.FileMessage:
		return linebot.MessageTypeFile
	case *linebot.LocationMessage:
		return linebot.MessageTypeLocation
	case *linebot.StickerMessage:
		return linebot.MessageTypeSticker
	}
	return ""
}

2nd way (I'm using Go Fiber Frameworks)

body := &struct {
		Events []struct {
			Message struct {
				Type string `json:"type"`
			} `json:"message,omitempty"`
		} `json:"events,omitempty"`
	}{}
	c.BodyParser(&body)
	fmt.Println(body.Events[0].Message.Type)