address PR comments.

Signed-off-by: Kai Xia <kaix+github@fastmail.com>
This commit is contained in:
Kai Xia 2026-03-02 22:24:44 +11:00
parent f5c97a1efa
commit d44b639f18

View file

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
"sync"
"time"
@ -78,7 +77,7 @@ func (c *LINEChannel) Start(ctx context.Context) error {
c.ctx, c.cancel = context.WithCancel(ctx)
// Fetch bot profile to get bot's userId for mention detection
info, err := c.client.GetBotInfo()
info, err := c.client.WithContext(ctx).GetBotInfo()
if err != nil {
logger.WarnCF("line", "Failed to fetch bot info (mention detection disabled)", map[string]any{
"error": err.Error(),
@ -178,9 +177,6 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
var mediaPaths []string
var messageID string
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
storeMedia := func(localPath, filename, scope string) string {
@ -198,6 +194,7 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
switch msg := msgEvent.Message.(type) {
case webhook.TextMessageContent:
messageID = msg.Id
content = msg.Text
isMentioned = c.isBotMentioned(msg)
if msg.QuoteToken != "" {
@ -207,29 +204,31 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
content = c.stripBotMention(content, msg)
}
case webhook.ImageMessageContent:
localPath := c.downloadContent(messageID, "image.jpg")
if localPath != "" {
messageID = msg.Id
if localPath := c.downloadContent(msg.Id, "image.jpg"); localPath != "" {
scope := channels.BuildMediaScope("line", chatID, msg.Id)
mediaPaths = append(mediaPaths, storeMedia(localPath, "image.jpg", scope))
content = "[image]"
}
case webhook.AudioMessageContent:
localPath := c.downloadContent(messageID, "audio.m4a")
if localPath != "" {
messageID = msg.Id
if localPath := c.downloadContent(msg.Id, "audio.m4a"); localPath != "" {
scope := channels.BuildMediaScope("line", chatID, msg.Id)
mediaPaths = append(mediaPaths, storeMedia(localPath, "audio.m4a", scope))
content = "[audio]"
}
case webhook.VideoMessageContent:
localPath := c.downloadContent(messageID, "video.mp4")
if localPath != "" {
messageID = msg.Id
if localPath := c.downloadContent(msg.Id, "video.mp4"); localPath != "" {
scope := channels.BuildMediaScope("line", chatID, msg.Id)
mediaPaths = append(mediaPaths, storeMedia(localPath, "video.mp4", scope))
content = "[video]"
}
case webhook.FileMessageContent:
messageID = msg.Id
content = "[file]"
case webhook.StickerMessageContent:
messageID = msg.Id
content = "[sticker]"
default:
logger.WarnCF("line", "Ignoring unsupported message type", map[string]any{
@ -390,6 +389,9 @@ func (c *LINEChannel) resolveSource(source webhook.SourceInterface) (senderID, c
case webhook.UserSource:
return src.UserId, src.UserId, "user"
default:
logger.WarnCF("line", "Unknown source type", map[string]any{
"type": fmt.Sprintf("%T", source),
})
return "", "", "unknown"
}
}
@ -416,7 +418,7 @@ func (c *LINEChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
if entry, ok := c.replyTokens.LoadAndDelete(msg.ChatID); ok {
tokenEntry := entry.(replyTokenEntry)
if time.Since(tokenEntry.timestamp) < lineReplyTokenMaxAge {
_, err := c.client.ReplyMessage(&messaging_api.ReplyMessageRequest{
_, err := c.client.WithContext(ctx).ReplyMessage(&messaging_api.ReplyMessageRequest{
ReplyToken: tokenEntry.token,
Messages: []messaging_api.MessageInterface{&textMsg},
})
@ -432,7 +434,7 @@ func (c *LINEChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
}
// Fall back to Push API
_, err := c.client.PushMessage(&messaging_api.PushMessageRequest{
_, err := c.client.WithContext(ctx).PushMessage(&messaging_api.PushMessageRequest{
To: msg.ChatID,
Messages: []messaging_api.MessageInterface{&textMsg},
}, "")
@ -462,7 +464,7 @@ func (c *LINEChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMessag
}
textMsg := messaging_api.TextMessage{Text: caption}
if _, err := c.client.PushMessage(&messaging_api.PushMessageRequest{
if _, err := c.client.WithContext(ctx).PushMessage(&messaging_api.PushMessageRequest{
To: msg.ChatID,
Messages: []messaging_api.MessageInterface{&textMsg},
}, ""); err != nil {
@ -520,7 +522,7 @@ func (c *LINEChannel) StartTyping(ctx context.Context, chatID string) (func(), e
// sendLoading sends a loading animation indicator to the chat.
func (c *LINEChannel) sendLoading(ctx context.Context, chatID string) error {
_, err := c.client.ShowLoadingAnimation(&messaging_api.ShowLoadingAnimationRequest{
_, err := c.client.WithContext(ctx).ShowLoadingAnimation(&messaging_api.ShowLoadingAnimationRequest{
ChatId: chatID,
LoadingSeconds: 60,
})