chirpnest/bot.go

91 lines
1.7 KiB
Go
Raw Normal View History

2019-05-03 05:58:36 +02:00
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},
})
2019-05-10 04:26:37 +02:00
failOnError(err, "StartBot")
2019-05-03 05:58:36 +02:00
2019-05-10 04:43:54 +02:00
b.Handle("/hello", func(m *tb.Message) {
2019-05-10 04:52:14 +02:00
s, err := botHello(m)
logOnError(err, "/hello")
if err == nil {
b.Send(m.Sender, s)
}
2019-05-10 04:43:54 +02:00
})
2019-05-10 04:58:28 +02:00
b.Handle("/msg_rescan", func(m *tb.Message) {
s, err := botMsgRescan(m)
logOnError(err, "/msg_rescan")
if err == nil {
b.Send(m.Sender, s)
}
})
2019-05-03 05:58:36 +02:00
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) {
2019-05-10 04:26:37 +02:00
fmt.Println("botPhoto :", m.Text)
2019-05-03 05:58:36 +02:00
// photos only
}
2019-05-10 04:50:40 +02:00
func botHello(m *tb.Message) (string, error) {
2019-05-10 04:26:37 +02:00
fmt.Println("botHello :", m.Text)
2019-05-03 05:58:36 +02:00
if !m.Private() {
2019-05-10 04:26:37 +02:00
fmt.Println("botHello : !m.Private()")
2019-05-10 04:51:15 +02:00
return ``, nil
2019-05-03 05:58:36 +02:00
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
2019-05-10 04:51:15 +02:00
return `hello world`, nil
2019-05-03 05:58:36 +02:00
}
func botChannelPost(m *tb.Message) {
2019-05-10 04:26:37 +02:00
fmt.Println("botChannelPost :", m.Text)
2019-05-03 05:58:36 +02:00
PrintText(m)
// channel posts only
}
func botQuery(q *tb.Query) {
2019-05-10 04:26:37 +02:00
fmt.Println("botQuery")
2019-05-03 05:58:36 +02:00
// incoming inline queries
}
func botText(m *tb.Message) {
2019-05-10 04:26:37 +02:00
fmt.Println("botText :", m.Text)
2019-05-03 05:58:36 +02:00
PrintText(m)
// all the text messages that weren't
// captured by existing handlers
}
2019-05-10 04:26:37 +02:00
2019-05-10 04:58:28 +02:00
func botMsgRescan(m *tb.Message) (string, error) {
2019-05-10 04:26:37 +02:00
fmt.Println("botRescanMsg :", m.Text)
if !m.Private() {
fmt.Println("botRescanMsg : !m.Private()")
2019-05-10 04:59:05 +02:00
return ``, nil
2019-05-10 04:26:37 +02:00
}
2019-05-10 04:58:28 +02:00
fmt.Println("botRescanMsg payload :", m.Payload) // <PAYLOAD>
2019-05-10 04:26:37 +02:00
PrintText(m)
2019-05-10 04:59:05 +02:00
return `rescaning`, nil
2019-05-10 04:26:37 +02:00
}