feat(telegram): add support for sending messages to @usernames

- Modified parseChatID to return telego.ChatID and handle usernames (strings starting with @)
- Updated Send method to use telego.ChatID directly in tu.Message() and tu.EditMessageText()
- Skip placeholder/edit logic for username targets since there's no previous message to edit

Closes: #feature/telegram-username-support
This commit is contained in:
Vishnuvardhan Reddy 2026-02-26 14:23:16 +00:00
parent 8a1fb03974
commit 7def9b26b6

View file

@ -169,29 +169,36 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
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 {
@ -427,10 +434,19 @@ func (c *TelegramChannel) downloadFile(ctx context.Context, fileID, ext string)
return c.downloadFileWithInfo(file, ext) return c.downloadFileWithInfo(file, ext)
} }
func parseChatID(chatIDStr string) (int64, error) { func parseChatID(chatIDStr string) (telego.ChatID, error) {
// Check if it's a username (starts with @)
if strings.HasPrefix(chatIDStr, "@") {
return tu.Username(chatIDStr), nil
}
// Otherwise, parse as numeric ID
var id int64 var id int64
_, err := fmt.Sscanf(chatIDStr, "%d", &id) _, err := fmt.Sscanf(chatIDStr, "%d", &id)
return id, err if err != nil {
return telego.ChatID{}, err
}
return tu.ID(id), nil
} }
func markdownToTelegramHTML(text string) string { func markdownToTelegramHTML(text string) string {