feat(im): add proactive support reply delivery

This commit is contained in:
姚章浩
2026-07-15 14:12:57 +08:00
parent 148c1be452
commit 0eb13a022e
2 changed files with 100 additions and 0 deletions
+42
View File
@@ -1253,6 +1253,48 @@ func (s *Service) GetChannelAdapter(channelID string) (Adapter, *IMChannel, bool
return cs.Adapter, cs.Channel, true
}
// ReplyTarget identifies the original customer conversation for a proactive reply.
type ReplyTarget struct {
ChannelID string
UserID string
ChatID string
ChatType ChatType
ThreadID string
}
// SendReplyToSource delivers a support reply through the configured platform adapter.
// It intentionally does not depend on a short-lived session webhook; adapters can use
// their authenticated proactive-message API when Extra is empty.
func (s *Service) SendReplyToSource(ctx context.Context, target ReplyTarget, content string) error {
if strings.TrimSpace(target.ChannelID) == "" {
return fmt.Errorf("channel ID is required")
}
content = strings.TrimSpace(content)
if content == "" {
return fmt.Errorf("reply content is required")
}
adapter, channel, ok := s.GetChannelAdapter(target.ChannelID)
if !ok || adapter == nil || channel == nil {
return fmt.Errorf("IM channel %s is not running", target.ChannelID)
}
if !channel.Enabled {
return fmt.Errorf("IM channel %s is disabled", target.ChannelID)
}
chatType := target.ChatType
if chatType == "" {
chatType = ChatTypeDirect
}
incoming := &IncomingMessage{
Platform: Platform(channel.Platform),
UserID: strings.TrimSpace(target.UserID),
ChatID: strings.TrimSpace(target.ChatID),
ChatType: chatType,
ThreadID: strings.TrimSpace(target.ThreadID),
MessageID: "support:" + uuid.New().String(),
}
return adapter.SendReply(ctx, incoming, &ReplyMessage{Content: content, IsFinal: true})
}
// GetChannelByID loads a channel from the database.
func (s *Service) GetChannelByID(channelID string) (*IMChannel, error) {
var ch IMChannel