package main import ( "encoding/json" "fmt" "log" "regexp" "strconv" "time" tb "gopkg.in/tucnak/telebot.v2" ) 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("/test", botTest) b.Handle("/msg_rescan", botMsgRescan) b.Handle("/msg_rescan_all", botMsgRescanAll) b.Handle("/msg_dump", botMsgDump) 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) // 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 botTest(m *tb.Message) { if !m.Private() { return } if _, ok := clientsKeepAlive[m.Chat.ID]; ok { c := TGCommand{ Type: commandSendMsg, Text: "🏅Me", FromUserID64: m.Chat.ID, ToChatID64: userID64ChtWrsBot, } MQTGCmdQueue <- c c = TGCommand{ Type: commandReplyMsg, Text: "Test sent", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botMsgRescan(m *tb.Message) { if !m.Private() { return } 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), MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) log.Printf("botMsgRescan : json : %s\n", string(b)) err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, int64(m.Sender.ID), time.Now(), b) logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)") if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Error scheduling the rescan for msg #%s", m.Payload), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Rescaning msg #%s", m.Payload), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } r = regexp.MustCompile("^all$") if r.MatchString(m.Payload) { botMsgRescanAll(m) } return } func botMsgRescanAll(m *tb.Message) { if !m.Private() { return } p := JobPayloadRescanMsg{ Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d ORDER BY id ASC;", objTypeMessage, objSubTypeMessageUnknown), MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, int64(m.Sender.ID), time.Now(), b) logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)") if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Error scheduling the rescan for all msg.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Rescaning all msg scheduled.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botMsgDump(m *tb.Message) { var res string r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { objId, _ := strconv.ParseInt(m.Payload, 10, 64) objTypeId, err := getObjTypeId(objId) logOnError(err, "botMsgDump : getObjSubTypeId") if err != nil { res = `Error retrieving the message` } else if objTypeId != objTypeMessage { res = `This is not a message reference` } else { cwm, _ := getMsg(objId) b, _ := json.Marshal(cwm) res = string(b) } } else { res = `/msg_dump ` } c := TGCommand{ Type: commandReplyMsg, Text: res, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return }