Skip to content

Commit

Permalink
refactor: refactor channel
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jun 28, 2023
1 parent f2999f8 commit f9aa397
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
55 changes: 30 additions & 25 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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õ <code>/help</code> để 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
Expand Down
12 changes: 6 additions & 6 deletions utils/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -209,21 +208,22 @@ func SetTypingAction(data structs.DataTele) {
res, err := client.Do(req)

if err != nil {
log.Println(err)
return
return err
}

defer res.Body.Close()

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 {
Expand Down

0 comments on commit f9aa397

Please sign in to comment.