From 5b6ebdcaf0dfe3d4922a6cbdcf8855cb1cc43929 Mon Sep 17 00:00:00 2001 From: kerrek8 <62326380+kerrek8@users.noreply.github.com> Date: Sun, 3 Dec 2023 13:59:03 +0500 Subject: [PATCH] Add files via upload --- gobot/cmd/main/main.go | 68 ++++ gobot/go.mod | 8 + gobot/go.sum | 4 + gobot/internal/commands/comandsconfig.go | 20 ++ gobot/internal/consts/consts.go | 49 +++ gobot/internal/handlers/game/gamehandler.go | 67 ++++ gobot/internal/handlers/horo/horohandler.go | 65 ++++ .../handlers/lightning/lightninghandler.go | 22 ++ .../internal/handlers/weather/weatherfuncs.go | 316 ++++++++++++++++++ .../handlers/weather/weatherhandler.go | 31 ++ .../internal/handlers/worl_tour/world_tour.go | 21 ++ .../internal/keyboards/reply/gamekeyboard.go | 19 ++ .../internal/keyboards/reply/horokeyboard.go | 34 ++ .../keyboards/reply/weatherkeybord.go | 24 ++ gobot/internal/server/serve.go | 22 ++ gobot/internal/setup/botstart.go | 15 + gobot/internal/setup/handlersmassagetype.go | 32 ++ gobot/internal/setup/webhooksconfig.go | 22 ++ gobot/internal/structures/horo/horo.go | 22 ++ gobot/internal/structures/owm_days/days.go | 22 ++ .../structures/owm_geo_cur/geo_cur.go | 22 ++ 21 files changed, 905 insertions(+) create mode 100644 gobot/cmd/main/main.go create mode 100644 gobot/go.mod create mode 100644 gobot/go.sum create mode 100644 gobot/internal/commands/comandsconfig.go create mode 100644 gobot/internal/consts/consts.go create mode 100644 gobot/internal/handlers/game/gamehandler.go create mode 100644 gobot/internal/handlers/horo/horohandler.go create mode 100644 gobot/internal/handlers/lightning/lightninghandler.go create mode 100644 gobot/internal/handlers/weather/weatherfuncs.go create mode 100644 gobot/internal/handlers/weather/weatherhandler.go create mode 100644 gobot/internal/handlers/worl_tour/world_tour.go create mode 100644 gobot/internal/keyboards/reply/gamekeyboard.go create mode 100644 gobot/internal/keyboards/reply/horokeyboard.go create mode 100644 gobot/internal/keyboards/reply/weatherkeybord.go create mode 100644 gobot/internal/server/serve.go create mode 100644 gobot/internal/setup/botstart.go create mode 100644 gobot/internal/setup/handlersmassagetype.go create mode 100644 gobot/internal/setup/webhooksconfig.go create mode 100644 gobot/internal/structures/horo/horo.go create mode 100644 gobot/internal/structures/owm_days/days.go create mode 100644 gobot/internal/structures/owm_geo_cur/geo_cur.go diff --git a/gobot/cmd/main/main.go b/gobot/cmd/main/main.go new file mode 100644 index 0000000..cfcd093 --- /dev/null +++ b/gobot/cmd/main/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "gobot/internal/commands" + "gobot/internal/handlers/game" + "gobot/internal/handlers/horo" + "gobot/internal/handlers/lightning" + "gobot/internal/handlers/weather" + "gobot/internal/handlers/worl_tour" + "gobot/internal/server" + "gobot/internal/setup" + "log" + "os" +) + +func main() { + + bot := setup.Botstart(os.Getenv("BOT-TOKEN")) + //bot.Debug = true + setup.Webhooksstart(bot) + + com := commands.CommandsReq() + _, err := bot.Request(com) + if err != nil { + log.Fatal(err) + } + updates := bot.ListenForWebhook("/" + os.Getenv("BOT-TOKEN")) + + go server.Server() + + for update := range updates { + + if setup.IsNil(update) { // ignore any non-Message updates + continue + } + + if setup.IsCommand(update) { // command updates + switch update.Message.Command() { + case "weather": + weather.Weather(bot, update) + case "game": + game.Game(bot, update) + case "horo": + horo.Horo(bot, update) + case "calc": + lightning.Lightning(bot, update) + case "world_tour": + worl_tour.WorldHandler(bot, update) + } + } + + if setup.IsText(update) { //text update + + //log.Println(update.Message.Text[0], update.Message.Text[len(update.Message.Text)-1]) + if setup.IsWeather(update) { + weather.WeatherHandler(bot, update) + } + if setup.IsGame(update) { + game.GameHandler(bot, update) + } + if setup.IsHoro(update) { + horo.Horohandler(bot, update) + } + } + + //log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + } +} diff --git a/gobot/go.mod b/gobot/go.mod new file mode 100644 index 0000000..ac492e6 --- /dev/null +++ b/gobot/go.mod @@ -0,0 +1,8 @@ +module gobot + +go 1.21 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect +) diff --git a/gobot/go.sum b/gobot/go.sum new file mode 100644 index 0000000..4729886 --- /dev/null +++ b/gobot/go.sum @@ -0,0 +1,4 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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= diff --git a/gobot/internal/commands/comandsconfig.go b/gobot/internal/commands/comandsconfig.go new file mode 100644 index 0000000..4128042 --- /dev/null +++ b/gobot/internal/commands/comandsconfig.go @@ -0,0 +1,20 @@ +package commands + +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + +var weather = tgbotapi.BotCommand{Command: "/weather", Description: "Погода"} +var game = tgbotapi.BotCommand{Command: "/game", Description: "Игры"} +var horo = tgbotapi.BotCommand{Command: "/horo", Description: "Гороскопы"} +var lightning = tgbotapi.BotCommand{Command: "/calc", Description: "Калькулятор молний"} +var world_tour = tgbotapi.BotCommand{Command: "/world_tour", Description: "Путешествие по миру"} + +func CommandsReq() tgbotapi.SetMyCommandsConfig { + commands := tgbotapi.NewSetMyCommands( + weather, + game, + horo, + lightning, + world_tour, + ) + return commands +} diff --git a/gobot/internal/consts/consts.go b/gobot/internal/consts/consts.go new file mode 100644 index 0000000..ef21902 --- /dev/null +++ b/gobot/internal/consts/consts.go @@ -0,0 +1,49 @@ +package consts + +// ngrok http --domain=blessed-expert-parakeet.ngrok-free.app 80 + +//youtubekey = "AIzaSyDMeRhspy2tqx3oepQQ3fVTtmLF-UK7yrY" + +var World_tour = map[string]string{ + "в Болгарию": "AgACAgIAAxkBAAIEm2VkZAABIaUNvOUIXgF7bIx8jXUmzAACM9ExG94RIEtoSJhkEiDS9AEAAwIAA3MAAzME", + "в Испанию": "AgACAgIAAxkBAAIEmWVkY8-KMUpH1XecNcrG4kLwsj0aAAIw0TEb3hEgS_DtaCpesBvLAQADAgADcwADMwQ", + "на Кубу": "AgACAgIAAxkBAAIEn2VkZLtIhPeQgiwYMZAgGI6OfHgHAAI70TEb3hEgS1TZw6KLFPR0AQADAgADcwADMwQ", + "в Кипр": "AgACAgIAAxkBAAIEmmVkY-rQBC5yQGPe5Kdkgakc4aQzAAIx0TEb3hEgS6Af3-AiiDaPAQADAgADcwADMwQ", + "в Таиланд": "AgACAgIAAxkBAAIEo2VkZvS-yX3ob9SaQoxSw5VCdl3mAAJG0TEb3hEgS-7LKamvAvg-AQADAgADcwADMwQ", + "во Вьетнам": "AgACAgIAAxkBAAIEnmVkZKckwwFyhuCHGXW-WnRMrYtCAAI60TEb3hEgS-5HzHLxCfLcAQADAgADcwADMwQ", + "в Индию": "AgACAgIAAxkBAAIEnWVkZJUzwWyYyGdYhnELgoffNc1yAAI50TEb3hEgSwTYKv2JGxkUAQADAgADcwADMwQ", + "в Тунис": "AgACAgIAAxkBAAIEnGVkZINhYbdlpgk4h2iGPvOb7myZAAI40TEb3hEgS3QJFVsWvzWpAQADAgADcwADMwQ", + "в Армению": "AgACAgIAAxkBAAIEmGVkY7WOizS9i_UnrzKHHvAVmvPMAAIu0TEb3hEgSwiuKTBbVAYbAQADAgADcwADMwQ", + "в Египет": "AgACAgIAAxkBAAIEoGVkZM52GbawfInvx0OA-LI60JuqAAI80TEb3hEgSxDIyiU95iw3AQADAgADcwADMwQ", + "в Турцию": "AgACAgIAAxkBAAIEoWVkZOcCi8OVvbs1h9IpHL-bIXr2AAI90TEb3hEgSzZ58Zys_RnkAQADAgADcwADMwQ", + "в Россию": "AgACAgIAAxkBAAIEomVkZP18kBq7q-xvlEywvGB_Rz1TAAI-0TEb3hEgS-sWRzINA4h9AQADAgADcwADMwQ", + "в Дубай": "AgACAgIAAxkBAAIEkmVkX8vZ3mtgXjfLJnXJ1xEN0S0fAALy0DEb3hEgS1dFvgnzkKD7AQADAgADcwADMwQ", +} + +const ( + TymenLat = 57.1522 + TymenLon = 65.5272 + StrLat = 60.72411 + StrLon = 77.58138 +) + +const WeatherEndpoinCurrent = `https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s&units=metric&lang=ru` +const WeatherEndpoinDays = `https://api.openweathermap.org/data/2.5/forecast?lat=%f&lon=%f&cnt=%d&appid=%s&units=metric&lang=ru` + +var Transcript_dict = map[string]string{ + "♉": "Taurus", + "♈": "Aries", + "♎": "Libra", + "♑": "Capricorn", + "♏": "Scorpio", + "♒": "Aquarius", + "♓": "Pisces", + "♍": "Virgo", + "♌": "Leo", + "♊": "Gemini", + "♐": "Sagittarius", + "♋": "Cancer", + "Сегодня": "Today", + "Завтра": "Tomorrow", + "Послезавтра": "Tomorrow02", +} diff --git a/gobot/internal/handlers/game/gamehandler.go b/gobot/internal/handlers/game/gamehandler.go new file mode 100644 index 0000000..96a352a --- /dev/null +++ b/gobot/internal/handlers/game/gamehandler.go @@ -0,0 +1,67 @@ +package game + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "gobot/internal/keyboards/reply" +) + +func Game(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Выберите игру") + msg.ReplyMarkup = reply.Gamekeyboard + bot.Send(msg) +} + +func GameHandler(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + switch u.Message.Text { + case "⁤Выход⁤": + gameExit(bot, u) + case "⁤Кости⁤": + dice(bot, u) + case "⁤Автомат⁤": + automat(bot, u) + case "⁤Боулинг⁤": + bouling(bot, u) + case "⁤Футбол⁤": + football(bot, u) + case "⁤Баскетбол⁤": + basketball(bot, u) + case "⁤Дартс⁤": + darts(bot, u) + } +} + +func gameExit(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Хорошего настроения") + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) +} + +func automat(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewDiceWithEmoji(u.Message.Chat.ID, "🎰") + bot.Send(msg) +} + +func dice(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewDiceWithEmoji(u.Message.Chat.ID, "🎲") + bot.Send(msg) +} + +func football(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewDiceWithEmoji(u.Message.Chat.ID, "⚽") + bot.Send(msg) +} + +func basketball(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewDiceWithEmoji(u.Message.Chat.ID, "🏀") + bot.Send(msg) +} + +func darts(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewDiceWithEmoji(u.Message.Chat.ID, "🎯") + bot.Send(msg) +} + +func bouling(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewDiceWithEmoji(u.Message.Chat.ID, "🎳") + bot.Send(msg) +} diff --git a/gobot/internal/handlers/horo/horohandler.go b/gobot/internal/handlers/horo/horohandler.go new file mode 100644 index 0000000..bd411d0 --- /dev/null +++ b/gobot/internal/handlers/horo/horohandler.go @@ -0,0 +1,65 @@ +package horo + +import ( + "encoding/xml" + "fmt" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "gobot/internal/consts" + "gobot/internal/keyboards/reply" + "gobot/internal/structures/horo" + "io" + "log" + "net/http" + "reflect" +) + +var choose string +var day string + +func Horo(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Выберите знак зодиака") + msg.ReplyMarkup = reply.Horokeyboard1 + bot.Send(msg) +} + +func Horohandler(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + r := []rune(u.Message.Text) + if 9800 <= r[1] && r[1] <= 9811 { + horoDays(string(r[1]), bot, u) + } else { + day = string(r[1 : len(r)-1]) + + resp, err := http.Get("https://ignio.com/r/export/utf/xml/daily/com.xml") + if err != nil { + log.Println(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + var result horo.Horo + err = xml.Unmarshal(body, &result) + if err != nil { + log.Fatalln(err) + } + s := "" + s += fmt.Sprintf("Гороскоп для %s на %s:\n", choose, day) + a := reflect.ValueOf(result).FieldByName(consts.Transcript_dict[choose]).FieldByName(consts.Transcript_dict[day]) + s += fmt.Sprintf("%s", a.Interface()) + msg := tgbotapi.NewMessage(u.Message.Chat.ID, s) + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) + } +} + +func horoDays(r string, bot *tgbotapi.BotAPI, u tgbotapi.Update) { + choose = r + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Выберите на какой день вы хотите гороскоп") + msg.ReplyMarkup = reply.Horokeyboard2 + bot.Send(msg) +} + +//func getresult() { +// +//} diff --git a/gobot/internal/handlers/lightning/lightninghandler.go b/gobot/internal/handlers/lightning/lightninghandler.go new file mode 100644 index 0000000..ac5731e --- /dev/null +++ b/gobot/internal/handlers/lightning/lightninghandler.go @@ -0,0 +1,22 @@ +package lightning + +import ( + "fmt" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "strconv" + "strings" +) + +func Lightning(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + time := strings.Split(u.Message.CommandArguments(), " ") + t := time[len(time)-1] + r, err := strconv.Atoi(t) + if err != nil { + bot.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "Некорректное время\nВведите команду и через пробел время, прошедшее со вспышки до грома")) + return + } + r = r * 340 + msg := tgbotapi.NewMessage(u.Message.Chat.ID, fmt.Sprintf("Молния ударила на расстоянии %d метров", r)) + msg.ParseMode = "HTML" + bot.Send(msg) +} diff --git a/gobot/internal/handlers/weather/weatherfuncs.go b/gobot/internal/handlers/weather/weatherfuncs.go new file mode 100644 index 0000000..2db1757 --- /dev/null +++ b/gobot/internal/handlers/weather/weatherfuncs.go @@ -0,0 +1,316 @@ +package weather + +import ( + "encoding/json" + "fmt" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "gobot/internal/consts" + "gobot/internal/keyboards/reply" + "gobot/internal/structures/owm_days" + "gobot/internal/structures/owm_geo_cur" + "io" + "log" + "math" + "net/http" + "os" + "strconv" + "time" +) + +var lat float64 +var lon float64 +var delta int + +func tymen(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Выберите интересующую выас опцию") + msg.ReplyMarkup = reply.Weatherkeyboard + bot.Send(msg) + lat = consts.TymenLat + lon = consts.TymenLon + delta = 0 +} + +func strej(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Выберите интересующую выас опцию") + msg.ReplyMarkup = reply.Weatherkeyboard + bot.Send(msg) + lat = consts.StrLat + lon = consts.StrLon + delta = 2 +} + +func now(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + url := fmt.Sprintf(consts.WeatherEndpoinCurrent, lat, lon, os.Getenv("OWM-API")) + resp, err := http.Get(url) + if err != nil { + log.Fatalln(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + var result owm_geo_cur.OwmCurrent + err = json.Unmarshal(body, &result) + if err != nil { + log.Fatalln(err) + } + temp := math.Round(result.Main.Temp + 1) + feelslike := math.Round(result.Main.FeelsLike + 1) + speed := math.Round(result.Wind.Speed) + s := "" + s += fmt.Sprintf("🌤ПОГОДА СЕЙЧАС🌤\n") + s += fmt.Sprintf("Температура: %.0f°C\n", temp) + s += fmt.Sprintf("Ощущается: %.0f°C\n", feelslike) + s += fmt.Sprintf("Погода: %s\n", result.Weather[0].Description) + s += fmt.Sprintf("Облачность: %d%%\n", result.Clouds.All) + s += fmt.Sprintf("Скорость ветра: %.0fм/с\n", speed) + msg := tgbotapi.NewMessage(u.Message.Chat.ID, s) + msg.ParseMode = "HTML" + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) +} + +func today(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + cnt := 8 + url := fmt.Sprintf(consts.WeatherEndpoinDays, lat, lon, cnt, os.Getenv("OWM-API")) + resp, err := http.Get(url) + if err != nil { + log.Fatalln(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + var result owm_days.OwmDays + err = json.Unmarshal(body, &result) + r := result.List + if err != nil { + log.Fatalln(err) + } + s := "" + s += fmt.Sprintf("🌤ПОГОДА НА СЕГОДНЯ🌤\n") + s += fmt.Sprintf("🌤%s", r[0].DtTxt[8:10]) + "-" + fmt.Sprintf("%s", r[0].DtTxt[5:7]) + "-" + fmt.Sprintf("%s🌤", r[0].DtTxt[0:4]) + var listappend [][]string + for i := 0; i < len(r); i++ { + timebreak, _ := strconv.Atoi(r[i].DtTxt[11:13]) + timebreak += 3 + delta + if timebreak == 3 || timebreak >= 25 { + break + } + temp := strconv.Itoa(int(math.Round(r[i].Main.Temp + 1))) + feelslike := strconv.Itoa(int(math.Round(r[i].Main.FeelsLike + 1))) + cloudness := strconv.Itoa(r[i].Clouds.All) + dtt := strconv.Itoa(timebreak) + h := []string{dtt, temp, feelslike, r[i].Weather[0].Description, cloudness} + listappend = append(listappend, h) + + } + for _, v := range listappend { + s += fmt.Sprintf("\nВ %s:00 \n", v[0]) + s += fmt.Sprintf("Температура: %s\n", v[1]) + s += fmt.Sprintf("Ощущается как: %s\n", v[2]) + s += fmt.Sprintf("Погода: %s\n", v[3]) + s += fmt.Sprintf("Облачность: %s%%\n", v[4]) + } + + msg := tgbotapi.NewMessage(u.Message.Chat.ID, s) + msg.ParseMode = "HTML" + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) +} + +func tomor(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + cnt := 16 + url := fmt.Sprintf(consts.WeatherEndpoinDays, lat, lon, cnt, os.Getenv("OWM-API")) + resp, err := http.Get(url) + if err != nil { + log.Fatalln(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + var result owm_days.OwmDays + err = json.Unmarshal(body, &result) + r := result.List + if err != nil { + log.Fatalln(err) + } + s := "" + for { + date, _ := strconv.Atoi(r[0].DtTxt[8:10]) + datenow, _ := strconv.Atoi(time.Now().Format("02")) + if date == datenow { + r = r[1:] + } else { + + break + } + } + + s += fmt.Sprintf("🌤ПОГОДА НА ЗАВТРА🌤\n") + s += fmt.Sprintf("🌤%s", r[0].DtTxt[8:10]) + "-" + fmt.Sprintf("%s", r[0].DtTxt[5:7]) + "-" + fmt.Sprintf("%s🌤", r[0].DtTxt[0:4]) + var listappend [][]string + + for i := 0; i < len(r); i++ { + timebreak, _ := strconv.Atoi(r[i].DtTxt[11:13]) + timebreak += 3 + delta + datenow, _ := strconv.Atoi(time.Now().Format("02")) + date, _ := strconv.Atoi(r[i].DtTxt[8:10]) + if timebreak >= 25 || date == datenow+2 { + break + } + temp := strconv.Itoa(int(math.Round(r[i].Main.Temp + 1))) + feelslike := strconv.Itoa(int(math.Round(r[i].Main.FeelsLike + 1))) + cloudness := strconv.Itoa(r[i].Clouds.All) + dtt := strconv.Itoa(timebreak) + h := []string{dtt, temp, feelslike, r[i].Weather[0].Description, cloudness} + listappend = append(listappend, h) + + } + + for _, v := range listappend { + s += fmt.Sprintf("\nВ %s:00 \n", v[0]) + s += fmt.Sprintf("Температура: %s\n", v[1]) + s += fmt.Sprintf("Ощущается как: %s\n", v[2]) + s += fmt.Sprintf("Погода: %s\n", v[3]) + s += fmt.Sprintf("Облачность: %s%%\n", v[4]) + } + + msg := tgbotapi.NewMessage(u.Message.Chat.ID, s) + msg.ParseMode = "HTML" + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) +} + +func afterTomorrow(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + cnt := 35 + url := fmt.Sprintf(consts.WeatherEndpoinDays, lat, lon, cnt, os.Getenv("OWM-API")) + resp, err := http.Get(url) + if err != nil { + log.Fatalln(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + var result owm_days.OwmDays + err = json.Unmarshal(body, &result) + r := result.List + if err != nil { + log.Fatalln(err) + } + s := "" + for { + date, _ := strconv.Atoi(r[0].DtTxt[8:10]) + datenow, _ := strconv.Atoi(time.Now().Format("02")) + if date == datenow || date == datenow+1 { + r = r[1:] + } else { + break + } + } + + s += fmt.Sprintf("🌤ПОГОДА НА ПОСЛЕЗАВТРА🌤\n") + s += fmt.Sprintf("🌤%s", r[0].DtTxt[8:10]) + "-" + fmt.Sprintf("%s", r[0].DtTxt[5:7]) + "-" + fmt.Sprintf("%s🌤", r[0].DtTxt[0:4]) + var listappend [][]string + + for i := 0; i < len(r); i++ { + timebreak, _ := strconv.Atoi(r[i].DtTxt[11:13]) + timebreak += 3 + delta + datenow, _ := strconv.Atoi(time.Now().Format("02")) + date, _ := strconv.Atoi(r[i].DtTxt[8:10]) + if timebreak >= 25 || date == datenow+3 { + break + } + temp := strconv.Itoa(int(math.Round(r[i].Main.Temp + 1))) + feelslike := strconv.Itoa(int(math.Round(r[i].Main.FeelsLike + 1))) + cloudness := strconv.Itoa(r[i].Clouds.All) + dtt := strconv.Itoa(timebreak) + h := []string{dtt, temp, feelslike, r[i].Weather[0].Description, cloudness} + listappend = append(listappend, h) + + } + + for _, v := range listappend { + s += fmt.Sprintf("\nВ %s:00 \n", v[0]) + s += fmt.Sprintf("Температура: %s\n", v[1]) + s += fmt.Sprintf("Ощущается как: %s\n", v[2]) + s += fmt.Sprintf("Погода: %s\n", v[3]) + s += fmt.Sprintf("Облачность: %s%%\n", v[4]) + } + + msg := tgbotapi.NewMessage(u.Message.Chat.ID, s) + msg.ParseMode = "HTML" + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) +} + +func fiveDays(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + cnt := 40 + url := fmt.Sprintf(consts.WeatherEndpoinDays, lat, lon, cnt, os.Getenv("OWM-API")) + resp, err := http.Get(url) + if err != nil { + log.Fatalln(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + var result owm_days.OwmDays + err = json.Unmarshal(body, &result) + r := result.List + if err != nil { + log.Fatalln(err) + } + s := "" + s += fmt.Sprintf("🌤ПОГОДА НА 5 ДНЕЙ🌤\n") + s += fmt.Sprintf("🌤%s", r[0].DtTxt[8:10]) + "-" + fmt.Sprintf("%s", r[0].DtTxt[5:7]) + "-" + fmt.Sprintf("%s🌤", r[0].DtTxt[0:4]) + var listappend [][]string + + for i := 0; i < len(r); i++ { + timebreak, _ := strconv.Atoi(r[i].DtTxt[11:13]) + timebreak += 3 + delta + //datenow, _ := strconv.Atoi(time.Now().Format("02")) + //date, _ := strconv.Atoi(r[i].DtTxt[8:10]) + if timebreak >= 25 || timebreak == 3 { + continue + } + d := fmt.Sprintf("%s-%s-%s", r[i].DtTxt[8:10], r[i].DtTxt[5:7], r[i].DtTxt[:4]) + temp := strconv.Itoa(int(math.Round(r[i].Main.Temp + 1))) + feelslike := strconv.Itoa(int(math.Round(r[i].Main.FeelsLike + 1))) + cloudness := strconv.Itoa(r[i].Clouds.All) + dtt := strconv.Itoa(timebreak) + h := []string{d, dtt, temp, feelslike, r[i].Weather[0].Description, cloudness} + listappend = append(listappend, h) + + } + + for _, v := range listappend { + if v[1] == "6" || v[1] == "5" { + s += fmt.Sprintf("\n🌤%s🌤\n", v[0]) + s += fmt.Sprintf("\nВ %s:00 \n", v[1]) + s += fmt.Sprintf("Температура: %s\n", v[2]) + s += fmt.Sprintf("Ощущается как: %s\n", v[3]) + s += fmt.Sprintf("Погода: %s\n", v[4]) + s += fmt.Sprintf("Облачность: %s%%\n", v[5]) + } else { + s += fmt.Sprintf("\nВ %s:00 \n", v[1]) + s += fmt.Sprintf("Температура: %s\n", v[2]) + s += fmt.Sprintf("Ощущается как: %s\n", v[3]) + s += fmt.Sprintf("Погода: %s\n", v[4]) + s += fmt.Sprintf("Облачность: %s%%\n", v[5]) + } + + } + + msg := tgbotapi.NewMessage(u.Message.Chat.ID, s) + msg.ParseMode = "HTML" + msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + bot.Send(msg) +} diff --git a/gobot/internal/handlers/weather/weatherhandler.go b/gobot/internal/handlers/weather/weatherhandler.go new file mode 100644 index 0000000..8f214d3 --- /dev/null +++ b/gobot/internal/handlers/weather/weatherhandler.go @@ -0,0 +1,31 @@ +package weather + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "gobot/internal/keyboards/reply" +) + +func Weather(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Выберите город") + msg.ReplyMarkup = reply.DayKeyboard + bot.Send(msg) +} + +func WeatherHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update) { + switch update.Message.Text { + case "\U00002063Стрежевой\U00002063": + strej(bot, update) + case "\U00002063Тюмень\U00002063": + tymen(bot, update) + case "\U00002063Сейчас\U00002063": + now(bot, update) + case "\U00002063Сегодня\U00002063": + today(bot, update) + case "\U00002063Завтра\U00002063": + tomor(bot, update) + case "\U00002063Послезавтра\U00002063": + afterTomorrow(bot, update) + case "\U00002063На 5 дней\U00002063": + fiveDays(bot, update) + } +} diff --git a/gobot/internal/handlers/worl_tour/world_tour.go b/gobot/internal/handlers/worl_tour/world_tour.go new file mode 100644 index 0000000..a64310d --- /dev/null +++ b/gobot/internal/handlers/worl_tour/world_tour.go @@ -0,0 +1,21 @@ +package worl_tour + +import ( + "fmt" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "gobot/internal/consts" + "math/rand" + "reflect" + "time" +) + +func WorldHandler(bot *tgbotapi.BotAPI, u tgbotapi.Update) { + s := rand.NewSource(time.Now().Unix()) + r := rand.New(s) + keys := reflect.ValueOf(consts.World_tour).MapKeys() + key := fmt.Sprintf("%s", keys[r.Intn(len(keys))]) + file := tgbotapi.FileID(fmt.Sprintf("%s", consts.World_tour[key])) + m := tgbotapi.NewPhoto(u.Message.Chat.ID, file) + m.Caption = fmt.Sprintf("Вы попали %s", key) + bot.Send(m) +} diff --git a/gobot/internal/keyboards/reply/gamekeyboard.go b/gobot/internal/keyboards/reply/gamekeyboard.go new file mode 100644 index 0000000..31347d1 --- /dev/null +++ b/gobot/internal/keyboards/reply/gamekeyboard.go @@ -0,0 +1,19 @@ +package reply + +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + +var Gamekeyboard = tgbotapi.NewReplyKeyboard( + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002064Кости\U00002064"), + tgbotapi.NewKeyboardButton("\U00002064Дартс\U00002064"), + tgbotapi.NewKeyboardButton("\U00002064Футбол\U00002064"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002064Автомат\U00002064"), + tgbotapi.NewKeyboardButton("\U00002064Баскетбол\U00002064"), + tgbotapi.NewKeyboardButton("\U00002064Боулинг\U00002064"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002064Выход\U00002064"), + ), +) diff --git a/gobot/internal/keyboards/reply/horokeyboard.go b/gobot/internal/keyboards/reply/horokeyboard.go new file mode 100644 index 0000000..33822e9 --- /dev/null +++ b/gobot/internal/keyboards/reply/horokeyboard.go @@ -0,0 +1,34 @@ +package reply + +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + +var Horokeyboard1 = tgbotapi.NewReplyKeyboard( + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002062♉Телец♉\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♋Рак♋\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♐Стрелец♐\U00002062"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002062♑Козерог♑\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♏Скорпион♏\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♒Водолей♒\U00002062"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002062♓Рыбы♓\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♍Дева♍\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♈Овен♈\U00002062"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002062♌Лев♌\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♎Весы♎\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062♊Близнецы♊\U00002062"), + ), +) + +var Horokeyboard2 = tgbotapi.NewReplyKeyboard( + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002062Сегодня\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062Завтра\U00002062"), + tgbotapi.NewKeyboardButton("\U00002062Послезавтра\U00002062"), + ), +) diff --git a/gobot/internal/keyboards/reply/weatherkeybord.go b/gobot/internal/keyboards/reply/weatherkeybord.go new file mode 100644 index 0000000..33fcf2f --- /dev/null +++ b/gobot/internal/keyboards/reply/weatherkeybord.go @@ -0,0 +1,24 @@ +package reply + +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + +var Weatherkeyboard = tgbotapi.NewReplyKeyboard( + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002063Сейчас\U00002063"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002063Сегодня\U00002063"), + tgbotapi.NewKeyboardButton("\U00002063Завтра\U00002063"), + tgbotapi.NewKeyboardButton("\U00002063Послезавтра\U00002063"), + ), + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002063На 5 дней\U00002063"), + ), +) + +var DayKeyboard = tgbotapi.NewReplyKeyboard( + tgbotapi.NewKeyboardButtonRow( + tgbotapi.NewKeyboardButton("\U00002063Стрежевой\U00002063"), + tgbotapi.NewKeyboardButton("\U00002063Тюмень\U00002063"), + ), +) diff --git a/gobot/internal/server/serve.go b/gobot/internal/server/serve.go new file mode 100644 index 0000000..8a41c10 --- /dev/null +++ b/gobot/internal/server/serve.go @@ -0,0 +1,22 @@ +package server + +import ( + "fmt" + "log" + "net/http" +) + +func Server() { + http.HandleFunc("/", alive) + err := http.ListenAndServe(":80", nil) + if err != nil { + log.Fatalln(err) + } +} + +func alive(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + fmt.Fprint(w, "Alive") + } +} diff --git a/gobot/internal/setup/botstart.go b/gobot/internal/setup/botstart.go new file mode 100644 index 0000000..1feb3d8 --- /dev/null +++ b/gobot/internal/setup/botstart.go @@ -0,0 +1,15 @@ +package setup + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "log" +) + +func Botstart(token string) *tgbotapi.BotAPI { + bot, err := tgbotapi.NewBotAPI(token) + if err != nil { + log.Panic(err) + } + log.Printf("Authorized on account %s", bot.Self.UserName) + return bot +} diff --git a/gobot/internal/setup/handlersmassagetype.go b/gobot/internal/setup/handlersmassagetype.go new file mode 100644 index 0000000..158bd78 --- /dev/null +++ b/gobot/internal/setup/handlersmassagetype.go @@ -0,0 +1,32 @@ +package setup + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +func IsCommand(u tgbotapi.Update) bool { + return u.Message != nil && u.Message.IsCommand() +} + +func IsText(u tgbotapi.Update) bool { + return !u.Message.IsCommand() && u.Message != nil +} + +func IsNil(u tgbotapi.Update) bool { + return u.Message == nil +} + +func IsWeather(u tgbotapi.Update) bool { + r := []rune(u.Message.Text) + return string(r[0]) == "⁣" && string(r[len(r)-1]) == "⁣" +} + +func IsGame(u tgbotapi.Update) bool { + r := []rune(u.Message.Text) + return string(r[0]) == "⁤" && string(r[len(r)-1]) == "⁤" +} + +func IsHoro(u tgbotapi.Update) bool { + r := []rune(u.Message.Text) + return string(r[0]) == "⁢" && string(r[len(r)-1]) == "⁢" +} diff --git a/gobot/internal/setup/webhooksconfig.go b/gobot/internal/setup/webhooksconfig.go new file mode 100644 index 0000000..8ee7855 --- /dev/null +++ b/gobot/internal/setup/webhooksconfig.go @@ -0,0 +1,22 @@ +package setup + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "log" + "os" +) + +func Webhooksstart(bot *tgbotapi.BotAPI) { + wh, _ := tgbotapi.NewWebhook(os.Getenv("NGROK-URL") + "/" + os.Getenv("BOT-TOKEN")) + _, err := bot.Request(wh) + if err != nil { + log.Fatal(err) + } + info, err := bot.GetWebhookInfo() + if err != nil { + log.Fatal(err) + } + if info.LastErrorDate != 0 { + log.Printf("Telegram callback failed: %s", info.LastErrorMessage) + } +} diff --git a/gobot/internal/structures/horo/horo.go b/gobot/internal/structures/horo/horo.go new file mode 100644 index 0000000..4f283ce --- /dev/null +++ b/gobot/internal/structures/horo/horo.go @@ -0,0 +1,22 @@ +package horo + +type days struct { + Today string `xml:"today"` + Tomorrow string `xml:"tomorrow"` + Tomorrow02 string `xml:"tomorrow02"` +} + +type Horo struct { + Aries days `xml:"aries"` + Taurus days `xml:"taurus"` + Gemini days `xml:"gemini"` + Cancer days `xml:"cancer"` + Leo days `xml:"leo"` + Virgo days `xml:"virgo"` + Libra days `xml:"libra"` + Scorpio days `xml:"scorpio"` + Sagittarius days `xml:"sagittarius"` + Capricorn days `xml:"capricorn"` + Aquarius days `xml:"aquarius"` + Pisces days `xml:"pisces"` +} diff --git a/gobot/internal/structures/owm_days/days.go b/gobot/internal/structures/owm_days/days.go new file mode 100644 index 0000000..4a3ac55 --- /dev/null +++ b/gobot/internal/structures/owm_days/days.go @@ -0,0 +1,22 @@ +package owm_days + +type OwmDays struct { + Cnt int `json:"cnt"` + List []list `json:"list"` +} +type main struct { + Temp float64 `json:"temp"` + FeelsLike float64 `json:"feels_like"` +} +type weather struct { + Description string `json:"description"` +} +type clouds struct { + All int `json:"all"` +} +type list struct { + Main main `json:"main"` + Weather []weather `json:"weather"` + Clouds clouds `json:"clouds"` + DtTxt string `json:"dt_txt"` +} diff --git a/gobot/internal/structures/owm_geo_cur/geo_cur.go b/gobot/internal/structures/owm_geo_cur/geo_cur.go new file mode 100644 index 0000000..c848367 --- /dev/null +++ b/gobot/internal/structures/owm_geo_cur/geo_cur.go @@ -0,0 +1,22 @@ +package owm_geo_cur + +type OwmCurrent struct { + Weather []weather `json:"weather"` + Main main `json:"main"` + Visibility int `json:"visibility"` + Wind wind `json:"wind"` + Clouds clouds `json:"clouds"` +} +type weather struct { + Description string `json:"description"` +} +type main struct { + Temp float64 `json:"temp"` + FeelsLike float64 `json:"feels_like"` +} +type wind struct { + Speed float64 `json:"speed"` +} +type clouds struct { + All int `json:"all"` +}