b2975f7d1d
registerIMAdapterFactories in internal/container/container.go had grown
to ~290 lines of platform-specific config parsing and runtime start/stop
logic across 7 IM platforms, plus three private credential helpers. The
container package is meant to wire dependencies, not own each platform's
credentials schema and websocket lifecycle.
Extract each platform's factory into its own subpackage file and expose
the credential helpers on the im package:
- New im.ParseCredentials / im.GetString / im.GetBool (formerly the
unexported parseCredentials / getString / credentialBool in container).
- New im.ResolveMode helper collapses the six identical "default to
websocket when Mode is empty" blocks.
- New {wecom,feishu,slack,telegram,dingtalk,mattermost,wechat}.NewFactory
returns an im.AdapterFactory; container.go now calls each one in a
single line.
Behavior is preserved: adapter construction, goroutine start/stop,
log messages, default modes (mattermost still defaults to "webhook";
others to "websocket"), and error strings are unchanged. The WeCom
corp_agent_id float64/int switch and the WeChat long-poll-only path
are preserved as-is.
Net: container.go drops ~320 lines and no longer needs to be touched
when adding or tuning an IM platform.
48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Tencent/WeKnora/internal/im"
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
)
|
|
|
|
// NewFactory returns an im.AdapterFactory for Telegram bot channels.
|
|
// Supports "webhook" and "websocket" (long polling, default).
|
|
func NewFactory() im.AdapterFactory {
|
|
return func(factoryCtx context.Context, channel *im.IMChannel, msgHandler func(context.Context, *im.IncomingMessage) error) (im.Adapter, context.CancelFunc, error) {
|
|
creds, err := im.ParseCredentials(channel.Credentials)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("parse telegram credentials: %w", err)
|
|
}
|
|
|
|
botToken := im.GetString(creds, "bot_token")
|
|
|
|
mode := im.ResolveMode(channel, "websocket")
|
|
|
|
switch mode {
|
|
case "webhook":
|
|
secretToken := im.GetString(creds, "secret_token")
|
|
adapter := NewWebhookAdapter(botToken, secretToken)
|
|
return adapter, nil, nil
|
|
|
|
case "websocket":
|
|
client := NewLongConnClient(botToken, msgHandler)
|
|
|
|
wsCtx, wsCancel := context.WithCancel(context.Background())
|
|
go func() {
|
|
if err := client.Start(wsCtx); err != nil && wsCtx.Err() == nil {
|
|
logger.Errorf(context.Background(), "[IM] Telegram long polling stopped for channel %s: %v", channel.ID, err)
|
|
}
|
|
}()
|
|
|
|
adapter := NewAdapter(client, botToken)
|
|
return adapter, wsCancel, nil
|
|
|
|
default:
|
|
return nil, nil, fmt.Errorf("unsupported telegram mode: %s", mode)
|
|
}
|
|
}
|
|
}
|