simplify message id retrieval logic.
Signed-off-by: Kai Xia <kaix+github@fastmail.com>
This commit is contained in:
parent
7ba1c43edc
commit
6c5cc53601
1 changed files with 11 additions and 10 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -176,6 +177,9 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
|
||||||
var mediaPaths []string
|
var mediaPaths []string
|
||||||
var messageID string
|
var messageID string
|
||||||
var isMentioned bool
|
var isMentioned bool
|
||||||
|
if f := reflect.Indirect(reflect.ValueOf(msgEvent.Message)).FieldByName("Id"); f.IsValid() {
|
||||||
|
messageID = f.String()
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to register a local file with the media store
|
// Helper to register a local file with the media store
|
||||||
storeMedia := func(localPath, filename, scope string) string {
|
storeMedia := func(localPath, filename, scope string) string {
|
||||||
|
|
@ -193,7 +197,6 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
|
||||||
|
|
||||||
switch msg := msgEvent.Message.(type) {
|
switch msg := msgEvent.Message.(type) {
|
||||||
case webhook.TextMessageContent:
|
case webhook.TextMessageContent:
|
||||||
messageID = msg.Id
|
|
||||||
content = msg.Text
|
content = msg.Text
|
||||||
isMentioned = c.isBotMentioned(msg)
|
isMentioned = c.isBotMentioned(msg)
|
||||||
if msg.QuoteToken != "" {
|
if msg.QuoteToken != "" {
|
||||||
|
|
@ -203,37 +206,35 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
|
||||||
content = c.stripBotMention(content, msg)
|
content = c.stripBotMention(content, msg)
|
||||||
}
|
}
|
||||||
case webhook.ImageMessageContent:
|
case webhook.ImageMessageContent:
|
||||||
messageID = msg.Id
|
localPath := c.downloadContent(messageID, "image.jpg")
|
||||||
localPath := c.downloadContent(msg.Id, "image.jpg")
|
|
||||||
if localPath != "" {
|
if localPath != "" {
|
||||||
scope := channels.BuildMediaScope("line", chatID, msg.Id)
|
scope := channels.BuildMediaScope("line", chatID, msg.Id)
|
||||||
mediaPaths = append(mediaPaths, storeMedia(localPath, "image.jpg", scope))
|
mediaPaths = append(mediaPaths, storeMedia(localPath, "image.jpg", scope))
|
||||||
content = "[image]"
|
content = "[image]"
|
||||||
}
|
}
|
||||||
case webhook.AudioMessageContent:
|
case webhook.AudioMessageContent:
|
||||||
messageID = msg.Id
|
localPath := c.downloadContent(messageID, "audio.m4a")
|
||||||
localPath := c.downloadContent(msg.Id, "audio.m4a")
|
|
||||||
if localPath != "" {
|
if localPath != "" {
|
||||||
scope := channels.BuildMediaScope("line", chatID, msg.Id)
|
scope := channels.BuildMediaScope("line", chatID, msg.Id)
|
||||||
mediaPaths = append(mediaPaths, storeMedia(localPath, "audio.m4a", scope))
|
mediaPaths = append(mediaPaths, storeMedia(localPath, "audio.m4a", scope))
|
||||||
content = "[audio]"
|
content = "[audio]"
|
||||||
}
|
}
|
||||||
case webhook.VideoMessageContent:
|
case webhook.VideoMessageContent:
|
||||||
messageID = msg.Id
|
localPath := c.downloadContent(messageID, "video.mp4")
|
||||||
localPath := c.downloadContent(msg.Id, "video.mp4")
|
|
||||||
if localPath != "" {
|
if localPath != "" {
|
||||||
scope := channels.BuildMediaScope("line", chatID, msg.Id)
|
scope := channels.BuildMediaScope("line", chatID, msg.Id)
|
||||||
mediaPaths = append(mediaPaths, storeMedia(localPath, "video.mp4", scope))
|
mediaPaths = append(mediaPaths, storeMedia(localPath, "video.mp4", scope))
|
||||||
content = "[video]"
|
content = "[video]"
|
||||||
}
|
}
|
||||||
case webhook.FileMessageContent:
|
case webhook.FileMessageContent:
|
||||||
messageID = msg.Id
|
|
||||||
content = "[file]"
|
content = "[file]"
|
||||||
case webhook.StickerMessageContent:
|
case webhook.StickerMessageContent:
|
||||||
messageID = msg.Id
|
|
||||||
content = "[sticker]"
|
content = "[sticker]"
|
||||||
default:
|
default:
|
||||||
content = fmt.Sprintf("[%s]", msgEvent.Message.GetType())
|
logger.WarnCF("line", "Ignoring unsupported message type", map[string]interface{}{
|
||||||
|
"type": msgEvent.Message.GetType(),
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(content) == "" {
|
if strings.TrimSpace(content) == "" {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue