Skip to content

Commit

Permalink
refactor: refactor package telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jul 5, 2023
1 parent 7be9135 commit 726bfad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
4 changes: 2 additions & 2 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b Bot) setTypingAction() {

log.Printf("Yêu cầu từ bạn %s: %s", data.FirstName, rawMessage)

err := telegram.SetTypingAction(data)
err := b.Telegram.SetTypingAction(data)

defer func() {
b.rCh <- rChannel{err: err}
Expand Down Expand Up @@ -195,5 +195,5 @@ func (b Bot) getFirstName() string {
}

func (b Bot) getBotCommands() (*structs.BotCommands, error) {
return telegram.GetBotCommands()
return b.Telegram.GetBotCommands()
}
23 changes: 6 additions & 17 deletions utils/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func (t Telegram) SendPhoto(data structs.DataTele, path string) error {
req, _ := http.NewRequest("GET", url, payload)
req.Header.Set("Content-Type", writer.FormDataContentType())

client := &http.Client{}
res, err := client.Do(req)
res, err := t.client.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,9 +163,7 @@ func (t Telegram) SendMessageWithReplyMarkup(data structs.DataTele, replyMark []

req.URL.RawQuery = q.Encode()

client := &http.Client{}

res, err := client.Do(req)
res, err := t.client.Do(req)
if err != nil {
return err
}
Expand All @@ -192,7 +189,7 @@ func (t Telegram) SendMessageWithReplyMarkup(data structs.DataTele, replyMark []
return errors.New(string(body))
}

func SetTypingAction(data structs.DataTele) error {
func (t Telegram) SetTypingAction(data structs.DataTele) error {
chatId := data.ChatId

url := getApiURL("sendChatAction")
Expand All @@ -207,18 +204,14 @@ func SetTypingAction(data structs.DataTele) error {

req.URL.RawQuery = q.Encode()

client := &http.Client{}

res, err := client.Do(req)

res, err := t.client.Do(req)
if err != nil {
return err
}

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)

if err != nil {
return err
}
Expand All @@ -230,26 +223,22 @@ func SetTypingAction(data structs.DataTele) error {
return nil
}

func GetBotCommands() (*structs.BotCommands, error) {
func (t Telegram) GetBotCommands() (*structs.BotCommands, error) {
url := getApiURL("getMyCommands")
req, err := http.NewRequest("GET", url, nil)

if err != nil {
return nil, err
}

client := &http.Client{}

res, err := client.Do(req)

res, err := t.client.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)

if err != nil {
return nil, err
}
Expand Down

0 comments on commit 726bfad

Please sign in to comment.