Skip to content

Commit

Permalink
Merge pull request #36 from CSUSTers/dev
Browse files Browse the repository at this point in the history
Merge branch `dev`
  • Loading branch information
hugefiver authored Jan 24, 2021
2 parents 2196823 + d878c3b commit ca95cad
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 75 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/docker_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ on:


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"
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions base/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -88,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
Expand Down Expand Up @@ -131,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)
}

Expand Down
17 changes: 17 additions & 0 deletions base/helper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package base

import (
"csust-got/log"
"csust-got/prom"
"csust-got/util"
"fmt"
"go.uber.org/zap"
"runtime"
"time"

Expand Down Expand Up @@ -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)
}
7 changes: 2 additions & 5 deletions base/hitokoto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base

import (
"csust-got/config"
"csust-got/entities"
"csust-got/log"
"csust-got/orm"
Expand All @@ -15,10 +16,6 @@ import (
"go.uber.org/zap"
)

const errMessage = `过去那些零碎的细语并不构成这个世界:对于你而言,该看,该想,该体会身边那些微小事物的律动。
忘了这些话吧。忘了这个功能吧——只今它已然不能给予你更多。而你的未来属于新的旅途:去欲望、去收获、去爱、去恨。
去做只属于你自己的选择,写下只有你深谙个中滋味的诗篇。我们的生命以后可能还会交织之时,但如今,再见辣。`

// HitokotoResponse is HitokotoResponse
type HitokotoResponse struct {
ID int `json:"id"`
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 13 additions & 4 deletions base/notification.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package base

import (
"csust-got/prom"
"csust-got/util"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

// 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
Expand All @@ -15,11 +17,18 @@ 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)
messageR := tgbotapi.NewMessage(message.Chat.ID, text)
util.SendMessage(bot, messageR)
prom.NewMember(message.Chat.Title)
}
}

func sendNotificationTo(bot *tgbotapi.BotAPI, chatID int64, text string) {
message := tgbotapi.NewMessage(chatID, text)
util.SendMessage(bot, message)
// 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)
}
6 changes: 0 additions & 6 deletions base/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions base/search-engines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ message:
links: "NO LINKS"
restrict_bot: "好 的, 我 杀 我 自 己。"
fake_ban_in_cd: ""
hitokoto_not_found: "过去那些零碎的细语并不构成这个世界:对于你而言,该看,该想,该体会身边那些微小事物的律动。忘了这些话吧。忘了这个功能吧——只今它已然不能给予你更多。而你的未来属于新的旅途:去欲望、去收获、去爱、去恨。去做只属于你自己的选择,写下只有你深谙个中滋味的诗篇。我们的生命以后可能还会交织之时,但如今,再见辣。"
no_sleep: "睡你麻痹起来嗨!"
boot_failed: "我不愿面对这苦涩的一天……:("
22 changes: 16 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -16,28 +17,32 @@ 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)
readConfig()
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
}

Expand All @@ -53,8 +58,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.
Expand All @@ -73,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
}
}
Expand Down Expand Up @@ -100,12 +107,15 @@ 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)
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
Expand Down
17 changes: 14 additions & 3 deletions config/message.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package config

import (
"csust-got/prom"
"github.com/spf13/viper"
"go.uber.org/zap"
"reflect"
)

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() {
v := reflect.ValueOf(c).Elem()
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))
prom.Log(zap.WarnLevel.String())
v.Field(i).SetString(missMsg)
}
}
Expand Down
2 changes: 2 additions & 0 deletions config/orm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"csust-got/prom"
"github.com/spf13/viper"
"go.uber.org/zap"
)
Expand All @@ -18,5 +19,6 @@ func (c *redisConfig) readConfig() {
func (c *redisConfig) checkConfig() {
if c.RedisAddr == "" {
zap.L().Panic(noRedisMsg)
prom.Log(zap.PanicLevel.String())
}
}
32 changes: 16 additions & 16 deletions config/special_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ca95cad

Please sign in to comment.