chirpnest/bot.go

81 lines
1.6 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
b.Handle("/hello", botHello)
2019-05-10 04:26:37 +02:00
b.Handle("/rescan_msg", botRescanMsg)
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
}
func botHello(m *tb.Message) {
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-03 05:58:36 +02:00
return
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
2019-05-10 04:39:16 +02:00
m, err := b.Send(m.Sender, "hello world")
logOnError(err, "botHello")
2019-05-10 04:32:22 +02:00
fmt.Println("botHello : hello world")
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
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)
2019-05-10 04:39:16 +02:00
m, err := b.Send(m.Sender, "rescan")
logOnError(err, "botRescanMsg")
2019-05-10 04:32:22 +02:00
fmt.Println("botRescanMsg : rescan")
2019-05-10 04:26:37 +02:00
}