Env documentation, log, last hope handler
Signed-off-by: Terekhin Alexandr <alex@bearns.me>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
module bearns.me/didinst/tg-bot-example
|
||||
|
||||
go 1.15
|
||||
|
||||
require gopkg.in/tucnak/telebot.v2 v2.3.5
|
||||
|
||||
@@ -8,37 +8,42 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
port = os.Getenv("PORT")
|
||||
publicURL = os.Getenv("PUBLIC_URL")
|
||||
token = os.Getenv("TOKEN")
|
||||
apiURL = os.Getenv("API_URL")
|
||||
)
|
||||
var (
|
||||
port = os.Getenv("PORT") // Bot app listening port, same as port in API_URL
|
||||
publicURL = os.Getenv("PUBLIC_URL") // http://<telegram-api-server>:<port>
|
||||
token = os.Getenv("TOKEN") // Token from @BotFather
|
||||
apiURL = os.Getenv("API_URL") // http://<bot-app-host>:<bot-app-port>/<random-name-path>
|
||||
)
|
||||
|
||||
webhook := &tb.Webhook{
|
||||
Listen: ":" + port,
|
||||
Endpoint: &tb.WebhookEndpoint{PublicURL: publicURL},
|
||||
}
|
||||
webhook := &tb.Webhook{
|
||||
Listen: ":" + port,
|
||||
Endpoint: &tb.WebhookEndpoint{PublicURL: publicURL},
|
||||
}
|
||||
|
||||
pref := tb.Settings{
|
||||
Token: token,
|
||||
Poller: webhook,
|
||||
URL: apiURL,
|
||||
}
|
||||
|
||||
b, err := tb.NewBot(pref)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
pref := tb.Settings{
|
||||
Token: token,
|
||||
Poller: webhook,
|
||||
URL: apiURL,
|
||||
}
|
||||
|
||||
b, err := tb.NewBot(pref)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
b.Handle("/hello", func(m *tb.Message) {
|
||||
b.Send(m.Sender, "Hello World!")
|
||||
log.Print(m)
|
||||
_, err := b.Send(m.Sender, "Hello World!")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
})
|
||||
|
||||
b.Handle(tb.OnText, func(m *tb.Message) {
|
||||
log.Print(m)
|
||||
// all the text messages that weren't
|
||||
// captured by existing handlers
|
||||
})
|
||||
|
||||
b.Start()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user