/
main.go
47 lines (38 loc) · 1011 Bytes
/
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
package main
import (
"log"
"net/http"
"os"
"github.com/aws/aws-lambda-go/lambda"
"github.com/ubogdan/telegram-bot/app/handlers"
"github.com/ubogdan/telegram-bot/app/httpadapter"
telegram "gopkg.in/telegram-bot-api.v4"
)
func main() {
botToken, found := os.LookupEnv("TELEGRAM_BOT_TOKEN")
if !found {
log.Fatalf("BOT_TOKEN not found")
}
botHookURL, found := os.LookupEnv("TELEGRAM_BOT_WEBHOOK")
if !found {
log.Fatalf("TELEGRAM_BOT_WEBHOOK not found")
}
bot, err := telegram.NewBotAPI(botToken)
if err != nil {
log.Fatalf("newBot %s", err)
}
// Register http handler to http.DefaultServeMux
updates := bot.ListenForWebhook("/hooks/telegram")
// Register hook with telegram
_, err = bot.SetWebhook(telegram.NewWebhook(botHookURL))
if err != nil {
panic(err)
}
// Start listening for updates
go func() {
for update := range updates {
go handlers.DefaultCommandHandler.HandleMessage(bot, update.Message)
}
}()
lambda.Start(httpadapter.New(http.DefaultServeMux))
}