-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.go
69 lines (56 loc) · 1.91 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
package main
import (
"fmt"
"os"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
"github.com/mymmrac/telego"
)
func main() {
botToken := os.Getenv("TOKEN")
// Note: Please keep in mind that default logger may expose sensitive information, use in development only
bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Set up a webhook on Telegram side
// Note: If you are keeping this call, you shouldn't use telego.WithWebhookSet option below
_ = bot.SetWebhook(&telego.SetWebhookParams{
URL: "https://example.com/bot" + bot.Token(),
})
// Receive information about webhook
info, _ := bot.GetWebhookInfo()
fmt.Printf("Webhook Info: %+v\n", info)
// Get an update channel from webhook, also all options are optional.
// Note: For one bot, only one webhook allowed.
updates, _ := bot.UpdatesViaWebhook("/bot"+bot.Token(),
// Set chan buffer (default 128)
telego.WithWebhookBuffer(128),
// Set fast http server that will be used to handle webhooks (default telego.FastHTTPWebhookServer)
// Note: If SecretToken is non-empty, it will be verified on each request
telego.WithWebhookServer(telego.FastHTTPWebhookServer{
Logger: bot.Logger(),
Server: &fasthttp.Server{},
Router: router.New(),
SecretToken: "token",
}),
// Calls SetWebhook before starting webhook
// Note: If you are using this option, you shouldn't call bot.SetWebhook above
telego.WithWebhookSet(&telego.SetWebhookParams{
URL: "https://example.com/bot" + bot.Token(),
}),
)
// Start server for receiving requests from the Telegram
go func() {
_ = bot.StartWebhook("localhost:443")
}()
// Stop reviving updates from update 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)
}
}