Commit efc79c58 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

init

parents
Pipeline #788 canceled with stages
.idea
*.iml
out
gen
*.sol
*.txt
.DS_Store
*.exe
build
\ No newline at end of file
FROM golang:1.21-alpine AS base
# Set up dependencies
ENV PACKAGES git openssh-client build-base
# Install dependencies
RUN apk add --update $PACKAGES
# Add source files
RUN mkdir -p ./sdk-api
COPY ./ ./sdk-api/
FROM base AS build
RUN cd sdk-api && go mod tidy && go build -v -o /tmp/api ./cmd/api && go build -v -o /tmp/messenger ./cmd/messenger
FROM alpine
WORKDIR /app
COPY ./config.toml /config.toml
COPY --from=build /tmp/api /usr/bin/api
COPY --from=build /tmp/messenger /usr/bin/messenger
EXPOSE 8080
\ No newline at end of file
.PHONY: default all clean dev messenger
GOBIN = $(shell pwd)/build/bin
default: all
all: api messenger
api:
go build $(BUILD_FLAGS) -v -o=${GOBIN}/$@ ./cmd/api
messenger:
go build $(BUILD_FLAGS) -v -o=${GOBIN}/$@ ./cmd/messenger
docker:
docker build -t caduceus/tg-messenger:latest -f Dockerfile .
package main
import (
"flag"
"sdk_api/config"
"sdk_api/dao"
messager "sdk_api/messenger"
"sdk_api/server"
"sdk_api/service"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}
func main() {
flag.Parse()
cfg, err := config.New()
if err != nil {
panic(err)
}
da, err := dao.New(cfg)
if err != nil {
panic(err)
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
msger := messager.NewMessenger(cfg.TGBot.Token, da)
svs := service.New(cfg, da, msger)
server.StartServer(svs, cfg)
}
package main
import (
"flag"
"sdk_api/config"
"sdk_api/dao"
messager "sdk_api/messenger"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}
func main() {
flag.Parse()
cfg, err := config.New()
if err != nil {
panic(err)
}
da, err := dao.New(cfg)
if err != nil {
panic(err)
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
msger := messager.NewMessenger(cfg.TGBot.Token, da)
msger.Start()
}
debug = true
[mysql]
#host = "tg-messenger-db"
host = "127.0.0.1"
port = 3306
user = "root"
password = ""
database = "sdk"
max_conn = 10
max_idle_conn = 2
enable_log = false
[server]
listen = "0.0.0.0:8080"
[tg_bot]
token = "6507972032:AAFAjaNz70ibA42kSH7k2gblyIQa1gWJiW0"
package config
import (
"flag"
"github.com/BurntSushi/toml"
)
type Config struct {
Debug bool `toml:"debug"`
MySQL MysqlConfig `toml:"mysql"`
Server ServerConfig `toml:"server"`
TGBot TGBotConfig `toml:"tg_bot"`
}
type MysqlConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
Database string `toml:"database"`
MaxConn int `toml:"max_conn"`
MaxIdleConn int `toml:"max_idle_conn"`
EnableLog bool `toml:"enable_log"`
}
type ServerConfig struct {
Listen string `toml:"listen"`
}
type TGBotConfig struct {
Token string `toml:"token"`
}
var confPath = flag.String("c", "config.toml", "config file path")
func New() (config *Config, err error) {
config = new(Config)
_, err = toml.DecodeFile(*confPath, config)
return
}
package constant
import (
"math/big"
)
const JwtSecret = "VrA1tFnHBhNTPRriHdUQLuFHb4PAFPAa"
const (
InvalidParam = "invalid param"
InternalError = "internal error"
)
var (
ZeroValue = big.NewInt(0)
Gwei = big.NewInt(1000000000)
Ether = big.NewInt(1000000000000000000)
)
package dao
import (
"fmt"
"sdk_api/config"
dbModel "sdk_api/model/db"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
type Dao struct {
c *config.Config
db *gorm.DB
}
func New(_c *config.Config) (dao *Dao, err error) {
dao = &Dao{
c: _c,
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True",
_c.MySQL.User, _c.MySQL.Password, _c.MySQL.Host, _c.MySQL.Port, _c.MySQL.Database)
lgr := logger.Default
if _c.MySQL.EnableLog {
lgr = logger.Default.LogMode(logger.Info)
}
dao.db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
Logger: lgr,
})
if err != nil {
return
}
sqlDB, err := dao.db.DB()
if err != nil {
return
}
sqlDB.SetMaxOpenConns(_c.MySQL.MaxConn)
sqlDB.SetMaxIdleConns(_c.MySQL.MaxIdleConn)
sqlDB.SetConnMaxIdleTime(time.Hour)
err = dao.db.AutoMigrate(&dbModel.User{}, &dbModel.Active{}, &dbModel.ChatGroup{})
if err != nil {
return
}
err = dao.addTestGroup()
if err != nil {
return
}
return dao, nil
}
package dao
import (
dbModel "sdk_api/model/db"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func (d *Dao) CreateUser(user *dbModel.User) (err error) {
return d.db.Clauses(clause.OnConflict{
DoUpdates: clause.Assignments(map[string]interface{}{"left_at": gorm.Expr("NULL")}),
}).Create(user).Error
}
func (d *Dao) IncrMessageCount(a *dbModel.Active) (err error) {
a.MsgCount = 1
return d.db.Clauses(clause.OnConflict{
DoUpdates: clause.Assignments(map[string]interface{}{"msg_count": gorm.Expr("msg_count + ?", 1)}),
}).Create(a).Error
}
func (d *Dao) GetUserActiveMsgCount(userId, chatId, unixDay int) (count int, err error) {
a := dbModel.Active{}
err = d.db.Model(&dbModel.Active{}).
Where("`user_id` = ? AND `chat_id` = ? AND `unix_day` = ?", userId, chatId, unixDay).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return 0, nil
}
return a.MsgCount, err
}
func (d *Dao) UserExist(userId, chatId int) (exist bool, err error) {
a := dbModel.User{}
err = d.db.Model(&dbModel.User{}).
Where("`user_id` = ? AND `chat_id` = ? AND `left_at` IS NULL", userId, chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
}
return err == nil, err
}
func (d *Dao) UserLeft(userId, chatId int) (err error) {
return d.db.Model(&dbModel.User{}).
Where("`user_id` = ? AND `chat_id` = ? AND `left_at` IS NULL", userId, chatId).
Update("left_at", gorm.Expr("NOW()")).Error
}
func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) {
a := dbModel.ChatGroup{}
err = d.db.Model(&dbModel.ChatGroup{}).
Where("`chat_id` = ?", chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
}
return err == nil, err
}
func (d *Dao) addTestGroup() (err error) {
return d.db.Clauses(clause.OnConflict{DoNothing: true}).Create([]dbModel.ChatGroup{
{
ChatId: -1002174503961,
ChatTitle: "mail_notice",
},
}).Error
}
networks:
default:
name: aon-db
services:
tg-messenger-api:
image: caduceus/tg-messenger:latest
pull_policy: always
container_name: tg-messenger-api
ports:
- "16669:8080"
depends_on:
aon-db:
condition: service_healthy
volumes:
- ./conf/tg_messenger/config.toml:/config.toml
command:
- "/bin/sh"
- "-c"
- "/usr/bin/api -c /config.toml"
restart:
unless-stopped
tg-messenger:
image: caduceus/tg-messenger:latest
pull_policy: always
container_name: tg-messenger
depends_on:
aon-db:
condition: service_healthy
volumes:
- ./conf/tg_messenger/config.toml:/config.toml
command:
- "/bin/sh"
- "-c"
- "/usr/bin/messenger -c /config.toml"
restart:
unless-stopped
\ No newline at end of file
module sdk_api
go 1.21.4
require (
github.com/BurntSushi/toml v0.3.1
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/sirupsen/logrus v1.9.0
github.com/tidwall/gjson v1.17.1
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.10
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
package messager
import (
"sdk_api/dao"
dbModel "sdk_api/model/db"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
)
type Messenger struct {
botToken string
bot *tgbotapi.BotAPI
d *dao.Dao
}
func NewMessenger(botToken string, _d *dao.Dao) *Messenger {
bot, err := tgbotapi.NewBotAPI(botToken)
if err != nil {
panic(err)
}
log.WithField("account", bot.Self.UserName).Info("Authorized on account")
return &Messenger{botToken: botToken, bot: bot, d: _d}
}
func (m *Messenger) Start() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := m.bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil ||
update.Message.IsCommand() {
continue
}
supported, err := m.d.IsSupportedChat(int(update.Message.Chat.ID))
if err != nil {
log.WithError(err).Error("check supported chat error")
continue
}
if !supported {
continue
}
var caught bool
caught = m.handleUserLeft(update.Message)
if caught {
continue
}
caught = m.handleNewUser(update.Message)
if caught {
continue
}
m.handleActive(update.Message)
}
}
func (m *Messenger) ExistGroupChat(userId, chatId int64) (exist bool, err error) {
mem, err := m.bot.GetChatMember(tgbotapi.GetChatMemberConfig{
ChatConfigWithUser: tgbotapi.ChatConfigWithUser{
ChatID: chatId,
UserID: userId,
},
})
if err != nil {
return
}
switch {
case mem.HasLeft(), mem.WasKicked():
return false, nil
default:
return true, nil
}
}
func (m *Messenger) handleNewUser(msg *tgbotapi.Message) (caught bool) {
for _, user := range msg.NewChatMembers {
log.WithFields(log.Fields{
"user_id": user.ID,
"username": user.UserName,
"chat_id": msg.Chat.ID,
"chat_title": msg.Chat.Title,
"is_bot": user.IsBot,
}).Debug("new user")
if user.IsBot {
continue
}
user := &dbModel.User{
UserId: int(user.ID),
ChatId: int(msg.Chat.ID),
Username: user.UserName,
}
if err := m.d.CreateUser(user); err != nil {
log.WithError(err).Error("create user error")
}
if !caught {
caught = true
}
}
return caught
}
func (m *Messenger) handleUserLeft(msg *tgbotapi.Message) (caught bool) {
if msg.LeftChatMember == nil {
return
}
log.WithFields(log.Fields{
"user_id": msg.LeftChatMember.ID,
"username": msg.LeftChatMember.UserName,
"chat_id": msg.Chat.ID,
"chat_title": msg.Chat.Title,
"is_bot": msg.LeftChatMember.IsBot,
}).Debug("user left")
user := msg.LeftChatMember
if user.IsBot {
return
}
err := m.d.UserLeft(int(user.ID), int(msg.Chat.ID))
if err != nil {
log.WithError(err).Error("user left error")
}
return true
}
func (m *Messenger) handleActive(msg *tgbotapi.Message) {
log.WithFields(log.Fields{
"user_id": msg.From.ID,
"username": msg.From.UserName,
"chat_id": msg.Chat.ID,
"chat_title": msg.Chat.Title,
"is_bot": msg.From.IsBot,
}).Debug("new msg")
if msg.From.IsBot {
return
}
exist, err := m.d.UserExist(int(msg.From.ID), int(msg.Chat.ID))
if err != nil {
log.WithError(err).Error("exist user error")
}
if err == nil && !exist {
err = m.d.CreateUser(&dbModel.User{
UserId: int(msg.From.ID),
ChatId: int(msg.Chat.ID),
Username: msg.From.UserName,
})
if err != nil {
log.WithError(err).Error("create user 2 error")
}
}
err = m.d.IncrMessageCount(&dbModel.Active{
UserId: int(msg.From.ID),
ChatId: int(msg.Chat.ID),
UnixDay: msg.Date / 86400,
})
if err != nil {
log.WithError(err).Error("incr message count error")
}
}
func (m *Messenger) SendMessage(chatId int64, msg string) error {
_, err := m.bot.Send(tgbotapi.NewMessage(chatId, msg))
return err
}
package messager
import (
"testing"
)
func TestFunc(t *testing.T) {
token := "6507972032:AAFAjaNz70ibA42kSH7k2gblyIQa1gWJiW0"
m := NewMessenger(token, nil)
m.ExistGroupChat(6689160806, -1002174503961)
m.Start()
}
package middleware
import (
"bytes"
"io"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func PrintRequestResponseBodyMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 读取请求 body
var requestBody []byte
if c.Request.Body != nil {
requestBody, _ = io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
}
log.WithFields(log.Fields{"method": c.Request.Method, "uri": c.Request.RequestURI, "body": string(requestBody)}).Debug("request body")
bodyWriter := &responseBodyWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = bodyWriter
c.Next()
responseBody := bodyWriter.body.String()
log.WithFields(log.Fields{"status": c.Writer.Status(), "body": responseBody}).Debug("response body")
}
}
type responseBodyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (r *responseBodyWriter) Write(b []byte) (int, error) {
r.body.Write(b)
return r.ResponseWriter.Write(b)
}
package middleware
import (
"sdk_api/util"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func JWTMiddleware(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
log.Debugln(tokenString)
if tokenString == "" || len(tokenString) < 7 {
c.JSON(200, gin.H{
"code": 1,
"msg": "invalid token",
"data": "",
})
c.Abort()
return
}
ok, expired, uid, _, _ := util.ParseJWT(tokenString[7:])
if !ok {
c.JSON(200, gin.H{
"code": 1,
"msg": "invalid token",
"data": "",
})
c.Abort()
return
}
if expired {
c.JSON(200, gin.H{
"code": 1,
"msg": "token expired",
"data": "",
})
c.Abort()
return
}
log.WithField("uid", uid).Debug("jwt uid")
c.Set("jwt-uid", uid)
c.Next()
}
package api_model
package db_model
import (
"database/sql"
"gorm.io/gorm"
)
type User struct {
Id int `gorm:"primaryKey"`
UserId int `gorm:"type:int;uniqueIndex:uidx_uid_cid;not null;comment:telegram用户id"`
Username string `gorm:"type:varchar(255);not null;comment:telegram用户名"`
ChatId int `gorm:"type:int;uniqueIndex:uidx_uid_cid;not null;comment:telegram群id"`
LeftAt sql.NullTime `gorm:"index;comment:退出时间"`
gorm.Model
}
type Active struct {
Id int `gorm:"primaryKey"`
UserId int `gorm:"type:int;uniqueIndex:uidx_uid_cid_day;not null;comment:telegram用户id"`
ChatId int `gorm:"type:int;uniqueIndex:uidx_uid_cid_day;not null;comment:telegram群id"`
UnixDay int `gorm:"type:int;not null;uniqueIndex:uidx_uid_cid_day;comment:unix day"`
MsgCount int `gorm:"type:int;not null;comment:消息数"`
gorm.Model
}
type ChatGroup struct {
Id int `gorm:"primaryKey"`
ChatId int `gorm:"type:int;uniqueIndex;not null;comment:telegram群id"`
ChatTitle string `gorm:"type:varchar(255);not null;comment:telegram群名"`
Description string `gorm:"type:varchar(255);comment:群描述"`
gorm.Model
}
# telegram-messenger
统计用户每日发言,是否在某个群组内
\ No newline at end of file
package server
import (
"sdk_api/middleware"
"github.com/gin-gonic/gin"
)
func initRouter(e *gin.Engine) {
e.Use(middleware.PrintRequestResponseBodyMiddleware())
v1 := e.Group("/api/v1")
{
user := v1.Group("/user")
user.GET("/joined", isJoined)
user.GET("/active", active)
}
}
package server
import (
"sdk_api/config"
"sdk_api/service"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
var srv *service.Service
var conf *config.Config
func StartServer(_srv *service.Service, _conf *config.Config) {
srv = _srv
conf = _conf
if !conf.Debug {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.Default()
_cors := cors.DefaultConfig()
_cors.AllowAllOrigins = true
_cors.AllowHeaders = []string{"*"}
engine.Use(cors.New(_cors))
initRouter(engine)
log.Infof("start http server listening %s", conf.Server.Listen)
if err := engine.Run(conf.Server.Listen); err != nil {
log.Error("http server run error: ", err)
}
}
func withSuccess(obj interface{}) interface{} {
return gin.H{
"code": 0,
"msg": "ok",
"data": obj,
}
}
func withError(msg string) interface{} {
return gin.H{
"code": 1,
"error": msg,
"data": "",
}
}
package server
import (
"sdk_api/constant"
"strconv"
"github.com/gin-gonic/gin"
)
func isJoined(c *gin.Context) {
_userId := c.Query("userId")
_chatId := c.Query("chatId")
userId, _ := strconv.Atoi(_userId)
chatId, _ := strconv.Atoi(_chatId)
if userId == 0 || chatId == 0 {
c.JSON(200, withError(constant.InvalidParam))
return
}
joined, err := srv.IsJoined(userId, chatId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, gin.H{"joined": joined})
return
}
func active(c *gin.Context) {
_userId := c.Query("userId")
_chatId := c.Query("chatId")
userId, _ := strconv.Atoi(_userId)
chatId, _ := strconv.Atoi(_chatId)
if userId == 0 || chatId == 0 {
c.JSON(200, withError(constant.InvalidParam))
return
}
msgCount, err := srv.Active(userId, chatId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, gin.H{"msgCount": msgCount})
}
package service
import (
"sdk_api/config"
"sdk_api/dao"
messager "sdk_api/messenger"
)
type Service struct {
d *dao.Dao
cfg *config.Config
msger *messager.Messenger
}
func New(conf *config.Config, da *dao.Dao, m *messager.Messenger) *Service {
return &Service{
d: da,
cfg: conf,
msger: m,
}
}
package service
import (
"time"
log "github.com/sirupsen/logrus"
)
func (s *Service) IsJoined(userId, chatId int) (joined bool, err error) {
joined, err = s.msger.ExistGroupChat(int64(userId), int64(chatId))
if err != nil {
log.WithError(err).Error("exist group chat error")
}
return
}
func (s *Service) Active(userId, chatId int) (msgCount int, err error) {
msgCount, err = s.d.GetUserActiveMsgCount(userId, chatId, int(time.Now().Unix()/86400))
if err != nil {
log.WithError(err).Error("get user active msg count error")
}
return
}
package util
import (
"time"
"github.com/golang-jwt/jwt/v5"
)
const secret = "cxcZa005Y5zWH1wFgXvPGDL02Ey4ZCLAh2XFcfp7HhG3wTg5TbcnhuYhNvN3YLgt"
func GenerateJWT(uid, platform, platformId string) string {
tk := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"uid": uid,
"platform": platform,
"platformId": platformId,
"iat": time.Now().Unix(),
"exp": time.Now().Add(7 * 24 * time.Hour).Unix(),
})
j, _ := tk.SignedString([]byte(secret))
return j
}
func ParseJWT(token string) (ok, expired bool, uid, platform, platformId string) {
claims := jwt.MapClaims{}
tk, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil {
return
}
if !tk.Valid {
return
}
uid = claims["uid"].(string)
platform = claims["platform"].(string)
platformId = claims["platformId"].(string)
exp := claims["exp"].(float64)
if time.Now().Unix() > int64(exp) {
expired = true
return
}
ok = true
return
}
package util
import (
"testing"
)
func TestA(t *testing.T) {
token := GenerateJWT("12345", "telegram", "0x12345")
t.Logf("token: %s", token)
ParseJWT(token)
}
func TestB(t *testing.T) {
t.Log(ParseJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTgwMTU2MTQsImlhdCI6MTcxNzQxMDgxNCwicGxhdGZvcm0iOiJ0ZWxlZ3JhbSIsInBsYXRmb3JtSWQiOiIweDEyMzQ1IiwidWlkIjoiMTIzNDUifQ.yZ1V_cGozBrwK55Y9iZsG4C-B5T96V2E3-AqP6CqkR8"))
}
package util
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/tidwall/gjson"
)
func VerifyInitData(initData, botToken string) (ok bool, botId, userId string) {
h := hmac.New(sha256.New, []byte("WebAppData"))
h.Write([]byte(botToken))
secret := h.Sum(nil)
h2 := hmac.New(sha256.New, secret)
params, err := url.ParseQuery(initData)
if err != nil {
return
}
var hashval string
var keys []string
for key := range params {
if key == "hash" {
hashval = params.Get(key)
continue
}
if key == "auth_date" {
authDate, _ := strconv.Atoi(params.Get(key))
if int64(authDate) < time.Now().Unix()-3600 || int64(authDate) > time.Now().Unix()+300 {
// todo 可以限制超时时间
return false, "", ""
}
}
if key == "user" {
userId = gjson.Get(params.Get(key), "id").String()
}
keys = append(keys, key)
}
sort.Strings(keys)
var payloads []string
for _, key := range keys {
payloads = append(payloads, fmt.Sprintf("%s=%s", key, params.Get(key)))
}
payload := strings.Join(payloads, "\n")
h2.Write([]byte(payload))
h2sum := h2.Sum(nil)
items := strings.Split(botToken, ":")
if len(items) != 2 {
return
}
ok = fmt.Sprintf("%x", h2sum) == hashval
botId = items[0]
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment