/
main.go
53 lines (42 loc) · 1.09 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
package main
import (
"fmt"
"log"
"net/http"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/joho/godotenv"
"github.com/romanzh1/weather-averager/internal/message"
)
func MainHandler(resp http.ResponseWriter, _ *http.Request) {
resp.Write([]byte("Hi there! I'm WeatherAveragerBot!"))
}
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println(err) // TODO on heroku crashes if put a fatal shutdown
}
token := os.Getenv("BOT_TOKEN")
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Fatalln(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.ListenForWebhook("/" + bot.Token)
http.HandleFunc("/", MainHandler)
go http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if os.Getenv("PORT") == "" {
// long pooling (local) //TODO change environment variable for automatic switching
updates, err = bot.GetUpdatesChan(u)
if err != nil {
fmt.Println(err)
}
}
err = message.SendResponse(updates, bot)
if err != nil {
fmt.Println(err)
}
}