Skip to content

Commit

Permalink
refactor: all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jun 27, 2023
1 parent e596fe7 commit f2999f8
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 584 deletions.
80 changes: 0 additions & 80 deletions api/hook/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@ package hook

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"zeril-bot/utils/bitcoin"
"zeril-bot/utils/bot"
"zeril-bot/utils/channel"
"zeril-bot/utils/help"
"zeril-bot/utils/kqxs"
"zeril-bot/utils/lunar"
"zeril-bot/utils/qr"
"zeril-bot/utils/random"
"zeril-bot/utils/shortener"
"zeril-bot/utils/structs"
"zeril-bot/utils/weather"
)

func Handler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -48,71 +36,3 @@ func Response(w http.ResponseWriter, res map[string]string, httpStatus int) {
mRes, _ := json.Marshal(res)
w.Write(mRes)
}

func resolveCommand(data structs.HookData) {
name := data.Message.From.FirstName
chatId := data.Message.Chat.ID
text := data.Message.Text
arr := strings.Fields(text)

setBotIsTyping(chatId)

log.Println(fmt.Sprintf("Yêu cầu từ bạn %s: %s", name, text))

command := arr[0]

switch command {
case "/start", "/start@zerill_bot":
help.SendStartMessage(chatId, name)
case "/help", "/help@zerill_bot":
help.SendHelpMessage(chatId)
case "/groupid", "/groupid@zerill_bot":
help.SendGroupId(chatId, string(data.Message.Chat.Type))
case "/quote", "/quote@zerill_bot":
// quote.SendAQuote(chatId)
case "/lunar", "/lunar@zerill_bot":
lunar.SendLunarDateNow(chatId)
case "/weather", "/weather@zerill_bot":
weather.SendForecastOfWeather(chatId, text)
case "/bitcoin", "/bitcoin@zerill_bot":
bitcoin.SendBitcoinPrice(chatId)
case "/qr", "/qr@zerill_bot":
qr.SendQRImage(chatId, text)
case "/random", "/random@zerill_bot":
random.RandomElements(chatId, text)
case "/kqxs", "/kqxs@zerill_bot":
kqxs.Send(chatId, text)
case "/shortener", "/shortener@zerill_bot":
shortener.Generate(chatId, text)
default:
channel.SendMessage(chatId, "Tôi không hiểu câu lệnh của bạn !!!")
}
}

func resolveCallback(callback structs.HookData) {
name := callback.CallbackQuery.Message.From.FirstName
chatId := callback.CallbackQuery.Message.Chat.ID
text := callback.CallbackQuery.Message.Text
data := callback.CallbackQuery.Data

setBotIsTyping(chatId)

log.Println(fmt.Sprintf("Yêu cầu từ bạn %s: %s, callback data: %s", name, text, data))

arr := strings.Fields(data)
command := arr[0]

switch command {
case "/weather":
weather.SendForecastOfWeather(chatId, data)
case "/kqxs":
kqxs.Send(chatId, data)
}
}

func setBotIsTyping(chatId int) {
go func() {
// bot.SetTypingAction(chatId)
// channel.GetWg().Done()
}()
}
6 changes: 3 additions & 3 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
)

func Handler(wri http.ResponseWriter, req *http.Request) {

r := chi.NewRouter()
r.Use(chiMiddle.Logger)
// r.Use(middleware.PreRequest)
r.Use(middleware.Recoverer)

r.NotFound(middleware.Handle404NotFound())
r.MethodNotAllowed(middleware.Handle405MethodNotAllowed())

// r.Get("/", func(w http.ResponseWriter, r *http.Request) {})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hi there !"))
})
r.Post("/api/hook", hook.Handler)
r.Get("/url", url.Handler)
r.Get("/trip", trip.Handler)
Expand Down
53 changes: 31 additions & 22 deletions utils/bitcoin/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,87 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"zeril-bot/utils/channel"
"zeril-bot/utils/structs"
"zeril-bot/utils/telegram"

"github.com/leekchan/accounting"
)

func SendBitcoinPrice(chatId int) {
func SendBitcoinPrice(data structs.DataTele) error {
acUsd := accounting.Accounting{Symbol: "$", Precision: 2}
acVnd := accounting.Accounting{Symbol: "", Precision: 0, Thousand: "."}

btc := getBitcoinPrice()
p, _ := strconv.ParseFloat(btc.Price, 64)
btc, err := getBitcoinPrice()
if err != nil {
return err
}

p, err := strconv.ParseFloat(btc.Price, 64)
if err != nil {
return err
}

usd := acUsd.FormatMoney(p)
v, err := exchangeUsdToVnd(p)
if err != nil {
return err
}

v := exchangeUsdToVnd(p)
vnd := acVnd.FormatMoney(v) + " đ"
vnd := acVnd.FormatMoney(*v) + " đ"

message := fmt.Sprintf("1 Bitcoin = %s (<b>%s</b>)", usd, vnd)

channel.SendMessage(chatId, message)
data.ReplyMessage = message

return telegram.SendMessage(data)
}

func getBitcoinPrice() structs.Btc {
func getBitcoinPrice() (*structs.Btc, error) {
res, err := http.Get("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")

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

defer res.Body.Close()

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

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

var data structs.Btc

err = json.Unmarshal(body, &data)
if err != nil {
log.Panic(err)
return nil, err
}

return data
return &data, nil
}

func exchangeUsdToVnd(p float64) float64 {
func exchangeUsdToVnd(p float64) (*float64, error) {
price := fmt.Sprintf("%.2f", p)
res, err := http.Get("https://api.exchangerate.host/convert?from=USD&to=VND&amount=" + price)

res, err := http.Get("https://api.exchangerate.host/convert?from=USD&to=VND&amount=" + price)
if err != nil {
log.Panic(err)
return nil, err
}

defer res.Body.Close()

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

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

var data structs.Exchange

err = json.Unmarshal(body, &data)
if err != nil {
log.Panic(err)
return nil, err
}

return data.Result
return &data.Result, nil
}
Loading

0 comments on commit f2999f8

Please sign in to comment.