tgonebot

package module
v0.0.0-...-ffb94d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 15 Imported by: 0

README

TG-OneBot

TG-OneBot 将 Telegram Bot API 适配为 OneBot 12,并同时支持两种接入方式:

  • 独立服务:作为单独进程启动,对外暴露 OneBot HTTP / WebSocket。
  • Go 模块:作为 tgonebot 包被业务程序直接导入,和业务进程共享生命周期。

这次迭代的取舍

这两种接入方式是合理的,而且应该共用同一套核心实现。独立服务适合多语言业务或进程隔离场景;嵌入模式适合纯 Go 项目,减少额外部署面和进程通信成本。相反,把两套启动逻辑分别维护是不合理的,因此当前版本把核心逻辑上提为库,CLI 只保留薄包装。

同时,仓库已移除部署配置、部署文档和已提交的真实凭据。Telegram 凭据不再写入 config.json,而是在运行时注入。

快速开始

配置文件

复制示例配置:

cp config.example.json config.json

config.json 只保留非敏感运行配置,例如代理、日志级别和 OneBot 通信监听参数。

方式一:独立服务

构建并启动:

go build -o tg-onebot ./cmd/tg-onebot
TG_ONEBOT_TELEGRAM_TOKEN=... ./tg-onebot -config config.json
方式二:作为 Go 模块嵌入
package main

import (
	"context"
	"log"
	"os"

	"github.com/DnullP/tgonebot"
	"github.com/DnullP/tgonebot/config"
)

func main() {
	cfg, err := config.LoadConfig("config.json")
	if err != nil {
		log.Fatal(err)
	}

	service, err := tgonebot.New(*cfg, tgonebot.Options{
		TelegramToken: os.Getenv("TG_ONEBOT_TELEGRAM_TOKEN"),
		ServiceName:   "my-app-bot-bridge",
	})
	if err != nil {
		log.Fatal(err)
	}

	if err := service.Run(context.Background()); err != nil {
		log.Fatal(err)
	}
}

完整可运行示例见 examples/sdk

对外 API

根包提供以下核心接口:

  • tgonebot.New(config.Config, tgonebot.Options):创建服务实例。
  • (*Service).Start():启动 OneBot 与 Telegram 桥接。
  • (*Service).Run(context.Context):在上下文生命周期内运行。
  • (*Service).Close():优雅关闭。
  • (*Service).Bot() / (*Service).OneBot():访问底层实例,便于业务侧继续扩展。

目录结构

tg-onebot/
├── adapter/           # Telegram <-> OneBot 转换逻辑
├── cmd/tg-onebot/     # 独立服务入口
├── config/            # 非敏感配置加载
├── examples/          # 独立服务 / SDK 示例
├── service.go         # 可复用服务入口
└── README.md

运行与测试

go test ./...
go build ./cmd/tg-onebot

说明

  • 仓库不再内置 Docker、Compose、Kubernetes 等部署资产。
  • 仓库不再保存 Telegram 凭据或其他真实敏感信息。
  • 如需部署,应由接入方在自己的基础设施仓库中编排,而不是放在 SDK 仓库中。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAllowedUpdates

func DefaultAllowedUpdates() []string

DefaultAllowedUpdates 返回默认订阅的 Telegram 更新类型。

func NewHTTPClient

func NewHTTPClient(proxy string) (*http.Client, error)

NewHTTPClient 根据代理配置创建 HTTP 客户端。

Types

type Options

type Options struct {
	TelegramToken  string
	Logger         *logrus.Logger
	HTTPClient     *http.Client
	AllowedUpdates []string
	ServiceName    string
}

Options 定义服务初始化时的运行参数。

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service 封装 Telegram Bot 与 OneBot 桥接服务。

func New

func New(cfg config.Config, opts Options) (*Service, error)

New 创建一个可复用的 TG-OneBot 服务实例。

func (*Service) Bot

func (s *Service) Bot() *bot.Bot

Bot 返回底层 Telegram Bot 实例。

func (*Service) Close

func (s *Service) Close()

Close 停止桥接服务。

func (*Service) Logger

func (s *Service) Logger() *logrus.Logger

Logger 返回服务日志实例。

func (*Service) OneBot

func (s *Service) OneBot() *libonebot.OneBot

OneBot 返回底层 OneBot 实例。

func (*Service) Run

func (s *Service) Run(ctx context.Context) error

Run 在给定上下文生命周期内运行服务。

func (*Service) Start

func (s *Service) Start()

Start 启动桥接服务。

Directories

Path Synopsis
examples
sdk command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL