Simple telegram bot to use as template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tg-bot-example/main.go

49 lines
975 B

package main
import (
"log"
"os"
tb "gopkg.in/tucnak/telebot.v2"
)
func main() {
var (
port = os.Getenv("PORT") // Bot app listening port, same as port in API_URL
publicURL = os.Getenv("PUBLIC_URL") // http://<bot-app-host>:<bot-app-port>/<random-name-path>
token = os.Getenv("TOKEN") // Token from @BotFather
apiURL = os.Getenv("API_URL") // http://<telegram-api-server>:<port>
)
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)
}
b.Handle("/hello", func(m *tb.Message) {
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()
}