feat(channels/telegram): Telegram 渠道插件(Bot API 长轮询/Webhook + file_id 媒体 + 主动推送强)
This commit is contained in:
1 parent
0b036645bb
commit
f9f5a78a02
12 files changed
+1983
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
|
||||
)
|
||||
|
||||
// discardLogger returns a silent, source-tagged logger for tests.
|
||||
func discardLogger() *slog.Logger {
|
||||
return logger.WithSource(logger.New(logger.Options{Out: io.Discard}), "plugins/channels/telegram")
|
||||
}
|
||||
|
||||
// newTestAdapter wires a Telegram adapter to an httptest server whose handler
|
||||
// answers the Bot API methods. The server is closed on test cleanup.
|
||||
func newTestAdapter(t *testing.T, h http.Handler) (*Adapter, *httptest.Server) {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(h)
|
||||
t.Cleanup(srv.Close)
|
||||
a := New(
|
||||
Config{BotToken: "123456:TESTTOKEN"},
|
||||
WithBaseURL(srv.URL),
|
||||
WithHTTPClient(srv.Client()),
|
||||
WithLogger(discardLogger()),
|
||||
)
|
||||
return a, srv
|
||||
}
|
||||
|
||||
// methodOf extracts the Bot API method name from the request path, which takes
|
||||
// the form /bot<token>/<method>.
|
||||
func methodOf(r *http.Request) string {
|
||||
parts := strings.Split(r.URL.Path, "/")
|
||||
return parts[len(parts)-1]
|
||||
}
|
||||
|
||||
// decodeBody unmarshals an incoming JSON request body into a map for assertions.
|
||||
func decodeBody(r *http.Request) map[string]any {
|
||||
var m map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// okResponse writes a generic Bot API success envelope with the given result.
|
||||
func okResponse(w http.ResponseWriter, result any) {
|
||||
body := map[string]any{"ok": true, "result": result}
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
}
|
||||
|
||||
// failResponse writes a Bot API error envelope.
|
||||
func failResponse(w http.ResponseWriter, desc string, code int) {
|
||||
body := map[string]any{"ok": false, "description": desc, "error_code": code}
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
}
|
||||
Reference in new issue
Block a user