Files
sam 86501fa1d4 feat(messaging): run Telegram as an archipelago channel extension (#1399)
The bundled internal/channels/telegram package (17 files) is deleted; the
channel now ships as archipelago/channels/telegram, launched by archiemessaging
through the existing channel-extension machinery. An existing [chat.telegram]
config enables it with the same keys: the token stays a secret reference
(token_ref) the host resolves, allowed_user_ids and show_tool_calls map onto
extension settings. Per-org bots compose the same extension per org from the
org's credential binding. Reload support is now derived from a channels.Reloader
interface instead of a *telegram.Gateway assertion. The five host-only seams
(/update, release announcements, running versions, in-place show_tool_calls,
reload) are dropped from the channel and need a contract home or deletion.
2026-10-31 15:30:27 +11:00

150 lines
4.5 KiB
Go

package main
import (
"context"
"fmt"
"io"
"net/http"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
gatewayv1 "github.com/samcharles93/archipelago/sdk/gateway/v1"
)
// attachment is one inbound file Telegram carries.
type attachment struct {
fileID, kind, mime, name, note string
}
// inboundAttachment maps a Telegram message's file onto an attachment. A
// message carries at most one file; a photo collapses to its largest size.
func inboundAttachment(msg *models.Message) (attachment, bool) {
switch {
case len(msg.Photo) > 0:
largest := msg.Photo[len(msg.Photo)-1]
for _, size := range msg.Photo {
if size.FileSize >= largest.FileSize {
largest = size
}
}
return attachment{fileID: largest.FileID, kind: "image", mime: "image/jpeg", note: "[photo]"}, true
case msg.Document != nil:
return attachment{
fileID: msg.Document.FileID, kind: "document", mime: msg.Document.MimeType,
name: msg.Document.FileName, note: "[document: " + msg.Document.FileName + "]",
}, true
case msg.Video != nil:
return attachment{fileID: msg.Video.FileID, kind: "video", mime: msg.Video.MimeType, note: "[video]"}, true
case msg.VideoNote != nil:
return attachment{fileID: msg.VideoNote.FileID, kind: "video", mime: "video/mp4", note: "[video message]"}, true
case msg.Voice != nil:
return attachment{fileID: msg.Voice.FileID, kind: "audio", mime: msg.Voice.MimeType, note: "[voice message]"}, true
case msg.Audio != nil:
return attachment{
fileID: msg.Audio.FileID, kind: "audio", mime: msg.Audio.MimeType,
name: msg.Audio.FileName, note: "[audio: " + msg.Audio.FileName + "]",
}, true
}
return attachment{}, false
}
// download fetches an attachment's bytes through the Bot API file link. The
// gateway process never holds the bot token, so the bytes ride on the inbound
// message.
func (g *gateway) download(ctx context.Context, fileID string) ([]byte, error) {
file, err := g.bot.GetFile(ctx, &bot.GetFileParams{FileID: fileID})
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, g.bot.FileDownloadLink(file), nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("telegram: download %s: %s", fileID, resp.Status)
}
return io.ReadAll(io.LimitReader(resp.Body, maxAttachmentBytes))
}
// maxAttachmentBytes bounds one inbound attachment.
const maxAttachmentBytes = 20 << 20
// sendMedia delivers an outbound media event: photos and documents by file id
// or url, anything else as a link.
func (g *gateway) sendMedia(ctx context.Context, chatID int64, threadID int, media *gatewayv1.Media) {
if media == nil {
return
}
var file any
switch {
case media.GetFileId() != "":
file = models.InputFileString{Data: media.GetFileId()}
case media.GetUrl() != "":
file = models.InputFileString{Data: media.GetUrl()}
}
send := func(err error) {
if err != nil {
g.log.WarnContext(ctx, "send media failed", "type", media.GetType(), "error", err)
}
}
switch media.GetType() {
case "image":
if file == nil {
g.sendText(ctx, chatID, threadID, media.GetUrl())
return
}
params := &bot.SendPhotoParams{ChatID: chatID, Photo: file.(models.InputFile)}
if threadID != 0 {
params.MessageThreadID = threadID
}
if media.GetFileName() != "" {
params.Caption = media.GetFileName()
}
_, err := g.bot.SendPhoto(ctx, params)
send(err)
case "video":
if file == nil {
g.sendText(ctx, chatID, threadID, media.GetUrl())
return
}
params := &bot.SendVideoParams{ChatID: chatID, Video: file.(models.InputFile)}
if threadID != 0 {
params.MessageThreadID = threadID
}
_, err := g.bot.SendVideo(ctx, params)
send(err)
case "audio":
if file == nil {
g.sendText(ctx, chatID, threadID, media.GetUrl())
return
}
params := &bot.SendAudioParams{ChatID: chatID, Audio: file.(models.InputFile)}
if threadID != 0 {
params.MessageThreadID = threadID
}
_, err := g.bot.SendAudio(ctx, params)
send(err)
default:
if file == nil {
if media.GetUrl() != "" {
g.sendText(ctx, chatID, threadID, media.GetUrl())
} else if media.GetPath() != "" {
g.sendText(ctx, chatID, threadID, media.GetPath())
}
return
}
params := &bot.SendDocumentParams{ChatID: chatID, Document: file.(models.InputFile)}
if threadID != 0 {
params.MessageThreadID = threadID
}
_, err := g.bot.SendDocument(ctx, params)
send(err)
}
}