diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e33ac77
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+
+
+*.idea
+*.env
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/CatFacts.iml b/.idea/CatFacts.iml
new file mode 100644
index 0000000..5e764c4
--- /dev/null
+++ b/.idea/CatFacts.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml
new file mode 100644
index 0000000..8d75c2c
--- /dev/null
+++ b/.idea/dataSources.xml
@@ -0,0 +1,12 @@
+
+
+
+
+ postgresql
+ true
+ org.postgresql.Driver
+ jdbc:postgresql://localhost:5432/postgres
+ $ProjectFileDir$
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..691101b
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/cmd/catFacts/main.go b/cmd/catFacts/main.go
index 04f8cba..4fb79f4 100644
--- a/cmd/catFacts/main.go
+++ b/cmd/catFacts/main.go
@@ -1,11 +1,11 @@
package main
import (
- run "github.com/bahodurnazarov/CatFacts/internal/bot"
+ "github.com/bahodurnazarov/CatFacts/internal/Job"
"github.com/bahodurnazarov/CatFacts/internal/handler"
)
func main() {
- go run.Route()
+ go Job.Route()
handler.Listen()
}
diff --git a/go.mod b/go.mod
index 72412ae..a634352 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,8 @@ go 1.19
require (
github.com/bas24/googletranslatefree v0.0.0-20220326200502-05ed9e639439
github.com/gin-gonic/gin v1.9.0
+ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
+ github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.10.2
github.com/lib/pq v1.10.9
github.com/sirupsen/logrus v1.9.0
diff --git a/go.sum b/go.sum
index 86b8d79..ce6901c 100644
--- a/go.sum
+++ b/go.sum
@@ -20,6 +20,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.13.0 h1:cFRQdfaSMCOSfGCCLB20MHvuoHb/s5G8L5pu2ppK5AQ=
github.com/go-playground/validator/v10 v10.13.0/go.mod h1:dwu7+CG8/CtBiJFZDz4e+5Upb6OLw04gtBYw0mcG/z4=
+github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
+github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
@@ -28,6 +30,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
diff --git a/internal/Job/ListenBot.go b/internal/Job/ListenBot.go
new file mode 100644
index 0000000..26e7e1e
--- /dev/null
+++ b/internal/Job/ListenBot.go
@@ -0,0 +1,36 @@
+package Job
+
+import (
+ "fmt"
+ lg "github.com/bahodurnazarov/CatFacts/pkg/utils"
+ "io/ioutil"
+ "net/http"
+ "time"
+)
+
+func Route() {
+ var timeDuration = 1
+ timer := time.NewTicker(time.Second * time.Duration(timeDuration))
+ defer timer.Stop()
+
+ for {
+ select {
+ case <-timer.C:
+ resp, err := http.Get("http://localhost:1323/bot")
+
+ if err != nil {
+ lg.Errl.Fatal(err)
+ }
+
+ defer resp.Body.Close()
+
+ body, err := ioutil.ReadAll(resp.Body)
+
+ if err != nil {
+ lg.Errl.Fatal(err)
+ }
+
+ fmt.Println(string(body))
+ }
+ }
+}
diff --git a/internal/bot/ToDB.go b/internal/bot/ToDB.go
new file mode 100644
index 0000000..d3bf761
--- /dev/null
+++ b/internal/bot/ToDB.go
@@ -0,0 +1,17 @@
+package bot
+
+import (
+ d "github.com/bahodurnazarov/CatFacts/pkg/db"
+ lg "github.com/bahodurnazarov/CatFacts/pkg/utils"
+)
+
+func InsertToDB(factEN, factRU string) {
+ db := d.ConnDB()
+ if factEN != "" {
+ _, err := db.Exec("INSERT into facts VALUES ($1, $2)", factEN, factRU)
+ if err != nil {
+ lg.Errl.Fatalf("111An error occured while executing query: %v", err)
+ }
+ }
+
+}
diff --git a/internal/bot/bot.go b/internal/bot/bot.go
new file mode 100644
index 0000000..a6431a1
--- /dev/null
+++ b/internal/bot/bot.go
@@ -0,0 +1,85 @@
+package bot
+
+import (
+ g "github.com/bahodurnazarov/CatFacts/internal/getFacts"
+ lg "github.com/bahodurnazarov/CatFacts/pkg/utils"
+ gt "github.com/bas24/googletranslatefree"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ "github.com/labstack/echo/v4"
+ "log"
+ "net/http"
+ "os"
+)
+
+var numericKeyboard = tgbotapi.NewReplyKeyboard(
+ tgbotapi.NewKeyboardButtonRow(
+ tgbotapi.NewKeyboardButton("Select Language 🇬🇧"),
+ ),
+ tgbotapi.NewKeyboardButtonRow(
+ tgbotapi.NewKeyboardButton("Выберите Язык 🇷🇺"),
+ ),
+)
+var facts = tgbotapi.NewReplyKeyboard(
+ tgbotapi.NewKeyboardButtonRow(
+ tgbotapi.NewKeyboardButton("/facts"),
+ ),
+)
+var fact = tgbotapi.NewReplyKeyboard(
+ tgbotapi.NewKeyboardButtonRow(
+ tgbotapi.NewKeyboardButton("/fact"),
+ ),
+)
+
+func Bot(c echo.Context) error {
+ token := os.Getenv("BOT_TOKEN")
+ bot, err := tgbotapi.NewBotAPI(token)
+ if err != nil {
+ lg.Errl.Fatal(err)
+ }
+ bot.Debug = true
+ log.Printf("Authorized on account %s", bot.Self.UserName)
+ u := tgbotapi.NewUpdate(0)
+ u.Timeout = 60
+
+ updates := bot.GetUpdatesChan(u)
+ for update := range updates {
+ if update.Message == nil { // ignore any non-Message updates
+ continue
+ }
+
+ // Create a new MessageConfig. We don't have text yet,
+ // so we leave it empty.
+ msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
+ image := g.GetImage()
+ //Extract the command from the Message.
+ switch update.Message.Command() {
+ case "start":
+ msg.ReplyMarkup = numericKeyboard
+ case "facts":
+ factEN := g.GetFacts()
+ factRU, _ := gt.Translate(factEN, "en", "ru")
+ InsertToDB(factEN, factRU)
+ msg.Text = image + "\n\n\"" + factEN + "\""
+ case "fact":
+ factEN := g.GetFacts()
+ factRU, _ := gt.Translate(factEN, "en", "ru")
+ InsertToDB(factEN, factRU)
+ msg.Text = image + "\n\n\"" + factRU + "\""
+ default:
+ msg.Text = "Неверная команда!"
+ }
+
+ if update.Message.Text == "Select Language 🇬🇧" {
+ msg.ReplyMarkup = facts
+ msg.Text = "To get started, select the command \"Facts\""
+ } else if update.Message.Text == "Выберите Язык 🇷🇺" {
+ msg.ReplyMarkup = fact
+ msg.Text = "Чтобы начать, выберете команду \"Факты\""
+ }
+
+ if _, err := bot.Send(msg); err != nil {
+ lg.Errl.Fatal(err)
+ }
+ }
+ return c.JSON(http.StatusOK, bot)
+}
diff --git a/internal/getFacts/image.go b/internal/getFacts/image.go
new file mode 100644
index 0000000..614be95
--- /dev/null
+++ b/internal/getFacts/image.go
@@ -0,0 +1,48 @@
+package getfacts
+
+import (
+ "encoding/json"
+ lg "github.com/bahodurnazarov/CatFacts/pkg/utils"
+ "io/ioutil"
+ "net/http"
+)
+
+type ImgStr struct {
+ Url string `json:"url"`
+}
+
+func GetImage() string {
+ url := "https://api.thecatapi.com/v1/images/search"
+ method := "GET"
+
+ client := &http.Client{}
+ req, err := http.NewRequest(method, url, nil)
+
+ if err != nil {
+ lg.Errl.Println(err)
+
+ }
+ res, err := client.Do(req)
+ if err != nil {
+ lg.Errl.Println(err)
+
+ }
+ defer res.Body.Close()
+
+ body, err := ioutil.ReadAll(res.Body)
+ if err != nil {
+ lg.Errl.Println(err)
+
+ }
+
+ var data []ImgStr
+ jsonErr := json.Unmarshal(body, &data)
+ if jsonErr != nil {
+ lg.Errl.Fatal(jsonErr)
+ }
+ var sterr string
+ for _, values := range data {
+ sterr = values.Url
+ }
+ return sterr
+}
diff --git a/internal/handler/handler.go b/internal/handler/handler.go
index fa0f552..5d5bcc9 100644
--- a/internal/handler/handler.go
+++ b/internal/handler/handler.go
@@ -1,11 +1,13 @@
package handler
import (
+ fd "github.com/bahodurnazarov/CatFacts/pkg/init"
"html/template"
"io"
"net/http"
//lg "github.com/bahodurnazarov/CatFacts/pkg/utils"
+ "github.com/bahodurnazarov/CatFacts/internal/bot"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
@@ -27,6 +29,7 @@ func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c
}
func Listen() {
+ fd.Init()
e := echo.New()
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"https://labstack.com", "https://labstack.net"},
@@ -39,7 +42,8 @@ func Listen() {
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
+
+ e.GET("/bot", bot.Bot)
e.GET("/home", HomeHandler)
e.Logger.Fatal(e.Start(":1323"))
-
}
diff --git a/internal/handler/home.go b/internal/handler/home.go
index 72c1844..723de8a 100644
--- a/internal/handler/home.go
+++ b/internal/handler/home.go
@@ -1,18 +1,17 @@
package handler
import (
- "html/template"
- "net/http"
-
g "github.com/bahodurnazarov/CatFacts/internal/getFacts"
- gt "github.com/bas24/googletranslatefree"
d "github.com/bahodurnazarov/CatFacts/pkg/db"
lg "github.com/bahodurnazarov/CatFacts/pkg/utils"
+ gt "github.com/bas24/googletranslatefree"
"github.com/labstack/echo/v4"
+ "html/template"
+ "net/http"
)
func HomeHandler(c echo.Context) error {
-
+ image := g.GetImage()
factEN := g.GetFacts()
factRU, _ := gt.Translate(factEN, "en", "ru")
@@ -36,6 +35,7 @@ func HomeHandler(c echo.Context) error {
return c.Render(http.StatusOK, "home.html", map[string]interface{}{
"name": "HOME",
+ "image": image,
"factEN": factEN,
"factRU": factRU,
})
diff --git a/pkg/assets/html/home.html b/pkg/assets/html/home.html
index ab5f757..5e2254c 100644
--- a/pkg/assets/html/home.html
+++ b/pkg/assets/html/home.html
@@ -1,8 +1,9 @@
-{{define "home.html"}}
+
+
Fact about Cats