init: 初始化 web_hgdjb_server 项目

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-11-07 10:47:32 +08:00
co-authored by Claude Opus 4.7
commit f13d33af4d
707 changed files with 147705 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
package tgbot
import (
"videoweb/pub/common/srvdef"
)
//获取群聊id
//https://api.telegram.org/bot1803190372:AAGmrrN6xUoh7IKiv4qCVJYdCs8wLt1ffCw/getUpdates
var (
//配置
//zhihu app机器人 2048234434:AAF3XoW4iE6vAOmttTT_bofgan_PleKuaEk
//zhihu web机器人 1973289619:AAHjCZ8KQOewVfr20foHKlKVwJoulrf_OFc
tgToken = "2048234434:AAF3XoW4iE6vAOmttTT_bofgan_PleKuaEk"
//程序支付监控群tgids
programTgGroupIds = []int64{-778146822}
//天行监控组tgids
monitorTgGroupIds = []int64{-1001373963166}
//管理人员tgid(yl:1652126945)
adminTgIds = []int64{1652126945}
prefix = "[汁乎App]"
tgBot *TgBot = nil
)
func InitTgBot(token string, prefixStr string) *srvdef.SrvErr {
if tgBot != nil {
return nil
}
if prefixStr != "" {
prefix = prefixStr
}
tgBot = NewTelegramBot(token)
if serr := tgBot.Init(); nil != serr {
return serr
}
//SendToAll("支付监控测试", "Test")
return nil
}
func ReleaseTgBot() {
if tgBot == nil {
return
}
tgBot.Release()
}
// SendToAdmins 发到管理人员
func SendToAdmins(title string, info string) {
sendTo(adminTgIds, title, info)
}
// SendToProgram 发到程序
func SendToProgram(title string, info string) {
sendTo(programTgGroupIds, title, info)
}
// SendToMonitor 发到监控群组
func SendToMonitor(title string, info string) {
sendTo(monitorTgGroupIds, title, info)
}
// SendToProgram 发到所有
func SendToAll(title string, info string) {
groupIds := programTgGroupIds
groupIds = append(groupIds, monitorTgGroupIds...)
//groupIds = append(groupIds, adminTgIds...)
sendTo(groupIds, title, info)
}
func sendTo(ids []int64, title string, info string) {
if tgBot == nil {
return
}
//异步发送
go func() {
msg := prefix + "\n" + title + "\n" + info
tgBot.Send(ids, msg)
}()
}
func RegisterCommands(commands []*BotCmd) {
if tgBot == nil {
return
}
tgBot.RegisterCommands(commands)
}
func Receive() {
if tgBot == nil {
return
}
tgBot.Receive()
}
+169
View File
@@ -0,0 +1,169 @@
package tgbot
import (
"fmt"
"time"
"videoweb/pub/common/log"
"videoweb/pub/common/srvdef"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type RSP_TYPE int
const (
TextType RSP_TYPE = 0
FileType RSP_TYPE = 1
)
type TgCmdRsp struct {
Buf []byte
BufType RSP_TYPE
}
// TgBot Telegram机器人.
type TgBot struct {
// 机器人标示;
token string
// // 管理员分组;
// admins []int64
// // 管理群分组;
// groups []int64
// 机器人引擎;
engine *tgbotapi.BotAPI
// 命令存储集;
command map[string]func(string, int64) TgCmdRsp
help string
exitRcv chan interface{}
}
// BotCmd 命令.
type BotCmd struct {
// 指令;
Key string
// 摘要;
Summary string
// 处理;
Handle func(string, int64) TgCmdRsp
}
type BotCmdList []*BotCmd
// NewBotCmd 新的命令.
func NewBotCmd(key, summary string, handle func(string, int64) TgCmdRsp) *BotCmd {
return &BotCmd{
Key: key,
Summary: summary,
Handle: handle,
}
}
// NewTelegramBot 新的Telegram机器人.
func NewTelegramBot(token string) *TgBot {
return &TgBot{
token: token,
exitRcv: make(chan interface{}),
}
}
// Init 机器人初始化.
func (tb *TgBot) Init() *srvdef.SrvErr {
var err error
tb.engine, err = tgbotapi.NewBotAPI(tb.token)
if err != nil {
log.Warn("(tgbot:Init) tgbotapi.NewBotAPI failed", log.E(err))
return srvdef.New(srvdef.BusinessLevel, "", "")
}
tb.command = make(map[string]func(string, int64) TgCmdRsp)
tb.command["help"] = tb.menu
return nil
}
// Release 机器人退出释放
func (tb *TgBot) Release() *srvdef.SrvErr {
tb.engine.StopReceivingUpdates()
close(tb.exitRcv)
return nil
}
func (tb *TgBot) menu(string, int64) TgCmdRsp {
return TgCmdRsp{Buf: []byte(tb.help), BufType: TextType}
}
// RegisterCommands 注册命令并且自动生成help.
func (tb *TgBot) RegisterCommands(commands []*BotCmd) {
for _, cmd := range commands {
tb.command[cmd.Key] = cmd.Handle
tb.help += fmt.Sprintln("/" + cmd.Key + ": " + cmd.Summary)
}
}
// Receive 接收消息(阻塞).
func (tb *TgBot) Receive() error {
log.Info("(tgbot:Receive) start")
// 设置更新频率;
cfg := tgbotapi.NewUpdate(0)
//cfg.Timeout = 1
// 得到更新管道;
updates, err := tb.engine.GetUpdatesChan(cfg)
if err != nil {
log.Warn("(tgbot:Receive) tb.engine.GetUpdatesChan failed", log.E(err))
return srvdef.New(srvdef.BusinessLevel, "", "")
}
for {
select {
case update := <-updates:
tb.ProcessMessage(&update)
case <-tb.exitRcv:
log.Info("(tgbot:Receive) exist")
break
default:
}
time.Sleep(time.Second * 1)
}
return nil
}
func (tb *TgBot) ProcessMessage(update *tgbotapi.Update) {
// 忽略非消息更新;
if update.Message == nil {
log.Info("(tgbot:ProcessMessage) message is nil")
return
}
info := fmt.Sprintf("[Name: %s%s][ID: %d][Text: %s]\n",
update.Message.From.FirstName, update.Message.From.LastName,
update.Message.Chat.ID, update.Message.Text)
log.Info(info)
if update.Message.IsCommand() {
message := tgbotapi.NewMessage(update.Message.Chat.ID, "")
handle, ok := tb.command[update.Message.Command()]
if !ok {
message.Text = "unknown command"
} else {
param := update.Message.CommandArguments()
rsp := handle(param, update.Message.Chat.ID)
switch rsp.BufType {
case FileType:
default:
message.Text = string(rsp.Buf)
}
//message.Text = handle(param)
}
tb.engine.Send(message)
}
}
func (tb *TgBot) StopReceive() {
tb.engine.StopReceivingUpdates()
close(tb.exitRcv)
}
// Send 发送消息.
func (tb *TgBot) Send(ids []int64, text string) {
for _, id := range ids {
message := tgbotapi.NewMessage(id, text)
tb.engine.Send(message)
}
}
+35
View File
@@ -0,0 +1,35 @@
package tgbot
import (
"testing"
)
func create(t *testing.T) *TgBot {
token := "1014745091:AAFkw5j3Fw4ytvWgIll6iRH1q5WooS6JO74"
robot := NewTelegramBot(token)
if err := robot.Init(); err != nil {
t.Error(err)
t.Fail()
}
// 注册命令.
cmds := BotCmdList{NewBotCmd("test", "return testfunc result", testfunc)}
robot.RegisterCommands(cmds)
return robot
}
func testfunc(string, int64) TgCmdRsp {
return TgCmdRsp{Buf: []byte("help"), BufType: TextType}
}
func TestReceive(t *testing.T) {
robot := create(t)
if err := robot.Receive(); err != nil {
t.Error(err)
t.Fail()
}
}
func TestSend(t *testing.T) {
robot := create(t)
robot.Send([]int64{790603940}, "test send")
}