address PR comments.
Signed-off-by: Kai Xia <kaix+github@fastmail.com>
This commit is contained in:
parent
f5c97a1efa
commit
d44b639f18
1 changed files with 17 additions and 15 deletions
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -78,7 +77,7 @@ func (c *LINEChannel) Start(ctx context.Context) error {
|
||||||
c.ctx, c.cancel = context.WithCancel(ctx)
|
c.ctx, c.cancel = context.WithCancel(ctx)
|
||||||
|
|
||||||
// Fetch bot profile to get bot's userId for mention detection
|
// 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 {
|
if err != nil {
|
||||||
logger.WarnCF("line", "Failed to fetch bot info (mention detection disabled)", map[string]any{
|
logger.WarnCF("line", "Failed to fetch bot info (mention detection disabled)", map[string]any{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
|
|
@ -178,9 +177,6 @@ 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 {
|
||||||
|
|
@ -198,6 +194,7 @@ 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 != "" {
|
||||||
|
|
@ -207,29 +204,31 @@ func (c *LINEChannel) processEvent(event webhook.EventInterface) {
|
||||||
content = c.stripBotMention(content, msg)
|
content = c.stripBotMention(content, msg)
|
||||||
}
|
}
|
||||||
case webhook.ImageMessageContent:
|
case webhook.ImageMessageContent:
|
||||||
localPath := c.downloadContent(messageID, "image.jpg")
|
messageID = msg.Id
|
||||||
if localPath != "" {
|
if localPath := c.downloadContent(msg.Id, "image.jpg"); 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:
|
||||||
localPath := c.downloadContent(messageID, "audio.m4a")
|
messageID = msg.Id
|
||||||
if localPath != "" {
|
if localPath := c.downloadContent(msg.Id, "audio.m4a"); 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:
|
||||||
localPath := c.downloadContent(messageID, "video.mp4")
|
messageID = msg.Id
|
||||||
if localPath != "" {
|
if localPath := c.downloadContent(msg.Id, "video.mp4"); 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:
|
||||||
logger.WarnCF("line", "Ignoring unsupported message type", map[string]any{
|
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:
|
case webhook.UserSource:
|
||||||
return src.UserId, src.UserId, "user"
|
return src.UserId, src.UserId, "user"
|
||||||
default:
|
default:
|
||||||
|
logger.WarnCF("line", "Unknown source type", map[string]any{
|
||||||
|
"type": fmt.Sprintf("%T", source),
|
||||||
|
})
|
||||||
return "", "", "unknown"
|
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 {
|
if entry, ok := c.replyTokens.LoadAndDelete(msg.ChatID); ok {
|
||||||
tokenEntry := entry.(replyTokenEntry)
|
tokenEntry := entry.(replyTokenEntry)
|
||||||
if time.Since(tokenEntry.timestamp) < lineReplyTokenMaxAge {
|
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,
|
ReplyToken: tokenEntry.token,
|
||||||
Messages: []messaging_api.MessageInterface{&textMsg},
|
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
|
// Fall back to Push API
|
||||||
_, err := c.client.PushMessage(&messaging_api.PushMessageRequest{
|
_, err := c.client.WithContext(ctx).PushMessage(&messaging_api.PushMessageRequest{
|
||||||
To: msg.ChatID,
|
To: msg.ChatID,
|
||||||
Messages: []messaging_api.MessageInterface{&textMsg},
|
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}
|
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,
|
To: msg.ChatID,
|
||||||
Messages: []messaging_api.MessageInterface{&textMsg},
|
Messages: []messaging_api.MessageInterface{&textMsg},
|
||||||
}, ""); err != nil {
|
}, ""); 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.
|
// sendLoading sends a loading animation indicator to the chat.
|
||||||
func (c *LINEChannel) sendLoading(ctx context.Context, chatID string) error {
|
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,
|
ChatId: chatID,
|
||||||
LoadingSeconds: 60,
|
LoadingSeconds: 60,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue