From 8fbaa97d7c0e6c9f9034a9dc3e6573f62d6709fe Mon Sep 17 00:00:00 2001 From: icceey Date: Sat, 2 Jan 2021 19:43:06 +0800 Subject: [PATCH 01/19] fix: disable web preview for command 'links' --- base/hello.go | 1 + 1 file changed, 1 insertion(+) diff --git a/base/hello.go b/base/hello.go index b18acb5f..0f527212 100644 --- a/base/hello.go +++ b/base/hello.go @@ -60,6 +60,7 @@ func Links(update tgbotapi.Update, bot *tgbotapi.BotAPI) { messageReply := tgbotapi.NewMessage(chatID, txt) messageReply.ParseMode = tgbotapi.ModeMarkdownV2 + messageReply.DisableWebPagePreview = true util.SendMessage(bot, messageReply) } From 4b1c45fb673708eb59219712d16e90346aa24a74 Mon Sep 17 00:00:00 2001 From: icceey Date: Sun, 3 Jan 2021 16:34:30 +0800 Subject: [PATCH 02/19] fix: disable web preview for search engines --- base/search-engines.go | 1 + 1 file changed, 1 insertion(+) diff --git a/base/search-engines.go b/base/search-engines.go index 803d51ed..ff1194bf 100644 --- a/base/search-engines.go +++ b/base/search-engines.go @@ -20,6 +20,7 @@ func mapToHTML(mapper htmlMapper) module.Module { resultMedia := tgbotapi.NewMessage(msg.Chat.ID, mapper(msg)) resultMedia.ParseMode = tgbotapi.ModeHTML resultMedia.ReplyToMessageID = msg.MessageID + resultMedia.DisableWebPagePreview = true return resultMedia }) } From 49928b3e62a4a8042e2b8aeb70677e78336511e7 Mon Sep 17 00:00:00 2001 From: hugefiver Date: Mon, 4 Jan 2021 17:50:41 +0800 Subject: [PATCH 03/19] add gh action: docker build and push --- .github/workflows/docker_push.yml | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/docker_push.yml diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml new file mode 100644 index 00000000..0234487d --- /dev/null +++ b/.github/workflows/docker_push.yml @@ -0,0 +1,35 @@ +name: Docker Image Push + +on: + push: + branches: [ dev ] + +jobs: + docker_build: + name: "build and push docker image" + runs-on: ubuntu-latest + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + push: true + tags: csust/csust-got:latest-test + build-args: | + arg1=value1 + arg2=value2 + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file From b04e791313f9679e9ee7c919f78ebaed5ffab713 Mon Sep 17 00:00:00 2001 From: hugefiver Date: Mon, 4 Jan 2021 17:54:05 +0800 Subject: [PATCH 04/19] rm unused value --- .github/workflows/docker_push.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index 0234487d..9ba9bb11 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -27,9 +27,6 @@ jobs: with: push: true tags: csust/csust-got:latest-test - build-args: | - arg1=value1 - arg2=value2 - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file From 99ab999417dc41a1add6c249108871a2262a07b5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 13 Jan 2021 21:07:13 +0000 Subject: [PATCH 05/19] build(deps): bump github.com/stretchr/testify from 1.6.1 to 1.7.0 Bumps [github.com/stretchr/testify](https://github.com/stretchr/testify) from 1.6.1 to 1.7.0. - [Release notes](https://github.com/stretchr/testify/releases) - [Commits](https://github.com/stretchr/testify/compare/v1.6.1...v1.7.0) Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index a3035ac1..9e0eabc7 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 github.com/prometheus/client_golang v1.9.0 github.com/spf13/viper v1.7.1 - github.com/stretchr/testify v1.6.1 + github.com/stretchr/testify v1.7.0 go.uber.org/zap v1.16.0 golang.org/x/time v0.0.0-20191024005414-555d28b269f0 google.golang.org/protobuf v1.25.0 // indirect diff --git a/go.sum b/go.sum index 132e4cf6..bdf71ddb 100644 --- a/go.sum +++ b/go.sum @@ -92,8 +92,6 @@ github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1 github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20200811182351-15c95b8a8418 h1:8z7Upfv4WLB03X0Cs4V0ejY+uZGEIBmZIpyqbyqE0LU= -github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20200811182351-15c95b8a8418/go.mod h1:lDm2E64X4OjFdBUA4hlN4mEvbSitvhJdKw7rsA8KHgI= github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 h1:CguiLTREMSU5GMaHMlAUAVb2cT8M+IpZVhgRK1te6Ds= github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947/go.mod h1:lDm2E64X4OjFdBUA4hlN4mEvbSitvhJdKw7rsA8KHgI= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= @@ -341,8 +339,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= From 07e8bd9c97bc4b4da63d4f54bbd979bca0162a01 Mon Sep 17 00:00:00 2001 From: icceey Date: Thu, 21 Jan 2021 21:57:48 +0800 Subject: [PATCH 06/19] feat: add black list and white list --- base/hello.go | 4 +-- base/hitokoto.go | 7 ++--- base/notification.go | 9 ++---- base/other.go | 6 ---- config.yaml | 3 ++ config/config.go | 17 +++++++---- config/message.go | 12 ++++++-- config/special_list.go | 32 ++++++++++----------- main.go | 65 +++++++++++++++++++++++++++++++++--------- restrict/fake-ban.go | 2 +- 10 files changed, 99 insertions(+), 58 deletions(-) diff --git a/base/hello.go b/base/hello.go index 0f527212..2cab2c7b 100644 --- a/base/hello.go +++ b/base/hello.go @@ -89,7 +89,7 @@ func Shutdown(update tgbotapi.Update) module.Module { msg := tgbotapi.NewMessage(update.Message.Chat.ID, GetHitokoto("i", false)+" 早上好,新的一天加油哦!:)") if err := orm.WriteBool(ctx, key, false); err != nil { log.Error("failed to access redis.", zap.Error(err)) - msg.Text = "我不愿面对这苦涩的一天……:(" + msg.Text = config.BotConfig.MessageConfig.BootFailed } util.SendMessage(bot, msg) return module.NextOfChain @@ -132,7 +132,7 @@ func NoSleep(update tgbotapi.Update, bot *tgbotapi.BotAPI) { message := update.Message chatID := message.Chat.ID - messageReply := tgbotapi.NewMessage(chatID, "睡你麻痹起来嗨!") + messageReply := tgbotapi.NewMessage(chatID, config.BotConfig.MessageConfig.NoSleep) util.SendMessage(bot, messageReply) } diff --git a/base/hitokoto.go b/base/hitokoto.go index 47053596..ab2b9392 100644 --- a/base/hitokoto.go +++ b/base/hitokoto.go @@ -1,6 +1,7 @@ package base import ( + "csust-got/config" "csust-got/entities" "csust-got/log" "csust-got/orm" @@ -15,10 +16,6 @@ import ( "go.uber.org/zap" ) -const errMessage = `过去那些零碎的细语并不构成这个世界:对于你而言,该看,该想,该体会身边那些微小事物的律动。 -忘了这些话吧。忘了这个功能吧——只今它已然不能给予你更多。而你的未来属于新的旅途:去欲望、去收获、去爱、去恨。 -去做只属于你自己的选择,写下只有你深谙个中滋味的诗篇。我们的生命以后可能还会交织之时,但如今,再见辣。` - // HitokotoResponse is HitokotoResponse type HitokotoResponse struct { ID int `json:"id"` @@ -142,7 +139,7 @@ func loadFromRedis(from bool) string { res, err := orm.GetClient().SRandMember("hitokoto").Result() if err != nil { log.Error("Err@Hitokoto [STORE]", zap.Error(err)) - return errMessage + return config.BotConfig.MessageConfig.HitokotoNotFound } if !from { res = res[:strings.LastIndex(res, " by ")+1] diff --git a/base/notification.go b/base/notification.go index c059a114..2f8d3952 100644 --- a/base/notification.go +++ b/base/notification.go @@ -7,6 +7,7 @@ import ( ) // WelcomeNewMember is handle for welcome new member. +// when someone new join group, bot will send welcome message. func WelcomeNewMember(update tgbotapi.Update, bot *tgbotapi.BotAPI) { message := update.Message memberSlice := message.NewChatMembers @@ -15,11 +16,7 @@ func WelcomeNewMember(update tgbotapi.Update, bot *tgbotapi.BotAPI) { } for _, member := range *memberSlice { text := "Welcome to this group!" + util.GetName(member) - go sendNotificationTo(bot, message.Chat.ID, text) + message := tgbotapi.NewMessage(message.Chat.ID, text) + util.SendMessage(bot, message) } } - -func sendNotificationTo(bot *tgbotapi.BotAPI, chatID int64, text string) { - message := tgbotapi.NewMessage(chatID, text) - util.SendMessage(bot, message) -} diff --git a/base/other.go b/base/other.go index 00c06a0d..b20c22f7 100644 --- a/base/other.go +++ b/base/other.go @@ -13,12 +13,6 @@ import ( // FakeBanMyself is handle for command `fake_ban_myself`. // Use it to just get a reply like command `ban_myself`. // It looks like you've been banned, but in fact you have a 2% chance that it will actually be banned。 -// Use it to just get a reply like command `ban_myself`. -// It looks like you've been banned, but in fact you have a 2% chance that it will actually be banned。 -// Use it to just get a reply like command `ban_myself`. -// It looks like you've been banned, but in fact you have a 2% chance that it will actually be banned。 -// Use it to just get a reply like command `ban_myself`. -// It looks like you've been banned, but in fact you have a 2% chance that it will actually be banned。 func FakeBanMyself(update tgbotapi.Update, bot *tgbotapi.BotAPI) { sec := time.Duration(rand.Intn(60)+60) * time.Second chatID := update.Message.Chat.ID diff --git a/config.yaml b/config.yaml index a1721772..3c25c95f 100644 --- a/config.yaml +++ b/config.yaml @@ -28,3 +28,6 @@ message: links: "NO LINKS" restrict_bot: "好 的, 我 杀 我 自 己。" fake_ban_in_cd: "" + hitokoto_not_found: "过去那些零碎的细语并不构成这个世界:对于你而言,该看,该想,该体会身边那些微小事物的律动。忘了这些话吧。忘了这个功能吧——只今它已然不能给予你更多。而你的未来属于新的旅途:去欲望、去收获、去爱、去恨。去做只属于你自己的选择,写下只有你深谙个中滋味的诗篇。我们的生命以后可能还会交织之时,但如今,再见辣。" + no_sleep: "睡你麻痹起来嗨!" + boot_failed: "我不愿面对这苦涩的一天……:(" diff --git a/config/config.go b/config/config.go index 1e9174ce..b7ab187f 100644 --- a/config/config.go +++ b/config/config.go @@ -16,12 +16,13 @@ var ( noRedisMsg = "redis address is not set! Please set config file config.yaml or env BOT_REDIS_ADDR!" ) +// interface for module config type config interface { readConfig() checkConfig() } -// InitConfig - init bot config +// InitConfig - init bot config. func InitConfig(configFile, envPrefix string) { BotConfig = NewBotConfig() initViper(configFile, envPrefix) @@ -29,15 +30,18 @@ func InitConfig(configFile, envPrefix string) { checkConfig() } -// NewBotConfig - return new bot config +// NewBotConfig - return new bot config with all zero value. +// In general, you don't need to NewBotConfig, global BotConfig should be used. func NewBotConfig() *Config { config := new(Config) config.RateLimitConfig = new(rateLimitConfig) config.RedisConfig = new(redisConfig) config.RestrictConfig = new(restrictConfig) config.MessageConfig = new(messageConfig) - config.WhiteListConfig = new(whiteListConfig) - config.BlackListConfig = new(blackListConfig) + config.WhiteListConfig = new(specialListConfig) + config.BlackListConfig = new(specialListConfig) + config.WhiteListConfig.SetName("white_list") + config.BlackListConfig.SetName("black_list") return config } @@ -53,8 +57,8 @@ type Config struct { RestrictConfig *restrictConfig RateLimitConfig *rateLimitConfig MessageConfig *messageConfig - BlackListConfig *blackListConfig - WhiteListConfig *whiteListConfig + BlackListConfig *specialListConfig + WhiteListConfig *specialListConfig } // BotID returns the BotID of this config. @@ -100,6 +104,7 @@ func readConfig() { BotConfig.BlackListConfig.readConfig() } +// check some config value is reasonable, otherwise set to default value. func checkConfig() { if BotConfig.Token == "" { zap.L().Panic(noTokenMsg) diff --git a/config/message.go b/config/message.go index 8c81be87..036abf3f 100644 --- a/config/message.go +++ b/config/message.go @@ -8,15 +8,21 @@ import ( var missMsg = "[this message has eat by bot]" type messageConfig struct { - Links string - RestrictBot string - FakeBanInCD string + Links string + RestrictBot string + FakeBanInCD string + HitokotoNotFound string + NoSleep string + BootFailed string } func (c *messageConfig) readConfig() { c.Links = viper.GetString("message.links") c.RestrictBot = viper.GetString("message.restrict_bot") c.FakeBanInCD = viper.GetString("message.fake_ban_in_cd") + c.HitokotoNotFound = viper.GetString("message.hitokoto_not_found") + c.NoSleep = viper.GetString("message.no_sleep") + c.BootFailed = viper.GetString("message.boot_failed") } func (c *messageConfig) checkConfig() { diff --git a/config/special_list.go b/config/special_list.go index e654528f..bfbc51fb 100644 --- a/config/special_list.go +++ b/config/special_list.go @@ -2,30 +2,30 @@ package config import "github.com/spf13/viper" -type blackListConfig struct { +type specialListConfig struct { + Name string Enabled bool - Users []int + Chats []int64 } -func (c *blackListConfig) readConfig() { - c.Users = make([]int, 0) - c.Enabled = viper.GetBool("black_list.enabled") +func (c *specialListConfig) readConfig() { + c.Chats = make([]int64, 0) + c.Enabled = viper.GetBool(c.Name + ".enabled") } -func (c *blackListConfig) checkConfig() { +func (c *specialListConfig) checkConfig() { } -type whiteListConfig struct { - Enabled bool - Users []int -} - -func (c *whiteListConfig) readConfig() { - c.Users = make([]int, 0) - c.Enabled = viper.GetBool("white_list.enabled") +func (c *specialListConfig) SetName(name string) { + c.Name = name } -func (c *whiteListConfig) checkConfig() { - +func (c *specialListConfig) Check(chatID int64) bool { + for _, v := range c.Chats { + if v == chatID { + return true + } + } + return false } diff --git a/main.go b/main.go index bebf5d76..2b2e992c 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "csust-got/orm" "csust-got/prom" "csust-got/restrict" + "github.com/go-redis/redis/v7" + "strconv" "sync" "time" @@ -30,26 +32,16 @@ func main() { } bot.Debug = config.BotConfig.DebugMode - - log.Info("Success Authorized", zap.String("botUserName", bot.Self.UserName)) - config.BotConfig.Bot = bot + log.Info("Success Authorized", zap.String("botUserName", bot.Self.UserName)) u := tgbotapi.NewUpdate(0) u.Timeout = 60 - updates, err := bot.GetUpdatesChan(u) ctx := context.Global(orm.GetClient(), config.BotConfig) - - // check database - rc := ctx.GlobalClient() - // blacklist - if list, err := rc.SMembers(ctx.WrapKey("black_black_list")).Result(); err != nil { - // dont do anything, maybe. (΄◞ิ౪◟ิ‵) - } else { - log.Info("Black List has load.", zap.Int("blackListLength", len(list))) - } + loadBlackList(ctx) + loadWhiteList(ctx) handles := module.Parallel([]module.Module{ module.Stateless(base.Hello, preds.IsCommand("say_hello")), @@ -101,6 +93,9 @@ func main() { go func() { defer wg.Done() for update := range updates { + if update.Message != nil && ctx.GlobalConfig().BlackListConfig.Check(update.Message.Chat.ID) { + continue + } start := time.Now() result := handles.HandleUpdate(ctx, update, bot) cost := time.Since(start) @@ -110,3 +105,47 @@ func main() { } wg.Wait() } + +func loadWhiteList(ctx context.Context) { + list, err := ctx.GlobalClient().SMembers(ctx.WrapKey("white_list")).Result() + if err != nil { + if err != redis.Nil { + log.Error("load white list failed", zap.Error(err)) + return + } + list = make([]string, 0) + } + log.Info("White List has load.", zap.Int("length", len(list))) + chats := make([]int64, 0) + for _, v := range list { + cID, err := strconv.ParseInt(v, 10, 64) + if err != nil { + log.Error("white list has invalid value", zap.String("value", v)) + continue + } + chats = append(chats, cID) + } + ctx.GlobalConfig().WhiteListConfig.Chats = chats +} + +func loadBlackList(ctx context.Context) { + list, err := ctx.GlobalClient().SMembers(ctx.WrapKey("black_list")).Result() + if err != nil { + if err != redis.Nil { + log.Error("load black list failed", zap.Error(err)) + return + } + list = make([]string, 0) + } + log.Info("Black List has load.", zap.Int("length", len(list))) + chats := make([]int64, 0) + for _, v := range list { + cID, err := strconv.ParseInt(v, 10, 64) + if err != nil { + log.Error("black list has invalid value", zap.String("value", v)) + continue + } + chats = append(chats, cID) + } + ctx.GlobalConfig().BlackListConfig.Chats = chats +} diff --git a/restrict/fake-ban.go b/restrict/fake-ban.go index b10fc42b..77095958 100644 --- a/restrict/fake-ban.go +++ b/restrict/fake-ban.go @@ -104,7 +104,7 @@ func generateTextWithCD(ctx context.Context, spec BanSpec) string { bbid := strconv.Itoa(spec.BigBrother.ID) rc := ctx.GlobalClient() // check if this user in Blacklist - if black, err := rc.SIsMember(ctx.WrapKey("black_black_list"), bbid).Result(); err != nil /*&& err == redis.Nil*/ { + if black, err := rc.SIsMember(ctx.WrapKey("black_list"), bbid).Result(); err != nil /*&& err == redis.Nil*/ { return "啊这,好像有点不太对,不过问题不大。" } else if black { return "CNM,你背叛了老嚯阶级。" From 064401fa673ce70c90dfbc788156778a842c53a4 Mon Sep 17 00:00:00 2001 From: icceey Date: Thu, 21 Jan 2021 22:34:59 +0800 Subject: [PATCH 07/19] feat: add member count --- base/helper.go | 17 +++++++++++++++++ base/notification.go | 16 ++++++++++++++-- main.go | 2 ++ prom/metrics.go | 18 ++++++++++++++++++ prom/prom.go | 25 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/base/helper.go b/base/helper.go index 07f8e64b..36e15971 100644 --- a/base/helper.go +++ b/base/helper.go @@ -1,8 +1,11 @@ package base import ( + "csust-got/log" + "csust-got/prom" "csust-got/util" "fmt" + "go.uber.org/zap" "runtime" "time" @@ -64,3 +67,17 @@ func GetChatID(update tgbotapi.Update, bot *tgbotapi.BotAPI) { util.SendMessage(bot, messageReply) } + +// GetGroupMember get how many members in group +func GetGroupMember(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + chat := update.Message.Chat + if chat.IsPrivate() { + return + } + num, err := bot.GetChatMembersCount(chat.ChatConfig()) + if err != nil { + log.Error("GetChatMembersCount failed", zap.Int64("chatID", chat.ID), zap.Error(err)) + return + } + prom.GetMember(chat.Title, num) +} diff --git a/base/notification.go b/base/notification.go index 2f8d3952..3fdaf0bf 100644 --- a/base/notification.go +++ b/base/notification.go @@ -1,6 +1,7 @@ package base import ( + "csust-got/prom" "csust-got/util" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" @@ -16,7 +17,18 @@ func WelcomeNewMember(update tgbotapi.Update, bot *tgbotapi.BotAPI) { } for _, member := range *memberSlice { text := "Welcome to this group!" + util.GetName(member) - message := tgbotapi.NewMessage(message.Chat.ID, text) - util.SendMessage(bot, message) + messageR := tgbotapi.NewMessage(message.Chat.ID, text) + util.SendMessage(bot, messageR) + prom.NewMember(message.Chat.Title) } } + +// LeftMember is handle for some member left. +func LeftMember(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + message := update.Message + member := message.LeftChatMember + if member == nil { + return + } + prom.MemberLeft(message.Chat.Title) +} diff --git a/main.go b/main.go index 2b2e992c..f964ff28 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,8 @@ func main() { module.Stateless(base.Sleep, preds.IsCommand("sleep")), module.Stateless(base.NoSleep, preds.IsCommand("no_sleep")), module.Stateless(base.WelcomeNewMember, preds.NonEmpty), + module.Stateless(base.LeftMember, preds.NonEmpty), + module.Stateless(base.GetGroupMember, preds.NonEmpty), module.Stateless(base.HelloToAll, preds.IsCommand("hello_to_all")), module.Stateless(restrict.BanMyself, preds.IsCommand("ban_myself")), module.Stateless(base.FakeBanMyself, preds.IsCommand("fake_ban_myself")), diff --git a/prom/metrics.go b/prom/metrics.go index 40b2f3c2..f87e2a9e 100644 --- a/prom/metrics.go +++ b/prom/metrics.go @@ -30,3 +30,21 @@ var updateCostTime = prometheus.NewGaugeVec( }, baseLabels, ) + +// chatMemberCount chatMemberCount +var chatMemberCount = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "bot_chat_members_count", + Help: "how many members in a chat", + }, + []string{"host", "chat_name"}, +) + +// newMemberCount chatMemberCount +var newMemberCount = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "bot_new_members_count", + Help: "how many new members in a chat", + }, + []string{"host", "chat_name"}, +) diff --git a/prom/prom.go b/prom/prom.go index b65036cc..097a6d2e 100644 --- a/prom/prom.go +++ b/prom/prom.go @@ -86,3 +86,28 @@ func DailUpdate(update tgbotapi.Update, valied bool, costTime time.Duration) { "is_sticker": isSticker, })).Inc() } + +func NewMember(chatName string) { + chatMemberCount.With(prometheus.Labels{ + "host": host, + "chat_name": chatName, + }).Inc() + newMemberCount.With(prometheus.Labels{ + "host": host, + "chat_name": chatName, + }).Inc() +} + +func MemberLeft(chatName string) { + chatMemberCount.With(prometheus.Labels{ + "host": host, + "chat_name": chatName, + }).Desc() +} + +func GetMember(chatName string, num int) { + chatMemberCount.With(prometheus.Labels{ + "host": host, + "chat_name": chatName, + }).Set(float64(num)) +} From 3e02b2131b17fc642b59e5cf6c705b3dbdf2dd00 Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Fri, 22 Jan 2021 00:10:12 +0800 Subject: [PATCH 08/19] fix: print build version --- .github/workflows/test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 136f41ca..b2625eeb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,10 +29,15 @@ jobs: - name: "Get source" uses: actions/checkout@v2 + + - name: "Print Version" + run: | + echo 'Version:' $(git branch --show-current)-$(git rev-parse --short HEAD) - name: "Get dependences" run: | make get + - name: "Run Test" run: | make test From bb1f4f1909d822afd48f93595074d56b332da662 Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Fri, 22 Jan 2021 00:14:42 +0800 Subject: [PATCH 09/19] fix: checkout git repo before build docker image --- .github/workflows/docker_push.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index 9ba9bb11..e790a3ef 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -5,6 +5,18 @@ on: branches: [ dev ] jobs: + get-source: + name: "Checkout HEAD" + runs-on: ubuntu-latest + steps: + + - name: "Get source" + uses: actions/checkout@v2 + + - name: "Print Version" + run: | + echo 'Version:' $(git branch --show-current)-$(git rev-parse --short HEAD) + docker_build: name: "build and push docker image" runs-on: ubuntu-latest @@ -26,7 +38,7 @@ jobs: uses: docker/build-push-action@v2 with: push: true - tags: csust/csust-got:latest-test + tags: csust/csust-got:latest-dev - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file + run: echo ${{ steps.docker_build.outputs.digest }} From 50e6b585d3fd704bac812f1f552d59fffc67ec26 Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Fri, 22 Jan 2021 00:21:41 +0800 Subject: [PATCH 10/19] fix: checkout --- .github/workflows/docker_push.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index e790a3ef..9ed43d15 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -5,8 +5,9 @@ on: branches: [ dev ] jobs: - get-source: - name: "Checkout HEAD" + + docker_build: + name: "build and push docker image" runs-on: ubuntu-latest steps: @@ -17,10 +18,6 @@ jobs: run: | echo 'Version:' $(git branch --show-current)-$(git rev-parse --short HEAD) - docker_build: - name: "build and push docker image" - runs-on: ubuntu-latest - steps: - name: Set up QEMU uses: docker/setup-qemu-action@v1 From 7a90629553fff7ce0b6c4683551dbf867c9c9de8 Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Fri, 22 Jan 2021 01:03:52 +0800 Subject: [PATCH 11/19] Update Makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 34cb8376..104596e4 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ .PHONY: get build test fmt deploy run clean PROJECT := csust-got -VERSION := $(shell git rev-parse --short HEAD) -BRANCH := $(shell git branch --show-current) +VERSION ?= $(shell git rev-parse --short HEAD) +BRANCH ?= $(shell git branch --show-current) BUILDTIME := $(shell TZ="Asia/Shanghai" date '+%Y/%m/%d-%H:%M:%S') FLAGPKG = $(PROJECT)/base From accfef8ca5ccbb3d8be727d0ac39e6cf5f0eeb6b Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Fri, 22 Jan 2021 01:24:11 +0800 Subject: [PATCH 12/19] fix: change context folder --- .github/workflows/docker_push.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index 9ed43d15..15ea4cc2 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -34,6 +34,7 @@ jobs: id: docker_build uses: docker/build-push-action@v2 with: + context: . push: true tags: csust/csust-got:latest-dev From 3fa0ee0d30fd1916863a36ad3598d5f2f04ee60d Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Fri, 22 Jan 2021 01:45:54 +0800 Subject: [PATCH 13/19] Update Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 104596e4..487295ac 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: get build test fmt deploy run clean PROJECT := csust-got -VERSION ?= $(shell git rev-parse --short HEAD) +VERSION ?= $(if $(TAG),$(TAG),$(shell git rev-parse --short HEAD)) BRANCH ?= $(shell git branch --show-current) BUILDTIME := $(shell TZ="Asia/Shanghai" date '+%Y/%m/%d-%H:%M:%S') From 6894cf5c27c7584d532b6c95fbc3fc38b976fcba Mon Sep 17 00:00:00 2001 From: icceey Date: Fri, 22 Jan 2021 13:00:13 +0800 Subject: [PATCH 14/19] fix: fix metrics --- config/message.go | 3 +++ docker-compose.yml | 4 +++- log/log.go | 7 +++++++ prom/metrics.go | 11 ++++++++++- prom/prom.go | 13 +++++++++++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/config/message.go b/config/message.go index 036abf3f..62c846d3 100644 --- a/config/message.go +++ b/config/message.go @@ -2,6 +2,7 @@ package config import ( "github.com/spf13/viper" + "go.uber.org/zap" "reflect" ) @@ -30,6 +31,8 @@ func (c *messageConfig) checkConfig() { for i := 0; i < v.NumField(); i++ { s := v.Field(i).String() if s == "" { + zap.L().Warn("message config not set, use default value", + zap.String("key", v.Type().Field(i).Name)) v.Field(i).SetString(missMsg) } } diff --git a/docker-compose.yml b/docker-compose.yml index 5749ffa3..f2b79268 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,13 +16,15 @@ services: restart: always depends_on: - redis + volumes: + - ./config.yaml:/app/config.yaml links: - redis ports: - "8080:8080" redis: - image: "redis:alpine" + image: redis:alpine container_name: bot-redis <<: *common expose: diff --git a/log/log.go b/log/log.go index b0dba449..071235d0 100644 --- a/log/log.go +++ b/log/log.go @@ -2,6 +2,7 @@ package log import ( "csust-got/config" + "csust-got/prom" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -84,26 +85,32 @@ func prodConfig() zap.Config { func Debug(msg string, fields ...zap.Field) { logger.Debug(msg, fields...) + prom.Log(zap.DebugLevel.String()) } func Info(msg string, fields ...zap.Field) { logger.Info(msg, fields...) + prom.Log(zap.InfoLevel.String()) } func Warn(msg string, fields ...zap.Field) { logger.Warn(msg, fields...) + prom.Log(zap.WarnLevel.String()) } func Error(msg string, fields ...zap.Field) { logger.Error(msg, fields...) + prom.Log(zap.ErrorLevel.String()) } func Fatal(msg string, fields ...zap.Field) { logger.Fatal(msg, fields...) + prom.Log(zap.FatalLevel.String()) } func Panic(msg string, fields ...zap.Field) { logger.Panic(msg, fields...) + prom.Log(zap.PanicLevel.String()) } func Sync() { diff --git a/prom/metrics.go b/prom/metrics.go index f87e2a9e..a1bd2c56 100644 --- a/prom/metrics.go +++ b/prom/metrics.go @@ -40,7 +40,7 @@ var chatMemberCount = prometheus.NewGaugeVec( []string{"host", "chat_name"}, ) -// newMemberCount chatMemberCount +// newMemberCount newMemberCount var newMemberCount = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "bot_new_members_count", @@ -48,3 +48,12 @@ var newMemberCount = prometheus.NewCounterVec( }, []string{"host", "chat_name"}, ) + +// logCount logCount +var logCount = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "bot_log_count", + Help: "how many logs", + }, + []string{"host", "level"}, +) diff --git a/prom/prom.go b/prom/prom.go index 097a6d2e..dbdf7a2e 100644 --- a/prom/prom.go +++ b/prom/prom.go @@ -2,7 +2,6 @@ package prom import ( "csust-got/entities" - "csust-got/log" "go.uber.org/zap" "net/http" "os" @@ -19,12 +18,15 @@ func InitPrometheus() { prometheus.MustRegister(commandTimes) prometheus.MustRegister(messageCount) prometheus.MustRegister(updateCostTime) + prometheus.MustRegister(chatMemberCount) + prometheus.MustRegister(newMemberCount) + prometheus.MustRegister(logCount) http.Handle("/metrics", promhttp.Handler()) go func() { err := http.ListenAndServe(":8080", nil) if err != nil { - log.Error("InitPrometheus: Serve http failed", zap.Error(err)) + zap.L().Error("InitPrometheus: Serve http failed", zap.Error(err)) } }() } @@ -111,3 +113,10 @@ func GetMember(chatName string, num int) { "chat_name": chatName, }).Set(float64(num)) } + +func Log(level string) { + logCount.With(prometheus.Labels{ + "host": host, + "level": level, + }).Inc() +} From 52f1d5fed970f53d8f71bc70333e241725a70476 Mon Sep 17 00:00:00 2001 From: icceey Date: Fri, 22 Jan 2021 13:08:25 +0800 Subject: [PATCH 15/19] fix: fix metrics --- config/config.go | 5 +++++ config/message.go | 2 ++ config/orm.go | 2 ++ log/log.go | 1 + prom/prom.go | 1 + 5 files changed, 11 insertions(+) diff --git a/config/config.go b/config/config.go index b7ab187f..a7ebbbf8 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config import ( + "csust-got/prom" "fmt" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/spf13/viper" @@ -77,6 +78,8 @@ func initViper(configFile, envPrefix string) { zap.String("configFile", configFile), zap.Error(err)) } zap.L().Warn("config file is not available...", zap.String("configFile", configFile), zap.Error(err)) + prom.Log(zap.WarnLevel.String()) + prom.Log(zap.WarnLevel.String()) return } } @@ -108,9 +111,11 @@ func readConfig() { func checkConfig() { if BotConfig.Token == "" { zap.L().Panic(noTokenMsg) + prom.Log(zap.PanicLevel.String()) } if BotConfig.DebugMode { zap.L().Warn("DEBUG MODE IS ON") + prom.Log(zap.WarnLevel.String()) } if BotConfig.Worker <= 0 { BotConfig.Worker = 1 diff --git a/config/message.go b/config/message.go index 62c846d3..10d0b7ee 100644 --- a/config/message.go +++ b/config/message.go @@ -1,6 +1,7 @@ package config import ( + "csust-got/prom" "github.com/spf13/viper" "go.uber.org/zap" "reflect" @@ -33,6 +34,7 @@ func (c *messageConfig) checkConfig() { if s == "" { zap.L().Warn("message config not set, use default value", zap.String("key", v.Type().Field(i).Name)) + prom.Log(zap.WarnLevel.String()) v.Field(i).SetString(missMsg) } } diff --git a/config/orm.go b/config/orm.go index c2e0805e..831627f3 100644 --- a/config/orm.go +++ b/config/orm.go @@ -1,6 +1,7 @@ package config import ( + "csust-got/prom" "github.com/spf13/viper" "go.uber.org/zap" ) @@ -18,5 +19,6 @@ func (c *redisConfig) readConfig() { func (c *redisConfig) checkConfig() { if c.RedisAddr == "" { zap.L().Panic(noRedisMsg) + prom.Log(zap.PanicLevel.String()) } } diff --git a/log/log.go b/log/log.go index 071235d0..1d0c8347 100644 --- a/log/log.go +++ b/log/log.go @@ -27,6 +27,7 @@ func NewLogger() *zap.Logger { return tmpLogger } else { zap.L().Error("NewLogger failed, using default logger", zap.Error(err)) + prom.Log(zap.ErrorLevel.String()) } return zap.L() } diff --git a/prom/prom.go b/prom/prom.go index dbdf7a2e..0e395112 100644 --- a/prom/prom.go +++ b/prom/prom.go @@ -27,6 +27,7 @@ func InitPrometheus() { err := http.ListenAndServe(":8080", nil) if err != nil { zap.L().Error("InitPrometheus: Serve http failed", zap.Error(err)) + Log(zap.ErrorLevel.String()) } }() } From c90784dc2a85ef54b1144995c31eb64a1488e611 Mon Sep 17 00:00:00 2001 From: hugefiver Date: Sat, 23 Jan 2021 22:58:48 +0800 Subject: [PATCH 16/19] improve github action: build docker image fix --- .github/workflows/docker_push.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index 15ea4cc2..de17f2c4 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -3,6 +3,7 @@ name: Docker Image Push on: push: branches: [ dev ] + jobs: @@ -30,13 +31,15 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build and push + - name: Build and push when push to id: docker_build uses: docker/build-push-action@v2 with: context: . push: true - tags: csust/csust-got:latest-dev + repository: csust/csust-got + tags: | + latest-dev - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} From 2a02443cd350fdcc7346d8c35e9904e53d663a51 Mon Sep 17 00:00:00 2001 From: hugefiver Date: Sat, 23 Jan 2021 23:19:11 +0800 Subject: [PATCH 17/19] add gh action: release action for docker image --- .github/workflows/docker_push_release.yml | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 .github/workflows/docker_push_release.yml diff --git a/.github/workflows/docker_push_release.yml b/.github/workflows/docker_push_release.yml new file mode 100644 index 00000000..e2b604ba --- /dev/null +++ b/.github/workflows/docker_push_release.yml @@ -0,0 +1,49 @@ +name: Docker Image Push + +on: + release: + types: [ published ] + +jobs: + + docker_build: + name: "build and push docker image when release" + runs-on: ubuntu-latest + steps: + + - name: "Get source" + uses: actions/checkout@v2 + + - name: "Print Version" + id: version + run: | + echo "Version:" $(git branch --show-current)-$(git rev-parse --short HEAD) + echo "::set-output name=tag:: ${GITHUB_REF/refs\/tags\//}" + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push when push to + id: docker_build + uses: docker/build-push-action@v2 + with: + context: . + push: true + repository: csust/csust-got + tags: | + latest + ${{ steps.version.outputs.tag }} + args: | + TAG=${{ steps.version.outputs.tag}} + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} From 94017f1d88fd816a408a9e94a631ee7c54908317 Mon Sep 17 00:00:00 2001 From: hugefiver Date: Sat, 23 Jan 2021 23:22:46 +0800 Subject: [PATCH 18/19] fix --- .github/workflows/docker_push.yml | 3 +-- .github/workflows/docker_push_release.yml | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker_push.yml b/.github/workflows/docker_push.yml index de17f2c4..dafdf330 100644 --- a/.github/workflows/docker_push.yml +++ b/.github/workflows/docker_push.yml @@ -37,9 +37,8 @@ jobs: with: context: . push: true - repository: csust/csust-got tags: | - latest-dev + csust/csust-got:latest-dev - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/.github/workflows/docker_push_release.yml b/.github/workflows/docker_push_release.yml index e2b604ba..dffb218b 100644 --- a/.github/workflows/docker_push_release.yml +++ b/.github/workflows/docker_push_release.yml @@ -38,10 +38,9 @@ jobs: with: context: . push: true - repository: csust/csust-got tags: | - latest - ${{ steps.version.outputs.tag }} + csust/csust-got:latest + csust/csust-got:${{ steps.version.outputs.tag }} args: | TAG=${{ steps.version.outputs.tag}} From 2e90dc3ffa658c7a9f76398837d5003d39007e3a Mon Sep 17 00:00:00 2001 From: Hugefiver Date: Sun, 24 Jan 2021 22:55:24 +0800 Subject: [PATCH 19/19] Feature action (#35) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add gh action: release action for docker image * pick * pick * pick 3fa0ee0d30fd1916863a36ad3598d5f2f04ee60d * fix * fix * fix Co-authored-by: 长岛冰茶、 --- .github/workflows/docker_push_release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker_push_release.yml b/.github/workflows/docker_push_release.yml index dffb218b..98092880 100644 --- a/.github/workflows/docker_push_release.yml +++ b/.github/workflows/docker_push_release.yml @@ -18,7 +18,7 @@ jobs: id: version run: | echo "Version:" $(git branch --show-current)-$(git rev-parse --short HEAD) - echo "::set-output name=tag:: ${GITHUB_REF/refs\/tags\//}" + echo "::set-output name=tag::${GITHUB_REF/refs\/tags\//}" - name: Set up QEMU uses: docker/setup-qemu-action@v1 @@ -41,7 +41,7 @@ jobs: tags: | csust/csust-got:latest csust/csust-got:${{ steps.version.outputs.tag }} - args: | + build-args: | TAG=${{ steps.version.outputs.tag}} - name: Image digest