chirpnest/bot.go
2019-05-03 11:34:38 +08:00

61 lines
1.1 KiB
Go

package main
import (
"time"
"log"
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
)
var (
b *tb.Bot
)
func StartBot() {
// Registering bot
b, err := tb.NewBot(tb.Settings{
Token: cfg.Telegram.Token,
URL: cfg.Telegram.URL,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
return
}
b.Handle("/hello", botHello)
b.Handle(tb.OnPhoto, func(m *tb.Message) {
fmt.Println("OnPhoto :", m.Text)
// photos only
})
b.Handle(tb.OnChannelPost, func (m *tb.Message) {
fmt.Println("OnChannelPost :", m.Text)
PrintText(m)
// channel posts only
})
b.Handle(tb.OnQuery, func (q *tb.Query) {
fmt.Println("Query ?")
// incoming inline queries
})
b.Handle(tb.OnText, func(m *tb.Message) {
PrintText(m)
// all the text messages that weren't
// captured by existing handlers
})
b.Start()
}
func botHello(m *tb.Message) {
if !m.Private() {
return
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
b.Send(m.Sender, "hello world")
}