2019-05-03 05:58:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2019-05-10 13:05:25 +02:00
|
|
|
"regexp"
|
2019-05-03 05:58:36 +02:00
|
|
|
"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 13:04:21 +02:00
|
|
|
r := regexp.MustCompile("^[0-9]+$")
|
|
|
|
if r.MatchString(m.Payload) {
|
2019-05-10 15:57:01 +02:00
|
|
|
m, _ := strconv.ParseInt(m.Payload, 10, 64)
|
|
|
|
p := JobPayloadRescanMsg{
|
|
|
|
MsgID64: m,
|
|
|
|
}
|
|
|
|
b, _ := json.Marshal(p)
|
|
|
|
err := createJob(objSubTypeJobRescanMsg, 2, time.Now(), b)
|
|
|
|
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
|
|
|
|
if err != nil {
|
|
|
|
return "Error scheduling the rescan for msg #" + m.Payload, nil
|
|
|
|
} else {
|
|
|
|
return "Rescaning msg #" + m.Payload, nil
|
|
|
|
}
|
|
|
|
|
2019-05-10 13:04:21 +02:00
|
|
|
}
|
2019-05-10 13:05:25 +02:00
|
|
|
r = regexp.MustCompile("^all$")
|
2019-05-10 13:04:21 +02:00
|
|
|
if r.MatchString(m.Payload) {
|
2019-05-10 15:57:01 +02:00
|
|
|
m, _ := strconv.ParseInt(m.Payload, 10, 64)
|
|
|
|
p := JobPayloadRescanAllMsg{}
|
|
|
|
b, _ := json.Marshal(p)
|
|
|
|
err := createJob(objSubTypeJobRescanAllMsg, 3, time.Now(), b)
|
|
|
|
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanAllMsg)")
|
|
|
|
if err != nil {
|
|
|
|
return "Error scheduling the rescan for all msg", nil
|
|
|
|
} else {
|
|
|
|
return "Rescaning all msg scheduled", nil
|
|
|
|
}
|
2019-05-10 13:04:21 +02:00
|
|
|
}
|
2019-05-10 15:57:01 +02:00
|
|
|
return "/msg_rescan msg_id or /msg_rescan all", nil
|
2019-05-10 04:26:37 +02:00
|
|
|
}
|