From f9aa3970a51aaeded7a9a2cb6f65b9d8cdd34a75 Mon Sep 17 00:00:00 2001 From: totanvix Date: Wed, 28 Jun 2023 12:13:34 +0700 Subject: [PATCH] refactor: refactor channel --- utils/bot/bot.go | 55 +++++++++++++++++++++----------------- utils/telegram/telegram.go | 12 ++++----- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/utils/bot/bot.go b/utils/bot/bot.go index 95da57c..8a9eda9 100644 --- a/utils/bot/bot.go +++ b/utils/bot/bot.go @@ -18,21 +18,21 @@ import ( ) type Bot struct { - HookData structs.HookData - typingCh chan struct{} - commandCh chan error + HookData structs.HookData + rCh chan rChannel +} + +type rChannel struct { + err error } func NewBot(hookData structs.HookData) *Bot { - tCh := make(chan struct{}) - commandCh := make(chan error) + ch := make(chan rChannel, 2) - return &Bot{hookData, tCh, commandCh} + return &Bot{HookData: hookData, rCh: ch} } func (b Bot) ResolveHook() error { - var err error - go b.setTypingAction() switch { @@ -42,19 +42,30 @@ func (b Bot) ResolveHook() error { go b.resolveCallbackCommand() } - // to do refactor - <-b.typingCh - err = <-b.commandCh + for r := range b.rCh { + if r.err != nil { + return r.err + } + } - return err + return nil } -func (b Bot) getTelegramData() structs.DataTele { - data := b.HookData +func (b Bot) setTypingAction() { + data := b.getTelegramData() rawMessage := b.getRawMessage() - name := data.Message.From.FirstName - log.Printf("Yêu cầu từ bạn %s: %s", name, rawMessage) + log.Printf("Yêu cầu từ bạn %s: %s", data.FirstName, rawMessage) + + err := telegram.SetTypingAction(data) + + defer func() { + b.rCh <- rChannel{err: err} + }() +} + +func (b Bot) getTelegramData() structs.DataTele { + rawMessage := b.getRawMessage() return structs.DataTele{ ChatId: b.getChatId(), @@ -70,7 +81,7 @@ func (b Bot) resolveCommand() error { var err error defer func() { - b.commandCh <- err + b.rCh <- rChannel{err: err} }() data := b.getTelegramData() @@ -109,7 +120,8 @@ func (b Bot) resolveCallbackCommand() error { var err error defer func() { - b.commandCh <- err + b.rCh <- rChannel{err: err} + close(b.rCh) }() data := b.getTelegramData() @@ -181,13 +193,6 @@ func (b Bot) getFirstName() string { return b.HookData.Message.From.FirstName } -func (b Bot) setTypingAction() { - defer close(b.typingCh) - - data := b.getTelegramData() - telegram.SetTypingAction(data) -} - func (b Bot) sendStartMessage(data structs.DataTele) error { message := fmt.Sprintf("Xin chào %s \n\nGõ /help để xem danh sách các lệnh mà bot hỗ trợ nhé.\n\nBạn cũng có thể truy cập nhanh các chức năng bằng cách nhấn nút Menu bên dưới.", data.FirstName) data.ReplyMessage = message diff --git a/utils/telegram/telegram.go b/utils/telegram/telegram.go index 1ffccb6..cf84fb6 100644 --- a/utils/telegram/telegram.go +++ b/utils/telegram/telegram.go @@ -188,14 +188,13 @@ func SendMessageWithReplyMarkup(data structs.DataTele, replyMark []structs.Butto return errors.New(string(body)) } -func SetTypingAction(data structs.DataTele) { +func SetTypingAction(data structs.DataTele) error { chatId := data.ChatId url := getApiURL("sendChatAction") req, err := http.NewRequest("GET", url, nil) if err != nil { - log.Println(err) - return + return err } q := req.URL.Query() @@ -209,8 +208,7 @@ func SetTypingAction(data structs.DataTele) { res, err := client.Do(req) if err != nil { - log.Println(err) - return + return err } defer res.Body.Close() @@ -218,12 +216,14 @@ func SetTypingAction(data structs.DataTele) { body, err := ioutil.ReadAll(res.Body) if err != nil { - log.Panic(err) + return err } if body != nil { log.Println("SetTypingAction OK") } + + return nil } func GetBotCommands() structs.BotCommands {