Tencent/WeKnora · error

parse telegram credentials: %w

Error message

parse telegram credentials: %w

What it means

The Telegram adapter factory parses channel.Credentials via im.ParseCredentials before building the bot client; malformed credentials JSON/structure causes construction to fail with this wrapped error, preserving the underlying parse error via %w.

Source

Thrown at internal/im/telegram/factory.go:17

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 {

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Ensure channel.Credentials is valid JSON, e.g. {"bot_token":"123:ABC"}
  2. Check the wrapped inner error (errors.Unwrap / %v of the cause) for the exact parse failure
  3. Fix the secret/config source so it emits a JSON object, not a bare token

Example fix

// before
credentials: "123456:ABC-DEF"
// after
credentials: "{\"bot_token\":\"123456:ABC-DEF\"}"
Defensive patterns

Strategy: validation

Validate before calling

var creds map[string]string
if err := json.Unmarshal([]byte(channel.Credentials), &creds); err != nil {
    return fmt.Errorf("telegram credentials must be a JSON object: %w", err)
}
if creds["bot_token"] == "" {
    return errors.New("telegram credentials missing bot_token")
}

Try / catch

adapter, cancel, err := telegram.NewFactory()(ctx, ch, handler)
if err != nil {
    var perr error
    if errors.As(err, &perr) { /* inspect unwrapped parse cause */ }
    log.Printf("telegram channel %s: %v", ch.ID, err)
}

Prevention

When it happens

Trigger: Creating a Telegram channel whose Credentials field is not valid JSON (or not the expected string map) — e.g. empty credentials, single-quoted JSON, or a non-JSON secret value.

Common situations: Pasting a bot token with stray whitespace/newlines into a non-JSON field; storing credentials as YAML instead of JSON; migrating channels where the credentials column was left empty; secrets manager returning a raw token instead of a JSON object.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/4214bb7dd910b927. Report an issue: GitHub.