Skip to content

Commit

Permalink
refactor: refactor endpoint url
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jul 6, 2023
1 parent 726bfad commit 2134057
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
2 changes: 1 addition & 1 deletion api/hook/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
panic(err)
}

telegram := telegram.New(&http.Client{})
telegram := telegram.New(&http.Client{}, telegram.BASE_URL)

bot := bot.NewBot(telegram, data)
res := make(map[string]string)
Expand Down
38 changes: 12 additions & 26 deletions utils/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,16 @@ import (
"zeril-bot/utils/structs"
)

const BASE_URL = "https://api.telegram.org"

type Telegram struct {
client *http.Client
client *http.Client
endpoint string
}

func New(http *http.Client) *Telegram {
return &Telegram{http}
}

func getApiURL(t string) string {
url := "https://api.telegram.org/bot" + os.Getenv("TELE_BOT_TOKEN")

switch t {
case "sendMessage":
return url + "/sendMessage"
case "sendPhoto":
return url + "/sendPhoto"
case "sendChatAction":
return url + "/sendChatAction"
case "getMyCommands":
return url + "/getMyCommands"
default:
return ""
}
func New(http *http.Client, baseUrl string) *Telegram {
endpoint := baseUrl + "/bot" + os.Getenv("TELE_BOT_TOKEN")
return &Telegram{client: http, endpoint: endpoint}
}

func (t Telegram) SendMessage(data structs.DataTele) error {
Expand All @@ -46,9 +33,8 @@ func (t Telegram) SendMessage(data structs.DataTele) error {
message += "\n@" + data.Username
}

url := getApiURL("sendMessage")
url := t.endpoint + "/sendMessage"
req, err := http.NewRequest("GET", url, nil)

if err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +92,7 @@ func (t Telegram) SendPhoto(data structs.DataTele, path string) error {

writer.Close()

url := getApiURL("sendPhoto")
url := t.endpoint + "/sendPhoto"
req, _ := http.NewRequest("GET", url, payload)
req.Header.Set("Content-Type", writer.FormDataContentType())

Expand Down Expand Up @@ -144,7 +130,7 @@ func (t Telegram) SendMessageWithReplyMarkup(data structs.DataTele, replyMark []
return err
}

url := getApiURL("sendMessage")
url := t.endpoint + "/sendMessage"
req, err := http.NewRequest("GET", url, bytes.NewReader(marshalled))
req.Header.Add("Content-Type", "application/json")
if err != nil {
Expand Down Expand Up @@ -192,7 +178,7 @@ func (t Telegram) SendMessageWithReplyMarkup(data structs.DataTele, replyMark []
func (t Telegram) SetTypingAction(data structs.DataTele) error {
chatId := data.ChatId

url := getApiURL("sendChatAction")
url := t.endpoint + "/sendChatAction"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
Expand Down Expand Up @@ -224,7 +210,7 @@ func (t Telegram) SetTypingAction(data structs.DataTele) error {
}

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

if err != nil {
Expand Down

0 comments on commit 2134057

Please sign in to comment.