-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
76 lines (70 loc) · 2.02 KB
/
server.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
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/net/http2"
)
func start() {
bot, err := tgbotapi.NewBotAPI(config.token)
if err != nil {
log.Printf("Init Bot error %s", err.Error())
return
}
log.Printf("Authorized on account %s", bot.Self.UserName)
// in case of webhook issue
bot.RemoveWebhook()
if config.hook {
_, err = bot.SetWebhook(tgbotapi.NewWebhook(fmt.Sprintf("https://%s/ybot/%s", config.domain, bot.Token)))
if err != nil {
log.Printf("Set webhook error %s", err.Error())
return
}
updates := bot.ListenForWebhook("/ybot/" + bot.Token)
if config.acme {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(config.domain), //your domain here
Cache: autocert.DirCache("certs"), //folder for storing certificates
Email: config.mail,
}
go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // 支持 http-01
server := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
NextProtos: []string{http2.NextProtoTLS, "http/1.1"},
MinVersion: tls.VersionTLS12,
},
MaxHeaderBytes: 32 << 20,
}
go server.ListenAndServeTLS("", "")
} else {
go http.ListenAndServe(fmt.Sprintf(":%d", config.prot), nil)
}
log.Println("Start ybot in webhook mode")
startBot(updates, bot)
} else {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Printf("Init pull mode error %s", err.Error())
return
}
log.Println("Start ybot in pull mode")
//add google cloud run support
if port := os.Getenv("PORT"); port != "" {
// run a fake http server
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("hello ybot"))
})
go http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
}
startBot(updates, bot)
}
}