Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Why do the following two pieces of code execute differently? #332

Closed
1046102779 opened this issue Dec 12, 2022 · 3 comments
Closed

Why do the following two pieces of code execute differently? #332

1046102779 opened this issue Dec 12, 2022 · 3 comments
Assignees

Comments

@1046102779
Copy link

1046102779 commented Dec 12, 2022

https://github.com/line/line-bot-sdk-go/blob/master/examples/echo_bot/server.go#L48

for _, event := range events {
	if event.Type == linebot.EventTypeMessage {
		switch message := event.Message.(type) {
		case *linebot.TextMessage:
			if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(message.Text)).Do(); err != nil {
				log.Print(err)
			}
		case *linebot.StickerMessage:
			replyMessage := fmt.Sprintf(
				"sticker id is %s, stickerResourceType is %s", message.StickerID, message.StickerResourceType)
			if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(replyMessage)).Do(); err != nil {
				log.Print(err)
			}
		}
	}
}

why not this?

func handleMessage(msg linebot.Message) error {
        if msg == nil {
                return nil
        }
        switch msg.Type() {
        case linebot.MessageTypeText:
                textMsg, _ := msg.(*linebot.TextMessage)
                handleTextMsg(textMsg)
        default:
                fmt.Printf("unknown msg type: %v\n", msg.Type())
        }

        return nil
}

event.Message.(type) is right, and msg.Type() is not right and the result is a empty value, why?

@sugyan @mokejp @kkdai cc

@1046102779
Copy link
Author

demo:

func handleTextMsg(msg *linebot.TextMessage) error {
        fmt.Printf("receive line user msg: \n")
        fmt.Printf("id: %s\n", msg.ID)
        fmt.Printf("type: %s\n", msg.Type())
        fmt.Printf("text: %s\n", msg.Text)
        if msg.Mention != nil {
                fmt.Printf("receiver: %+v\n", msg.Mention.Mentionees)
        }

        return nil
}

output:

receive line user msg: 
id: 17282142297924
type: 
text: hello
-----------------------------------
receive line user msg: 
id: 17282157358950
type: 
text: hi
-----------------------------------
receive line user msg: 
id: 17282157766222
type: 
text: no
-----------------------------------
receive line user msg: 
id: 17282158604392
type: 
text: say hi

I found that text message type is a empty value.

phone:
image

@1046102779
Copy link
Author

1046102779 commented Dec 13, 2022

I found it.

{
    "destination":"U68ffccf515e89f74fbbbb1e94e2f3da7",
    "events":[
        {
            "type":"message",
            "message":{
                "type":"text",
                "id":"17285133560396",
                "text":"你好"
            },
            "webhookEventId":"01GM4JXB7JGA0R6SSH6PYM26ZM",
            "deliveryContext":{
                "isRedelivery":true
            },
            "timestamp":1670896331675,
            "source":{
                "type":"user",
                "userId":"Ubfebb3c90048678f5bd092386a5a9b40"
            },
            "replyToken":"5d6143f945264ebfbfe4e7f2a3b43f39",
            "mode":"active"
        }
    ]
}

The UnmarshalJSON method of event hasn't assign messageType. line:426

Copy link
Contributor

This issue was closed because it has been inactive for 14 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 16, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants