-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
// weatherAPIURL = "https://api.weather.yandex.ru/v2/informers"
weatherAPIURL = "https://api.weather.yandex.ru/v2/forecast"
)
type WeatherResponse struct {
Fact struct {
Temperature int `json:"temp"`
Condition string `json:"condition"`
} `json:"fact"`
}
func getWeather(city string, apiKey string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s?lat=%s&lon=%s", weatherAPIURL, "55.75396", "37.620393"), nil)
if err != nil {
return "", err
}
req.Header.Add("X-Yandex-API-Key", apiKey)
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to get weather: %s", resp.Status)
}
var weatherResponse WeatherResponse
if err := json.NewDecoder(resp.Body).Decode(&weatherResponse); err != nil {
return "", err
}
return fmt.Sprintf("Температура: %d°C, Условия: %s", weatherResponse.Fact.Temperature, weatherResponse.Fact.Condition), nil
}
func main() {
token := os.Getenv("TELEGRAM_BOT_TOKEN")
apiKey := os.Getenv("YANDEX_API_KEY")
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.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 non-Message Updates
continue
}
if update.Message.IsCommand() {
switch update.Message.Command() {
case "start":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Привет! Напиши название города, чтобы узнать погоду.")
bot.Send(msg)
default:
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Неизвестная команда. Используйте /start.")
bot.Send(msg)
}
} else {
city := update.Message.Text
weather, err := getWeather(city, apiKey)
if err != nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Ошибка получения погоды: "+err.Error())
bot.Send(msg)
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, weather)
bot.Send(msg)
}
}
}