chirpnest/bot.go
2019-05-11 19:22:50 +08:00

137 lines
3.6 KiB
Go

package main
import (
"encoding/json"
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"log"
"regexp"
"time"
)
func BotHandlers(b *tb.Bot) {
b.Handle("/hello", func(m *tb.Message) {
s, err := botHello(m)
logOnError(err, "/hello")
if err == nil {
b.Send(m.Sender, s)
}
})
b.Handle("/msg_rescan", func(m *tb.Message) {
s, err := botMsgRescan(m)
logOnError(err, "/msg_rescan")
if err == nil {
b.Send(m.Sender, s)
}
})
b.Handle("/msg_rescan_all", func(m *tb.Message) {
s, err := botMsgRescanAll(m)
logOnError(err, "/msg_rescan_all")
if err == nil {
b.Send(m.Sender, s)
}
})
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) (string, error) {
fmt.Println("botHello :", m.Text)
if !m.Private() {
fmt.Println("botHello : !m.Private()")
return ``, nil
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
return `hello world`, nil
}
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 botMsgRescan(m *tb.Message) (string, error) {
fmt.Println("botRescanMsg :", m.Text)
if !m.Private() {
fmt.Println("botRescanMsg : !m.Private()")
return ``, nil
}
r := regexp.MustCompile("^[0-9]+$")
if r.MatchString(m.Payload) {
p := JobPayloadRescanMsg{
Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.id = %s AND o.obj_type_id = %d AND o.obj_sub_type_id = %d;", m.Payload, objTypeMessage, objSubTypeMessageUnknown),
}
b, _ := json.Marshal(p)
log.Printf("botMsgRescan : json : %s\n", string(b))
err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, 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
}
}
r = regexp.MustCompile("^all$")
if r.MatchString(m.Payload) {
p := JobPayloadRescanMsg{
Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d;", objTypeMessage, objSubTypeMessageUnknown),
}
b, _ := json.Marshal(p)
log.Printf("botMsgRescan : json : %s\n", string(b))
err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, time.Now(), b)
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
if err != nil {
return "Error scheduling the rescan for all msg", nil
} else {
return "Rescaning all msg scheduled", nil
}
}
return "/msg_rescan msg_id or /msg_rescan all", nil
}
func botMsgRescanAll(m *tb.Message) (string, error) {
fmt.Println("botRescanMsgAll :", m.Text)
if !m.Private() {
fmt.Println("botRescanMsg : !m.Private()")
return ``, nil
}
p := JobPayloadRescanMsg{
Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d;", objTypeMessage, objSubTypeMessageUnknown),
}
b, _ := json.Marshal(p)
log.Printf("botMsgRescan : json : %s\n", string(b))
err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, time.Now(), b)
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
if err != nil {
return "Error scheduling the rescan for all msg", nil
} else {
return "Rescaning all msg scheduled", nil
}
}