Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zong-Z committed Mar 20, 2021
1 parent 2d9a94f commit 8fd6834
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 26 deletions.
13 changes: 2 additions & 11 deletions betypes/chat.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package betypes

import (
"strings"
"telegram-chat_bot/logger"

"github.com/google/uuid"
)

// NewChat returns the *Chat with the UUID.
func NewChat() *Chat {
UUID := uuid.New().String()
if strings.EqualFold(UUID, "") {
err := "Error, bad UUID."
logger.ForInfo(err)
panic(err)
}

return &Chat{
Users: make([]User, 0),
ID: UUID,
ID: uuid.New().String(),
}
}

Expand Down Expand Up @@ -69,5 +59,6 @@ func (c *Chat) IsUserInChat(userID int) bool {
return true
}
}

return false
}
18 changes: 12 additions & 6 deletions betypes/chats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package betypes
//
// The StartAddingUsers function starts working in goroutine.
func NewChats(usersCount, buffer int) *Chats {
c := &Chats{
chats := &Chats{
Chats: make([]Chat, 0),
UsersCount: usersCount,
Queue: make(chan User, buffer),
}

go c.StartAddingUsers()
go chats.StartAddingUsers()

return c
return chats
}

// StartAddingUsers takes the user from the queue.
Expand Down Expand Up @@ -56,10 +56,16 @@ func (c *Chats) FindSuitableChatsForUser(user User) []*Chat {
continue
}

u := c.Chats[i].Users[len(c.Chats[i].Users)-1]
if user.IsSuitableAge(u) && user.IsSuitableCity(u) && user.IsSuitableSex(u) {
func(suitable ...func(User) bool) {
u := c.Chats[i].Users[len(c.Chats[i].Users)-1]
for _, f := range suitable {
if !f(u) {
return
}
}

chats = append(chats, &c.Chats[i])
}
}(user.IsSuitableAge, user.IsSuitableCity, user.IsSuitableSex)
}

if len(chats) != 0 {
Expand Down
4 changes: 2 additions & 2 deletions betypes/texts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ var text Text
func init() {
b, err := ioutil.ReadFile(textsFile)
if err != nil {
logger.ForInfo(err.Error())
logger.ForWarning(err.Error())
}

err = json.Unmarshal(b, &text)
if err != nil {
logger.ForInfo(err.Error())
logger.ForWarning(err.Error())
}
}

Expand Down
2 changes: 1 addition & 1 deletion configs/configs.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bot]
webhook = "{WEBHOOK}" # If the field is empty("") or webhook is invalid, the bot starts using "Polling".
webhook = "{WEBHOOK}" # If the field is empty(""), the bot starts using "Polling".
token = "{TOKEN}" # Telegram bot token.
port = "8090" # If you wont to change this bot port, you also need change bot port in "docker-compose.yml".
channel_id = "" # If the field is empty("") or ID is invalid, the bot does not check the group subscription.
Expand Down
3 changes: 1 addition & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ func ForWarning(v ...interface{}) {
func ForError(v ...interface{}) {
str := fmt.Sprintf("%s |ERROR|: %v\n", time.Now().Format(TimeFormat), v)
logFile.Println(str)
log.Println(str)
panic(str)
log.Fatalln(str)
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func main() {

var updates tgbotapi.UpdatesChannel
if !strings.EqualFold(betypes.GetConfig().Bot.Webhook, "") {
updates, err = setWebhook(bot)
updates, err = setWebhook(betypes.GetConfig().Bot.Webhook, bot)
if err != nil {
logger.ForWarning(err.Error())
logger.ForError(err.Error())
}
}

Expand Down Expand Up @@ -210,8 +210,8 @@ func setPolling(offset, limit, timeout int, bot *tgbotapi.BotAPI) (tgbotapi.Upda
return updates, nil
}

func setWebhook(bot *tgbotapi.BotAPI) (tgbotapi.UpdatesChannel, error) {
_, err := bot.SetWebhook(tgbotapi.NewWebhook(betypes.GetConfig().Bot.Webhook))
func setWebhook(webhook string, bot *tgbotapi.BotAPI) (tgbotapi.UpdatesChannel, error) {
_, err := bot.SetWebhook(tgbotapi.NewWebhook(webhook))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8fd6834

Please sign in to comment.