chirpnest/bot.go
2019-05-10 10:45:38 +08:00

84 lines
1.6 KiB
Go

package main
import (
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"time"
)
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},
})
failOnError(err, "StartBot")
b.Handle("/hello", func(m *tb.Message) {
botHello(m)
})
b.Handle("/rescan_msg", botRescanMsg)
b.Handle(tb.OnPhoto, botPhoto)
b.Handle(tb.OnChannelPost, botChannelPost)
b.Handle(tb.OnQuery, botQuery)
b.Handle(tb.OnText, botText)
b.Start()
}
func botPhoto(m *tb.Message) {
fmt.Println("botPhoto :", m.Text)
// photos only
}
func botHello(m *tb.Message) {
fmt.Println("botHello :", m.Text)
if !m.Private() {
fmt.Println("botHello : !m.Private()")
return
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
m, err := b.Send(m.Sender, "hello world")
logOnError(err, "botHello")
fmt.Println("botHello : hello world")
}
func botChannelPost(m *tb.Message) {
fmt.Println("botChannelPost :", m.Text)
PrintText(m)
// channel posts only
}
func botQuery(q *tb.Query) {
fmt.Println("botQuery")
// incoming inline queries
}
func botText(m *tb.Message) {
fmt.Println("botText :", m.Text)
PrintText(m)
// all the text messages that weren't
// captured by existing handlers
}
func botRescanMsg(m *tb.Message) {
fmt.Println("botRescanMsg :", m.Text)
if !m.Private() {
fmt.Println("botRescanMsg : !m.Private()")
return
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
m, err := b.Send(m.Sender, "rescan")
logOnError(err, "botRescanMsg")
fmt.Println("botRescanMsg : rescan")
}