forked from mymmrac/telego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 1.61 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
package main
import (
"fmt"
"os"
"github.com/dsurl/tgo"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func main() {
botToken := os.Getenv("TOKEN")
// Note: Please keep in mind that default logger may expose sensitive information, use in development only
bot, err := tgo.NewBot(botToken, tgo.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Set up a webhook on Telegram side
_ = bot.SetWebhook(&tgo.SetWebhookParams{
URL: "https://example.com/bot" + bot.Token(),
})
// Receive information about webhook
info, _ := bot.GetWebhookInfo()
fmt.Printf("Webhook Info: %+v\n", info)
// Get updates channel from webhook, all options are optional.
// Note: For one bot only one webhook allowed.
updates, _ := bot.UpdatesViaWebhook("/bot"+bot.Token(),
// Set chan buffer (default 100)
tgo.WithWebhookBuffer(100),
// Set fast http server that will be used to handle webhooks (default &fasthttp.Server{})
tgo.WithWebhookServer(&fasthttp.Server{}),
// Set router to use, you can define your own routes (default router.New())
tgo.WithWebhookRouter(router.New()),
// Enable default health API on `/health` (default disabled)
// Note: Should be used only after tgo.WithWebhookRouter() if any
tgo.WithWebhookHealthAPI(),
)
// Start server for receiving requests from Telegram
_ = bot.StartListeningForWebhook("localhost:443")
// Stop reviving updates from updates channel and shutdown webhook server
defer func() {
_ = bot.StopWebhook()
}()
// Loop through all updates when they came
for update := range updates {
fmt.Printf("Update: %+v\n", update)
}
}