Merge branch 'feat-enhancement/telegram-username-support' into dev
- Use telego.ChatID for username support (simpler approach) - Skip placeholder/edit logic for username targets
This commit is contained in:
commit
9ee43ffcd3
1 changed files with 38 additions and 38 deletions
|
|
@ -38,14 +38,14 @@ var (
|
||||||
|
|
||||||
type TelegramChannel struct {
|
type TelegramChannel struct {
|
||||||
*BaseChannel
|
*BaseChannel
|
||||||
bot *telego.Bot
|
bot *telego.Bot
|
||||||
commands TelegramCommander
|
commands TelegramCommander
|
||||||
config *config.Config
|
config *config.Config
|
||||||
chatIDs map[string]int64
|
chatIDs map[string]int64
|
||||||
usernameCache map[string]int64 // Cache username -> chat ID resolution
|
usernameCache map[string]int64 // Cache username -> chat ID resolution
|
||||||
transcriber *voice.GroqTranscriber
|
transcriber *voice.GroqTranscriber
|
||||||
placeholders sync.Map // chatID -> messageID
|
placeholders sync.Map // chatID -> messageID
|
||||||
stopThinking sync.Map // chatID -> thinkingCancel
|
stopThinking sync.Map // chatID -> thinkingCancel
|
||||||
}
|
}
|
||||||
|
|
||||||
type thinkingCancel struct {
|
type thinkingCancel struct {
|
||||||
|
|
@ -194,34 +194,41 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
|
||||||
return fmt.Errorf("telegram bot not running")
|
return fmt.Errorf("telegram bot not running")
|
||||||
}
|
}
|
||||||
|
|
||||||
chatID, err := c.parseChatID(ctx, msg.ChatID)
|
chatID, err := parseChatID(msg.ChatID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid chat ID: %w", err)
|
return fmt.Errorf("invalid chat ID: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop thinking animation
|
// Check if this is a username target (for new conversations)
|
||||||
if stop, ok := c.stopThinking.Load(msg.ChatID); ok {
|
isUsername := strings.HasPrefix(msg.ChatID, "@")
|
||||||
if cf, ok := stop.(*thinkingCancel); ok && cf != nil {
|
|
||||||
cf.Cancel()
|
// Stop thinking animation (only for numeric chat IDs)
|
||||||
|
if !isUsername {
|
||||||
|
if stop, ok := c.stopThinking.Load(msg.ChatID); ok {
|
||||||
|
if cf, ok := stop.(*thinkingCancel); ok && cf != nil {
|
||||||
|
cf.Cancel()
|
||||||
|
}
|
||||||
|
c.stopThinking.Delete(msg.ChatID)
|
||||||
}
|
}
|
||||||
c.stopThinking.Delete(msg.ChatID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlContent := markdownToTelegramHTML(msg.Content)
|
htmlContent := markdownToTelegramHTML(msg.Content)
|
||||||
|
|
||||||
// Try to edit placeholder
|
// Try to edit placeholder (skip for username targets - no previous message)
|
||||||
if pID, ok := c.placeholders.Load(msg.ChatID); ok {
|
if !isUsername {
|
||||||
c.placeholders.Delete(msg.ChatID)
|
if pID, ok := c.placeholders.Load(msg.ChatID); ok {
|
||||||
editMsg := tu.EditMessageText(tu.ID(chatID), pID.(int), htmlContent)
|
c.placeholders.Delete(msg.ChatID)
|
||||||
editMsg.ParseMode = telego.ModeHTML
|
editMsg := tu.EditMessageText(chatID, pID.(int), htmlContent)
|
||||||
|
editMsg.ParseMode = telego.ModeHTML
|
||||||
|
|
||||||
if _, err = c.bot.EditMessageText(ctx, editMsg); err == nil {
|
if _, err = c.bot.EditMessageText(ctx, editMsg); err == nil {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
// Fallback to new message if edit fails
|
||||||
}
|
}
|
||||||
// Fallback to new message if edit fails
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tgMsg := tu.Message(tu.ID(chatID), htmlContent)
|
tgMsg := tu.Message(chatID, htmlContent)
|
||||||
tgMsg.ParseMode = telego.ModeHTML
|
tgMsg.ParseMode = telego.ModeHTML
|
||||||
|
|
||||||
if _, err = c.bot.SendMessage(ctx, tgMsg); err != nil {
|
if _, err = c.bot.SendMessage(ctx, tgMsg); err != nil {
|
||||||
|
|
@ -457,26 +464,19 @@ func (c *TelegramChannel) downloadFile(ctx context.Context, fileID, ext string)
|
||||||
return c.downloadFileWithInfo(file, ext)
|
return c.downloadFileWithInfo(file, ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TelegramChannel) parseChatID(ctx context.Context, chatIDStr string) (int64, error) {
|
func parseChatID(chatIDStr string) (telego.ChatID, error) {
|
||||||
// Strip @ prefix if present (username format)
|
// Check if it's a username (starts with @)
|
||||||
chatIDStr = strings.TrimPrefix(chatIDStr, "@")
|
if strings.HasPrefix(chatIDStr, "@") {
|
||||||
|
return tu.Username(chatIDStr), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Try parsing as numeric ID
|
// Otherwise, parse as numeric ID
|
||||||
var id int64
|
var id int64
|
||||||
_, err := fmt.Sscanf(chatIDStr, "%d", &id)
|
_, err := fmt.Sscanf(chatIDStr, "%d", &id)
|
||||||
if err == nil {
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If not an integer, try to resolve as username
|
|
||||||
chatID, err := c.resolveUsername(ctx, chatIDStr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("chat ID must be numeric (e.g., 123456789) or a valid username (@%s). "+
|
return telego.ChatID{}, err
|
||||||
"Use your numeric user ID from https://t.me/getdivid or ensure the username is correct",
|
|
||||||
chatIDStr)
|
|
||||||
}
|
}
|
||||||
|
return tu.ID(id), nil
|
||||||
return chatID, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func markdownToTelegramHTML(text string) string {
|
func markdownToTelegramHTML(text string) string {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue