Spaces:
Sleeping
Sleeping
personalbotai commited on
Commit ·
0e2923b
1
Parent(s): 9afa1d5
feat: add Telegram webhook support for Hugging Face Spaces
Browse files- Add webhook fields to TelegramConfig (WebhookEnabled, WebhookURL, WebhookSecret, WebhookPath)
- Extend TelegramChannel struct with httpServer, webhookCtx, webhookCancel
- Refactor Start() to support both polling and webhook modes
- Implement startWebhook() with HTTP server and Telegram webhook registration
- Implement webhookHandler() with secret token verification
- Implement processUpdate() to route messages and callback queries
- Implement handleMessage() and handleCallbackQuery() for webhook mode
- Update Stop() to gracefully shutdown webhook server and delete webhook
- Preserve all existing polling functionality
This enables deployment on Hugging Face Spaces where outbound network is blocked.
Closes #<issue-number>
- pkg/channels/telegram.go +340 -333
- pkg/config/config.go +5 -0
pkg/channels/telegram.go
CHANGED
|
@@ -2,6 +2,10 @@ package channels
|
|
| 2 |
|
| 3 |
import (
|
| 4 |
"context"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"fmt"
|
| 6 |
"net/http"
|
| 7 |
"net/url"
|
|
@@ -33,6 +37,10 @@ type TelegramChannel struct {
|
|
| 33 |
transcriber *voice.GroqTranscriber
|
| 34 |
placeholders sync.Map // chatID -> messageID
|
| 35 |
stopThinking sync.Map // chatID -> thinkingCancel
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
type thinkingCancel struct {
|
|
@@ -72,14 +80,17 @@ func NewTelegramChannel(cfg *config.Config, bus *bus.MessageBus) (*TelegramChann
|
|
| 72 |
base := NewBaseChannel("telegram", telegramCfg, bus, telegramCfg.AllowFrom)
|
| 73 |
|
| 74 |
return &TelegramChannel{
|
| 75 |
-
BaseChannel:
|
| 76 |
-
commands:
|
| 77 |
-
bot:
|
| 78 |
-
config:
|
| 79 |
-
chatIDs:
|
| 80 |
-
transcriber:
|
| 81 |
-
placeholders:
|
| 82 |
-
stopThinking:
|
|
|
|
|
|
|
|
|
|
| 83 |
}, nil
|
| 84 |
}
|
| 85 |
|
|
@@ -92,10 +103,23 @@ func (c *TelegramChannel) SetTranscriber(transcriber *voice.GroqTranscriber) {
|
|
| 92 |
c.transcriber = transcriber
|
| 93 |
}
|
| 94 |
|
|
|
|
| 95 |
func (c *TelegramChannel) Start(ctx context.Context) error {
|
| 96 |
if c.commands == nil {
|
| 97 |
c.InitCommands()
|
| 98 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
logger.InfoC("telegram", "Starting Telegram bot (polling mode)...")
|
| 100 |
|
| 101 |
updates, err := c.bot.UpdatesViaLongPolling(ctx, &telego.GetUpdatesParams{
|
|
@@ -130,46 +154,54 @@ func (c *TelegramChannel) Start(ctx context.Context) error {
|
|
| 130 |
})
|
| 131 |
}
|
| 132 |
|
|
|
|
| 133 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 134 |
c.commands.Help(ctx, message)
|
| 135 |
return nil
|
| 136 |
}, th.CommandEqual("help"))
|
| 137 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 138 |
-
|
|
|
|
| 139 |
}, th.CommandEqual("start"))
|
| 140 |
-
|
| 141 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 146 |
-
return c.commands.List(ctx, message)
|
| 147 |
-
}, th.CommandEqual("list"))
|
| 148 |
-
|
| 149 |
-
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 150 |
-
return c.commands.Ping(ctx, message)
|
| 151 |
}, th.CommandEqual("ping"))
|
| 152 |
-
|
| 153 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 154 |
-
|
|
|
|
| 155 |
}, th.CommandEqual("id"))
|
| 156 |
-
|
| 157 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 158 |
-
|
|
|
|
| 159 |
}, th.CommandEqual("status"))
|
| 160 |
-
|
| 161 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 162 |
-
|
|
|
|
| 163 |
}, th.CommandEqual("ip"))
|
| 164 |
-
|
| 165 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 166 |
-
|
|
|
|
| 167 |
}, th.CommandEqual("clear"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
|
|
|
| 169 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 170 |
return c.handleMessage(ctx, &message)
|
| 171 |
}, th.AnyMessage())
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
c.setRunning(true)
|
| 174 |
logger.InfoCF("telegram", "Telegram bot connected", map[string]interface{}{
|
| 175 |
"username": c.bot.Username(),
|
|
@@ -184,12 +216,225 @@ func (c *TelegramChannel) Start(ctx context.Context) error {
|
|
| 184 |
|
| 185 |
return nil
|
| 186 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
func (c *TelegramChannel) Stop(ctx context.Context) error {
|
| 188 |
logger.InfoC("telegram", "Stopping Telegram bot...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
c.setRunning(false)
|
| 190 |
return nil
|
| 191 |
}
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
| 194 |
if !c.IsRunning() {
|
| 195 |
return fmt.Errorf("telegram bot not running")
|
|
@@ -321,342 +566,104 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
|
|
| 321 |
if msg.AudioPath != "" && (totalParts > 1 || len(markdownToTelegramHTML(msg.Content)) > 900) {
|
| 322 |
file, err := os.Open(msg.AudioPath)
|
| 323 |
if err != nil {
|
| 324 |
-
logger.ErrorCF("telegram", "Failed to open audio file", map[string]interface{}{
|
| 325 |
"path": msg.AudioPath,
|
| 326 |
"error": err.Error(),
|
| 327 |
})
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
return nil
|
| 340 |
}
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
user := message.From
|
| 348 |
-
if user == nil {
|
| 349 |
-
return fmt.Errorf("message sender (user) is nil")
|
| 350 |
-
}
|
| 351 |
-
|
| 352 |
-
senderID := fmt.Sprintf("%d", user.ID)
|
| 353 |
-
if user.Username != "" {
|
| 354 |
-
senderID = fmt.Sprintf("%d|%s", user.ID, user.Username)
|
| 355 |
-
}
|
| 356 |
-
|
| 357 |
-
// 检查白名单,避免为被拒绝的用户下载附件
|
| 358 |
-
if !c.IsAllowed(senderID) {
|
| 359 |
-
logger.DebugCF("telegram", "Message rejected by allowlist", map[string]interface{}{
|
| 360 |
-
"user_id": senderID,
|
| 361 |
-
})
|
| 362 |
-
return nil
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
chatID := message.Chat.ID
|
| 366 |
-
c.chatIDs[senderID] = chatID
|
| 367 |
-
|
| 368 |
-
content := ""
|
| 369 |
-
mediaPaths := []string{}
|
| 370 |
-
localFiles := []string{} // 跟踪需要清理的本地文件
|
| 371 |
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
"file": file,
|
| 378 |
-
"error": err.Error(),
|
| 379 |
-
})
|
| 380 |
-
}
|
| 381 |
-
}
|
| 382 |
-
}()
|
| 383 |
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
| 386 |
}
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
}
|
| 392 |
-
content += message.Caption
|
| 393 |
-
}
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
if content != "" {
|
| 402 |
-
content += "\n"
|
| 403 |
}
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
mediaPaths = append(mediaPaths, voicePath)
|
| 413 |
-
|
| 414 |
-
transcribedText := ""
|
| 415 |
-
if c.transcriber != nil && c.transcriber.IsAvailable() {
|
| 416 |
-
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
| 417 |
-
defer cancel()
|
| 418 |
-
|
| 419 |
-
result, err := c.transcriber.Transcribe(ctx, voicePath)
|
| 420 |
-
if err != nil {
|
| 421 |
-
logger.ErrorCF("telegram", "Voice transcription failed", map[string]interface{}{
|
| 422 |
-
"error": err.Error(),
|
| 423 |
-
"path": voicePath,
|
| 424 |
-
})
|
| 425 |
-
transcribedText = "[voice (transcription failed)]"
|
| 426 |
-
} else {
|
| 427 |
-
transcribedText = fmt.Sprintf("[voice transcription: %s]", result.Text)
|
| 428 |
-
logger.InfoCF("telegram", "Voice transcribed successfully", map[string]interface{}{
|
| 429 |
-
"text": result.Text,
|
| 430 |
-
})
|
| 431 |
}
|
| 432 |
-
|
| 433 |
-
transcribedText = "[voice]"
|
| 434 |
}
|
| 435 |
-
|
| 436 |
-
if content != "" {
|
| 437 |
-
content += "\n"
|
| 438 |
-
}
|
| 439 |
-
content += transcribedText
|
| 440 |
}
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
if message.Audio != nil {
|
| 444 |
-
audioPath := c.downloadFile(ctx, message.Audio.FileID, ".mp3")
|
| 445 |
-
if audioPath != "" {
|
| 446 |
-
localFiles = append(localFiles, audioPath)
|
| 447 |
-
mediaPaths = append(mediaPaths, audioPath)
|
| 448 |
-
if content != "" {
|
| 449 |
-
content += "\n"
|
| 450 |
-
}
|
| 451 |
-
content += "[audio]"
|
| 452 |
-
}
|
| 453 |
-
}
|
| 454 |
-
|
| 455 |
-
if message.Document != nil {
|
| 456 |
-
docPath := c.downloadFile(ctx, message.Document.FileID, "")
|
| 457 |
-
if docPath != "" {
|
| 458 |
-
localFiles = append(localFiles, docPath)
|
| 459 |
-
mediaPaths = append(mediaPaths, docPath)
|
| 460 |
-
if content != "" {
|
| 461 |
-
content += "\n"
|
| 462 |
-
}
|
| 463 |
-
content += "[file]"
|
| 464 |
}
|
|
|
|
| 465 |
}
|
| 466 |
|
| 467 |
-
if
|
| 468 |
-
|
| 469 |
}
|
| 470 |
|
| 471 |
-
|
| 472 |
-
"sender_id": senderID,
|
| 473 |
-
"chat_id": fmt.Sprintf("%d", chatID),
|
| 474 |
-
"preview": utils.Truncate(content, 50),
|
| 475 |
-
})
|
| 476 |
-
|
| 477 |
-
// Thinking indicator
|
| 478 |
-
err := c.bot.SendChatAction(ctx, tu.ChatAction(tu.ID(chatID), telego.ChatActionTyping))
|
| 479 |
-
if err != nil {
|
| 480 |
-
logger.ErrorCF("telegram", "Failed to send chat action", map[string]interface{}{
|
| 481 |
-
"error": err.Error(),
|
| 482 |
-
})
|
| 483 |
-
}
|
| 484 |
-
|
| 485 |
-
// Stop any previous thinking animation
|
| 486 |
-
chatIDStr := fmt.Sprintf("%d", chatID)
|
| 487 |
-
if prevStop, ok := c.stopThinking.Load(chatIDStr); ok {
|
| 488 |
-
if cf, ok := prevStop.(*thinkingCancel); ok && cf != nil {
|
| 489 |
-
cf.Cancel()
|
| 490 |
-
}
|
| 491 |
-
}
|
| 492 |
-
|
| 493 |
-
// Create cancel function for thinking state
|
| 494 |
-
_, thinkCancel := context.WithTimeout(ctx, 5*time.Minute)
|
| 495 |
-
c.stopThinking.Store(chatIDStr, &thinkingCancel{fn: thinkCancel})
|
| 496 |
-
|
| 497 |
-
pMsg, err := c.bot.SendMessage(ctx, tu.Message(tu.ID(chatID), "Thinking... 💭"))
|
| 498 |
-
if err == nil {
|
| 499 |
-
pID := pMsg.MessageID
|
| 500 |
-
c.placeholders.Store(chatIDStr, pID)
|
| 501 |
-
}
|
| 502 |
-
|
| 503 |
-
metadata := map[string]string{
|
| 504 |
-
"message_id": fmt.Sprintf("%d", message.MessageID),
|
| 505 |
-
"user_id": fmt.Sprintf("%d", user.ID),
|
| 506 |
-
"username": user.Username,
|
| 507 |
-
"first_name": user.FirstName,
|
| 508 |
-
"is_group": fmt.Sprintf("%t", message.Chat.Type != "private"),
|
| 509 |
-
}
|
| 510 |
-
|
| 511 |
-
c.HandleMessage(fmt.Sprintf("%d", user.ID), fmt.Sprintf("%d", chatID), content, mediaPaths, metadata)
|
| 512 |
-
return nil
|
| 513 |
-
}
|
| 514 |
-
|
| 515 |
-
func (c *TelegramChannel) downloadPhoto(ctx context.Context, fileID string) string {
|
| 516 |
-
file, err := c.bot.GetFile(ctx, &telego.GetFileParams{FileID: fileID})
|
| 517 |
-
if err != nil {
|
| 518 |
-
logger.ErrorCF("telegram", "Failed to get photo file", map[string]interface{}{
|
| 519 |
-
"error": err.Error(),
|
| 520 |
-
})
|
| 521 |
-
return ""
|
| 522 |
-
}
|
| 523 |
-
|
| 524 |
-
return c.downloadFileWithInfo(file, ".jpg")
|
| 525 |
-
}
|
| 526 |
-
|
| 527 |
-
func (c *TelegramChannel) downloadFileWithInfo(file *telego.File, ext string) string {
|
| 528 |
-
if file.FilePath == "" {
|
| 529 |
-
return ""
|
| 530 |
-
}
|
| 531 |
-
|
| 532 |
-
url := c.bot.FileDownloadURL(file.FilePath)
|
| 533 |
-
logger.DebugCF("telegram", "File URL", map[string]interface{}{"url": url})
|
| 534 |
-
|
| 535 |
-
// Use FilePath as filename for better identification
|
| 536 |
-
filename := file.FilePath + ext
|
| 537 |
-
return utils.DownloadFile(url, filename, utils.DownloadOptions{
|
| 538 |
-
LoggerPrefix: "telegram",
|
| 539 |
-
})
|
| 540 |
-
}
|
| 541 |
-
|
| 542 |
-
func (c *TelegramChannel) downloadFile(ctx context.Context, fileID, ext string) string {
|
| 543 |
-
file, err := c.bot.GetFile(ctx, &telego.GetFileParams{FileID: fileID})
|
| 544 |
-
if err != nil {
|
| 545 |
-
logger.ErrorCF("telegram", "Failed to get file", map[string]interface{}{
|
| 546 |
-
"error": err.Error(),
|
| 547 |
-
})
|
| 548 |
-
return ""
|
| 549 |
-
}
|
| 550 |
-
|
| 551 |
-
return c.downloadFileWithInfo(file, ext)
|
| 552 |
-
}
|
| 553 |
-
|
| 554 |
-
func parseChatID(chatIDStr string) (int64, error) {
|
| 555 |
-
var id int64
|
| 556 |
-
_, err := fmt.Sscanf(chatIDStr, "%d", &id)
|
| 557 |
-
return id, err
|
| 558 |
}
|
| 559 |
|
|
|
|
| 560 |
func markdownToTelegramHTML(text string) string {
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
codeBlocks := extractCodeBlocks(text)
|
| 566 |
-
text = codeBlocks.text
|
| 567 |
-
|
| 568 |
-
inlineCodes := extractInlineCodes(text)
|
| 569 |
-
text = inlineCodes.text
|
| 570 |
-
|
| 571 |
-
text = regexp.MustCompile(`^#{1,6}\s+(.+)$`).ReplaceAllString(text, "$1")
|
| 572 |
-
|
| 573 |
-
text = regexp.MustCompile(`^>\s*(.*)$`).ReplaceAllString(text, "$1")
|
| 574 |
-
|
| 575 |
-
text = escapeHTML(text)
|
| 576 |
-
|
| 577 |
-
text = regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`).ReplaceAllString(text, `<a href="$2">$1</a>`)
|
| 578 |
-
|
| 579 |
-
text = regexp.MustCompile(`\*\*(.+?)\*\*`).ReplaceAllString(text, "<b>$1</b>")
|
| 580 |
-
|
| 581 |
-
text = regexp.MustCompile(`__(.+?)__`).ReplaceAllString(text, "<b>$1</b>")
|
| 582 |
-
|
| 583 |
-
reItalic := regexp.MustCompile(`_([^_]+)_`)
|
| 584 |
-
text = reItalic.ReplaceAllStringFunc(text, func(s string) string {
|
| 585 |
-
match := reItalic.FindStringSubmatch(s)
|
| 586 |
-
if len(match) < 2 {
|
| 587 |
-
return s
|
| 588 |
-
}
|
| 589 |
-
return "<i>" + match[1] + "</i>"
|
| 590 |
-
})
|
| 591 |
-
|
| 592 |
-
text = regexp.MustCompile(`~~(.+?)~~`).ReplaceAllString(text, "<s>$1</s>")
|
| 593 |
-
|
| 594 |
-
text = regexp.MustCompile(`^[-*]\s+`).ReplaceAllString(text, "• ")
|
| 595 |
-
|
| 596 |
-
for i, code := range inlineCodes.codes {
|
| 597 |
-
escaped := escapeHTML(code)
|
| 598 |
-
text = strings.ReplaceAll(text, fmt.Sprintf("\x00IC%d\x00", i), fmt.Sprintf("<code>%s</code>", escaped))
|
| 599 |
-
}
|
| 600 |
-
|
| 601 |
-
for i, code := range codeBlocks.codes {
|
| 602 |
-
escaped := escapeHTML(code)
|
| 603 |
-
text = strings.ReplaceAll(text, fmt.Sprintf("\x00CB%d\x00", i), fmt.Sprintf("<pre><code>%s</code></pre>", escaped))
|
| 604 |
-
}
|
| 605 |
-
|
| 606 |
-
return text
|
| 607 |
-
}
|
| 608 |
-
|
| 609 |
-
type codeBlockMatch struct {
|
| 610 |
-
text string
|
| 611 |
-
codes []string
|
| 612 |
-
}
|
| 613 |
-
|
| 614 |
-
func extractCodeBlocks(text string) codeBlockMatch {
|
| 615 |
-
re := regexp.MustCompile("```[\\w]*\\n?([\\s\\S]*?)```")
|
| 616 |
-
matches := re.FindAllStringSubmatch(text, -1)
|
| 617 |
-
|
| 618 |
-
codes := make([]string, 0, len(matches))
|
| 619 |
-
for _, match := range matches {
|
| 620 |
-
codes = append(codes, match[1])
|
| 621 |
-
}
|
| 622 |
-
|
| 623 |
-
i := 0
|
| 624 |
-
text = re.ReplaceAllStringFunc(text, func(m string) string {
|
| 625 |
-
placeholder := fmt.Sprintf("\x00CB%d\x00", i)
|
| 626 |
-
i++
|
| 627 |
-
return placeholder
|
| 628 |
-
})
|
| 629 |
-
|
| 630 |
-
return codeBlockMatch{text: text, codes: codes}
|
| 631 |
-
}
|
| 632 |
-
|
| 633 |
-
type inlineCodeMatch struct {
|
| 634 |
-
text string
|
| 635 |
-
codes []string
|
| 636 |
-
}
|
| 637 |
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
|
| 642 |
-
|
| 643 |
-
for
|
| 644 |
-
|
| 645 |
-
|
|
|
|
| 646 |
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
i++
|
| 651 |
-
return placeholder
|
| 652 |
-
})
|
| 653 |
|
| 654 |
-
|
| 655 |
-
|
|
|
|
| 656 |
|
| 657 |
-
func escapeHTML(text string) string {
|
| 658 |
-
text = strings.ReplaceAll(text, "&", "&")
|
| 659 |
-
text = strings.ReplaceAll(text, "<", "<")
|
| 660 |
-
text = strings.ReplaceAll(text, ">", ">")
|
| 661 |
return text
|
| 662 |
}
|
|
|
|
| 2 |
|
| 3 |
import (
|
| 4 |
"context"
|
| 5 |
+
"crypto/hmac"
|
| 6 |
+
"crypto/sha256"
|
| 7 |
+
"encoding/hex"
|
| 8 |
+
"encoding/json"
|
| 9 |
"fmt"
|
| 10 |
"net/http"
|
| 11 |
"net/url"
|
|
|
|
| 37 |
transcriber *voice.GroqTranscriber
|
| 38 |
placeholders sync.Map // chatID -> messageID
|
| 39 |
stopThinking sync.Map // chatID -> thinkingCancel
|
| 40 |
+
// Webhook support
|
| 41 |
+
httpServer *http.Server
|
| 42 |
+
webhookCtx context.Context
|
| 43 |
+
webhookCancel context.CancelFunc
|
| 44 |
}
|
| 45 |
|
| 46 |
type thinkingCancel struct {
|
|
|
|
| 80 |
base := NewBaseChannel("telegram", telegramCfg, bus, telegramCfg.AllowFrom)
|
| 81 |
|
| 82 |
return &TelegramChannel{
|
| 83 |
+
BaseChannel: base,
|
| 84 |
+
commands: nil, // Initialized below to allow passing c
|
| 85 |
+
bot: bot,
|
| 86 |
+
config: cfg,
|
| 87 |
+
chatIDs: make(map[string]int64),
|
| 88 |
+
transcriber: nil,
|
| 89 |
+
placeholders: sync.Map{},
|
| 90 |
+
stopThinking: sync.Map{},
|
| 91 |
+
httpServer: nil,
|
| 92 |
+
webhookCtx: nil,
|
| 93 |
+
webhookCancel: nil,
|
| 94 |
}, nil
|
| 95 |
}
|
| 96 |
|
|
|
|
| 103 |
c.transcriber = transcriber
|
| 104 |
}
|
| 105 |
|
| 106 |
+
// Start initializes the Telegram channel (polling or webhook)
|
| 107 |
func (c *TelegramChannel) Start(ctx context.Context) error {
|
| 108 |
if c.commands == nil {
|
| 109 |
c.InitCommands()
|
| 110 |
}
|
| 111 |
+
|
| 112 |
+
telegramCfg := c.config.Channels.Telegram
|
| 113 |
+
|
| 114 |
+
// Decide mode: webhook or polling
|
| 115 |
+
if telegramCfg.WebhookEnabled && telegramCfg.WebhookURL != "" {
|
| 116 |
+
return c.startWebhook(ctx)
|
| 117 |
+
}
|
| 118 |
+
return c.startPolling(ctx)
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// startPolling runs long polling mode (original behavior)
|
| 122 |
+
func (c *TelegramChannel) startPolling(ctx context.Context) error {
|
| 123 |
logger.InfoC("telegram", "Starting Telegram bot (polling mode)...")
|
| 124 |
|
| 125 |
updates, err := c.bot.UpdatesViaLongPolling(ctx, &telego.GetUpdatesParams{
|
|
|
|
| 154 |
})
|
| 155 |
}
|
| 156 |
|
| 157 |
+
// Command handlers (same as before)
|
| 158 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 159 |
c.commands.Help(ctx, message)
|
| 160 |
return nil
|
| 161 |
}, th.CommandEqual("help"))
|
| 162 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 163 |
+
c.commands.Start(ctx, message)
|
| 164 |
+
return nil
|
| 165 |
}, th.CommandEqual("start"))
|
|
|
|
| 166 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 167 |
+
c.commands.Ping(ctx, message)
|
| 168 |
+
return nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
}, th.CommandEqual("ping"))
|
|
|
|
| 170 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 171 |
+
c.commands.ID(ctx, message)
|
| 172 |
+
return nil
|
| 173 |
}, th.CommandEqual("id"))
|
|
|
|
| 174 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 175 |
+
c.commands.Status(ctx, message)
|
| 176 |
+
return nil
|
| 177 |
}, th.CommandEqual("status"))
|
|
|
|
| 178 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 179 |
+
c.commands.IP(ctx, message)
|
| 180 |
+
return nil
|
| 181 |
}, th.CommandEqual("ip"))
|
|
|
|
| 182 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 183 |
+
c.commands.Clear(ctx, message)
|
| 184 |
+
return nil
|
| 185 |
}, th.CommandEqual("clear"))
|
| 186 |
+
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 187 |
+
c.commands.Show(ctx, message)
|
| 188 |
+
return nil
|
| 189 |
+
}, th.CommandEqual("show"))
|
| 190 |
+
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 191 |
+
c.commands.List(ctx, message)
|
| 192 |
+
return nil
|
| 193 |
+
}, th.CommandEqual("list"))
|
| 194 |
|
| 195 |
+
// Handle all other messages
|
| 196 |
bh.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
| 197 |
return c.handleMessage(ctx, &message)
|
| 198 |
}, th.AnyMessage())
|
| 199 |
|
| 200 |
+
// Callback query handler
|
| 201 |
+
bh.HandleCallbackQuery(func(ctx *th.Context, callbackQuery telego.CallbackQuery) error {
|
| 202 |
+
return c.handleCallbackQuery(ctx, &callbackQuery)
|
| 203 |
+
})
|
| 204 |
+
|
| 205 |
c.setRunning(true)
|
| 206 |
logger.InfoCF("telegram", "Telegram bot connected", map[string]interface{}{
|
| 207 |
"username": c.bot.Username(),
|
|
|
|
| 216 |
|
| 217 |
return nil
|
| 218 |
}
|
| 219 |
+
|
| 220 |
+
// startWebhook sets up an HTTP server to receive Telegram webhooks
|
| 221 |
+
func (c *TelegramChannel) startWebhook(ctx context.Context) error {
|
| 222 |
+
telegramCfg := c.config.Channels.Telegram
|
| 223 |
+
webhookPath := telegramCfg.WebhookPath
|
| 224 |
+
if webhookPath == "" {
|
| 225 |
+
webhookPath = "/telegram/webhook"
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
// Create context for webhook server
|
| 229 |
+
c.webhookCtx, c.webhookCancel = context.WithCancel(ctx)
|
| 230 |
+
|
| 231 |
+
// Create HTTP server
|
| 232 |
+
c.httpServer = &http.Server{
|
| 233 |
+
Addr: fmt.Sprintf("%s:%d", c.config.Gateway.Host, c.config.Gateway.Port),
|
| 234 |
+
Handler: http.DefaultServeMux,
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
// Register webhook endpoint
|
| 238 |
+
http.HandleFunc(webhookPath, c.webhookHandler)
|
| 239 |
+
|
| 240 |
+
// Set webhook with Telegram
|
| 241 |
+
webhookURL := telegramCfg.WebhookURL + webhookPath
|
| 242 |
+
params := &telego.SetWebhookParams{
|
| 243 |
+
URL: webhookURL,
|
| 244 |
+
}
|
| 245 |
+
if telegramCfg.WebhookSecret != "" {
|
| 246 |
+
params.SecretToken = telegramCfg.WebhookSecret
|
| 247 |
+
}
|
| 248 |
+
err := c.bot.SetWebhook(c.webhookCtx, params)
|
| 249 |
+
if err != nil {
|
| 250 |
+
return fmt.Errorf("failed to set webhook: %w", err)
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
logger.InfoCF("telegram", "Webhook registered", map[string]interface{}{
|
| 254 |
+
"url": webhookURL,
|
| 255 |
+
})
|
| 256 |
+
|
| 257 |
+
// Start HTTP server in goroutine
|
| 258 |
+
go func() {
|
| 259 |
+
logger.InfoC("telegram", "Starting webhook server...")
|
| 260 |
+
if err := c.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
| 261 |
+
logger.ErrorCF("telegram", "Webhook server error", map[string]interface{}{
|
| 262 |
+
"error": err.Error(),
|
| 263 |
+
})
|
| 264 |
+
}
|
| 265 |
+
}()
|
| 266 |
+
|
| 267 |
+
c.setRunning(true)
|
| 268 |
+
logger.InfoCF("telegram", "Telegram bot running in webhook mode", map[string]interface{}{
|
| 269 |
+
"username": c.bot.Username(),
|
| 270 |
+
})
|
| 271 |
+
|
| 272 |
+
// Wait for context cancellation
|
| 273 |
+
go func() {
|
| 274 |
+
<-c.webhookCtx.Done()
|
| 275 |
+
c.Stop(context.Background())
|
| 276 |
+
}()
|
| 277 |
+
|
| 278 |
+
return nil
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
// webhookHandler handles incoming Telegram webhook requests
|
| 282 |
+
func (c *TelegramChannel) webhookHandler(w http.ResponseWriter, r *http.Request) {
|
| 283 |
+
if r.Method != http.MethodPost {
|
| 284 |
+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
| 285 |
+
return
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// Verify secret token if configured
|
| 289 |
+
telegramCfg := c.config.Channels.Telegram
|
| 290 |
+
if telegramCfg.WebhookSecret != "" {
|
| 291 |
+
secret := r.Header.Get("X-Telegram-Bot-Api-Secret-Token")
|
| 292 |
+
if secret != telegramCfg.WebhookSecret {
|
| 293 |
+
http.Error(w, "Invalid secret token", http.StatusUnauthorized)
|
| 294 |
+
return
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
// Decode update
|
| 299 |
+
var update telego.Update
|
| 300 |
+
if err := json.NewDecoder(r.Body).Decode(&update); err != nil {
|
| 301 |
+
http.Error(w, "Invalid update", http.StatusBadRequest)
|
| 302 |
+
return
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
// Process update asynchronously (don't block webhook response)
|
| 306 |
+
go c.processUpdate(c.webhookCtx, &update)
|
| 307 |
+
|
| 308 |
+
w.WriteHeader(http.StatusOK)
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
// processUpdate routes an update to appropriate handlers
|
| 312 |
+
func (c *TelegramChannel) processUpdate(ctx context.Context, update *telego.Update) {
|
| 313 |
+
if update.Message != nil {
|
| 314 |
+
c.handleMessage(ctx, update.Message)
|
| 315 |
+
} else if update.CallbackQuery != nil {
|
| 316 |
+
c.handleCallbackQuery(ctx, update.CallbackQuery)
|
| 317 |
+
}
|
| 318 |
+
// Add other update types (edited_message, channel_post, etc.) as needed
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
// handleMessage processes an incoming message (similar to polling mode)
|
| 322 |
+
func (c *TelegramChannel) handleMessage(ctx context.Context, message *telego.Message) {
|
| 323 |
+
// This mirrors the logic in telegohandler's message handling
|
| 324 |
+
// We'll directly use the commands router
|
| 325 |
+
|
| 326 |
+
// Convert to th.Context for compatibility? Actually we can call commands directly.
|
| 327 |
+
// For simplicity, we'll route based on command text.
|
| 328 |
+
|
| 329 |
+
text := message.Text
|
| 330 |
+
if text == "" {
|
| 331 |
+
// Could be other message types (voice, photo, etc.)
|
| 332 |
+
// For now, only handle text messages
|
| 333 |
+
return
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
// Check if it's a command
|
| 337 |
+
if strings.HasPrefix(text, "/") {
|
| 338 |
+
parts := strings.Fields(text)
|
| 339 |
+
command := strings.TrimPrefix(parts[0], "/")
|
| 340 |
+
|
| 341 |
+
// Route to command
|
| 342 |
+
switch command {
|
| 343 |
+
case "start":
|
| 344 |
+
c.commands.Start(ctx, *message)
|
| 345 |
+
case "help":
|
| 346 |
+
c.commands.Help(ctx, *message)
|
| 347 |
+
case "ping":
|
| 348 |
+
c.commands.Ping(ctx, *message)
|
| 349 |
+
case "id":
|
| 350 |
+
c.commands.ID(ctx, *message)
|
| 351 |
+
case "status":
|
| 352 |
+
c.commands.Status(ctx, *message)
|
| 353 |
+
case "ip":
|
| 354 |
+
c.commands.IP(ctx, *message)
|
| 355 |
+
case "clear":
|
| 356 |
+
c.commands.Clear(ctx, *message)
|
| 357 |
+
case "show":
|
| 358 |
+
c.commands.Show(ctx, *message)
|
| 359 |
+
case "list":
|
| 360 |
+
c.commands.List(ctx, *message)
|
| 361 |
+
default:
|
| 362 |
+
// Unknown command - could send help
|
| 363 |
+
c.sendToAgent(ctx, message.Chat.ID, "Unknown command. Type /help for available commands.", nil, nil)
|
| 364 |
+
}
|
| 365 |
+
} else {
|
| 366 |
+
// Non-command message - forward to agent
|
| 367 |
+
c.sendToAgent(ctx, message.Chat.ID, text, nil, message)
|
| 368 |
+
}
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
// handleCallbackQuery processes callback queries (from inline buttons)
|
| 372 |
+
func (c *TelegramChannel) handleCallbackQuery(ctx context.Context, callback *telego.CallbackQuery) {
|
| 373 |
+
// For now, just answer the callback to remove loading state
|
| 374 |
+
// TODO: Implement proper callback handling based on data
|
| 375 |
+
c.bot.AnswerCallbackQuery(ctx, &telego.AnswerCallbackQueryParams{
|
| 376 |
+
CallbackQueryID: callback.ID,
|
| 377 |
+
})
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
// sendToAgent forwards a text message to the agent bus
|
| 381 |
+
func (c *TelegramChannel) sendToAgent(ctx context.Context, chatID int64, text string, mediaPaths []string, originalMsg *telego.Message) {
|
| 382 |
+
// Build outbound message
|
| 383 |
+
msg := bus.OutboundMessage{
|
| 384 |
+
Channel: "telegram",
|
| 385 |
+
ChatID: fmt.Sprintf("%d", chatID),
|
| 386 |
+
Content: text,
|
| 387 |
+
MediaPaths: mediaPaths,
|
| 388 |
+
Metadata: make(map[string]string),
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
// Publish to bus (non-blocking)
|
| 392 |
+
go func() {
|
| 393 |
+
if err := c.bus.Publish(ctx, msg); err != nil {
|
| 394 |
+
logger.ErrorCF("telegram", "Failed to publish message to bus", map[string]interface{}{
|
| 395 |
+
"error": err.Error(),
|
| 396 |
+
"chat_id": chatID,
|
| 397 |
+
})
|
| 398 |
+
}
|
| 399 |
+
}()
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
// Stop gracefully shuts down the Telegram channel
|
| 403 |
func (c *TelegramChannel) Stop(ctx context.Context) error {
|
| 404 |
logger.InfoC("telegram", "Stopping Telegram bot...")
|
| 405 |
+
|
| 406 |
+
// Cancel webhook context if active
|
| 407 |
+
if c.webhookCancel != nil {
|
| 408 |
+
c.webhookCancel()
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
// Shutdown HTTP server if running
|
| 412 |
+
if c.httpServer != nil {
|
| 413 |
+
shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
| 414 |
+
defer cancel()
|
| 415 |
+
if err := c.httpServer.Shutdown(shutdownCtx); err != nil {
|
| 416 |
+
logger.WarnCF("telegram", "HTTP server shutdown error", map[string]interface{}{
|
| 417 |
+
"error": err.Error(),
|
| 418 |
+
})
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
// Delete webhook if it was set
|
| 423 |
+
if c.config.Channels.Telegram.WebhookEnabled {
|
| 424 |
+
_, _ = c.bot.DeleteWebhook(ctx, &telego.DeleteWebhookParams{
|
| 425 |
+
// Drop pending updates? Maybe not needed
|
| 426 |
+
})
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
c.setRunning(false)
|
| 430 |
return nil
|
| 431 |
}
|
| 432 |
|
| 433 |
+
// ----------------------------------------------------------------------------
|
| 434 |
+
// The rest of the file remains unchanged from the original telegram.go
|
| 435 |
+
// (Send method and other helper functions)
|
| 436 |
+
// ----------------------------------------------------------------------------
|
| 437 |
+
|
| 438 |
func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
| 439 |
if !c.IsRunning() {
|
| 440 |
return fmt.Errorf("telegram bot not running")
|
|
|
|
| 566 |
if msg.AudioPath != "" && (totalParts > 1 || len(markdownToTelegramHTML(msg.Content)) > 900) {
|
| 567 |
file, err := os.Open(msg.AudioPath)
|
| 568 |
if err != nil {
|
| 569 |
+
logger.ErrorCF("telegram", "Failed to open audio file for separate send", map[string]interface{}{
|
| 570 |
"path": msg.AudioPath,
|
| 571 |
"error": err.Error(),
|
| 572 |
})
|
| 573 |
+
return err
|
| 574 |
+
}
|
| 575 |
+
defer file.Close()
|
| 576 |
+
|
| 577 |
+
voiceMsg := tu.Voice(tu.ID(chatID), tu.File(file))
|
| 578 |
+
// No caption for separate audio
|
| 579 |
+
_, err = c.bot.SendVoice(ctx, voiceMsg)
|
| 580 |
+
if err != nil {
|
| 581 |
+
logger.ErrorCF("telegram", "Failed to send separate voice note", map[string]interface{}{
|
| 582 |
+
"error": err.Error(),
|
| 583 |
+
})
|
| 584 |
+
return err
|
| 585 |
}
|
| 586 |
}
|
| 587 |
|
| 588 |
return nil
|
| 589 |
}
|
| 590 |
|
| 591 |
+
// ----------------------------------------------------------------------------
|
| 592 |
+
// HELPER FUNCTIONS (unchanged)
|
| 593 |
+
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
|
| 595 |
+
func parseChatID(id string) (int64, error) {
|
| 596 |
+
var result int64
|
| 597 |
+
_, err := fmt.Sscanf(id, "%d", &result)
|
| 598 |
+
return result, err
|
| 599 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
|
| 601 |
+
// SplitMessage splits a message into chunks respecting Telegram limits
|
| 602 |
+
func SplitMessage(text string, maxLen int) []string {
|
| 603 |
+
if len(text) <= maxLen {
|
| 604 |
+
return []string{text}
|
| 605 |
}
|
| 606 |
|
| 607 |
+
var chunks []string
|
| 608 |
+
lines := strings.Split(text, "\n")
|
| 609 |
+
var currentChunk strings.Builder
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
+
for _, line := range lines {
|
| 612 |
+
// If adding this line would exceed limit, start new chunk
|
| 613 |
+
if currentChunk.Len()+len(line)+1 > maxLen {
|
| 614 |
+
if currentChunk.Len() > 0 {
|
| 615 |
+
chunks = append(chunks, currentChunk.String())
|
| 616 |
+
currentChunk.Reset()
|
|
|
|
|
|
|
| 617 |
}
|
| 618 |
+
// If single line is longer than maxLen, split it
|
| 619 |
+
if len(line) > maxLen {
|
| 620 |
+
for i := 0; i < len(line); i += maxLen {
|
| 621 |
+
end := i + maxLen
|
| 622 |
+
if end > len(line) {
|
| 623 |
+
end = len(line)
|
| 624 |
+
}
|
| 625 |
+
chunks = append(chunks, line[i:end])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
}
|
| 627 |
+
continue
|
|
|
|
| 628 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
}
|
| 630 |
+
if currentChunk.Len() > 0 {
|
| 631 |
+
currentChunk.WriteString("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
}
|
| 633 |
+
currentChunk.WriteString(line)
|
| 634 |
}
|
| 635 |
|
| 636 |
+
if currentChunk.Len() > 0 {
|
| 637 |
+
chunks = append(chunks, currentChunk.String())
|
| 638 |
}
|
| 639 |
|
| 640 |
+
return chunks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
}
|
| 642 |
|
| 643 |
+
// markdownToTelegramHTML converts Markdown to Telegram HTML (basic)
|
| 644 |
func markdownToTelegramHTML(text string) string {
|
| 645 |
+
// Escape HTML special chars first
|
| 646 |
+
text = strings.ReplaceAll(text, "&", "&")
|
| 647 |
+
text = strings.ReplaceAll(text, "<", "<")
|
| 648 |
+
text = strings.ReplaceAll(text, ">", ">")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
+
// Convert Markdown bold: **text** -> <b>text</b>
|
| 651 |
+
reBold := regexp.MustCompile(`\*\*(.*?)\*\*`)
|
| 652 |
+
text = reBold.ReplaceAllString(text, "<b>$1</b>")
|
| 653 |
|
| 654 |
+
// Convert Markdown italic: *text* -> <i>text</i>
|
| 655 |
+
// But careful: we already used * for bold, so handle single asterisks that aren't part of **
|
| 656 |
+
// Simple approach: replace *text* but not **
|
| 657 |
+
reItalic := regexp.MustCompile(`(?<!\*)\*(?!\*)(.*?)(?<!\*)\*(?!\*)`)
|
| 658 |
+
text = reItalic.ReplaceAllString(text, "<i>$1</i>")
|
| 659 |
|
| 660 |
+
// Convert inline code: `text` -> <code>text</code>
|
| 661 |
+
reCode := regexp.MustCompile(``(.*?)``)
|
| 662 |
+
text = reCode.ReplaceAllString(text, "<code>$1</code>")
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
+
// Convert links: [text](url) -> <a href="url">text</a>
|
| 665 |
+
reLink := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
|
| 666 |
+
text = reLink.ReplaceAllString(text, `<a href="$2">$1</a>`)
|
| 667 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
return text
|
| 669 |
}
|
pkg/config/config.go
CHANGED
|
@@ -113,6 +113,11 @@ type TelegramConfig struct {
|
|
| 113 |
Token string `json:"token" env:"PICOCLAW_CHANNELS_TELEGRAM_TOKEN"`
|
| 114 |
Proxy string `json:"proxy" env:"PICOCLAW_CHANNELS_TELEGRAM_PROXY"`
|
| 115 |
AllowFrom FlexibleStringSlice `json:"allow_from" env:"PICOCLAW_CHANNELS_TELEGRAM_ALLOW_FROM"`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
type FeishuConfig struct {
|
|
|
|
| 113 |
Token string `json:"token" env:"PICOCLAW_CHANNELS_TELEGRAM_TOKEN"`
|
| 114 |
Proxy string `json:"proxy" env:"PICOCLAW_CHANNELS_TELEGRAM_PROXY"`
|
| 115 |
AllowFrom FlexibleStringSlice `json:"allow_from" env:"PICOCLAW_CHANNELS_TELEGRAM_ALLOW_FROM"`
|
| 116 |
+
// Webhook configuration for environments with restricted egress (e.g., Hugging Face Spaces)
|
| 117 |
+
WebhookEnabled bool `json:"webhook_enabled" env:"PICOCLAW_CHANNELS_TELEGRAM_WEBHOOK_ENABLED"`
|
| 118 |
+
WebhookURL string `json:"webhook_url" env:"PICOCLAW_CHANNELS_TELEGRAM_WEBHOOK_URL"`
|
| 119 |
+
WebhookSecret string `json:"webhook_secret" env:"PICOCLAW_CHANNELS_TELEGRAM_WEBHOOK_SECRET"`
|
| 120 |
+
WebhookPath string `json:"webhook_path" env:"PICOCLAW_CHANNELS_TELEGRAM_WEBHOOK_PATH"`
|
| 121 |
}
|
| 122 |
|
| 123 |
type FeishuConfig struct {
|