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("/test_delay", botTestDelay) b.Handle("/test_html", botTestHTML) b.Handle("/msg_rescan", botMsgRescan) b.Handle("/msg_rescan_all", botMsgRescanAll) b.Handle("/msg_dump", botMsgDump) b.Handle("/parse_rules", botListParsingRules) b.Handle("/parse_rule", botListParsingRule) b.Handle("/timer", botTimer) b.Handle("/g_stock", botGStock) b.Handle("/g_deposit_all", botGDepositAll) b.Handle("/backup_export", botBackupExport) b.Handle("/backup_import", botBackupImport) b.Handle("/help", botHelp) b.Handle("/get_item_id", botGetItemId) b.Handle("/clients", botGetClients) b.Handle(tb.OnPhoto, botPhoto) b.Handle(tb.OnChannelPost, botChannelPost) b.Handle(tb.OnQuery, botQuery) b.Handle(tb.OnText, botText) b.Handle(tb.OnDocument, botDocument) b.Start() } func botPhoto(m *tb.Message) { fmt.Println("botPhoto :", m.Text) // photos only } func botDocument(m *tb.Message) { fmt.Printf("botDocument : %s (%d bytes)\n", m.Document.FileName, m.Document.File.FileSize) // documents 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 botHelp(m *tb.Message) { if !m.Private() { return } c := TGCommand{ Type: commandReplyMsg, Text: `/help - this help /msg_rescan - rescan one message /msg_rescan_all - rescan all messages /parse_rules - list parsing rules\n /parse_rule - detail for one rule /timer "msg" - schedule msg for client in ETA /g_stock - check guild's vault /backup_export - export message database /backup_import - import message database from URL /get_item_id - identify item_id from string /clients - list all connected clients`, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } func botTestHTML(m *tb.Message) { if !m.Private() { return } c := TGCommand{ Type: commandReplyMsg, Text: `bold, bold, italic, italic, inline URL, inline fixed-width code,
pre-formatted fixed-width code block
`, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, ParseMode: cmdParseModeHTML, } TGCmdQueue <- c return } func botTest(m *tb.Message) { if !m.Private() { return } if clt, ok := getLockedClient(m.Chat.ID, false); ok { clt.Mux.Unlock() clientSendCWMsg(m.Chat.ID, "🏅Me") 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 botTestDelay(m *tb.Message) { if !m.Private() { return } if clt, ok := getLockedClient(m.Chat.ID, false); ok { clt.Mux.Unlock() clientSendCWMsgDelay(m.Chat.ID, "🏅Me", 5) 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 botGetClients(m *tb.Message) { if !m.Private() { return } if clt, ok := getLockedClient(m.Chat.ID, false); ok { clt.Mux.Unlock() muxClients.RLock() var ret string for id, c := range clients { if c.Active { ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (online)\n", ret, c.Login, c.CWUserID64, id) } else { ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (offline)\n", ret, c.Login, c.CWUserID64, id) } } muxClients.RUnlock() c := TGCommand{ Type: commandReplyMsg, Text: ret, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, ParseMode: cmdParseModeHTML, } 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 } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c 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), 0, time.Now().UTC(), 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 } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c 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), 0, time.Now().UTC(), 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 botBackupExport(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } p := JobPayloadBackupExport{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) _, err := createJob(objSubTypeJobBackupExport, objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b) logOnError(err, "botBackupExport : createJob(objSubTypeJobBackupExport)") return } func botBackupImport(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } r := regexp.MustCompile(`^((http[s]?\:)\/\/)?([^\?\:\/#]+)(\:([0-9]+))?(\/[^\?\#]*)?(\?([^#]*))?(#.*)?.zip$`) if !r.MatchString(m.Payload) { c := TGCommand{ Type: commandReplyMsg, Text: "URL not valid.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } p := JobPayloadBackupImport{ URL: m.Payload, MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) _, err := createJob(objSubTypeJobBackupImport, objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b) logOnError(err, "botBackupImport : createJob(objSubTypeJobBackupImport)") return } func botMsgDump(m *tb.Message) { var res string if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } 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, _ := getObjMsg(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 } func botListParsingRules(m *tb.Message) { var s string = "" if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } for _, v := range msgParsingRules { s = fmt.Sprintf("%s[%d] %s\n", s, v.ID, v.Description) } c := TGCommand{ Type: commandReplyMsg, Text: s, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } func botListParsingRule(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { i, _ := strconv.ParseInt(m.Payload, 10, 64) for _, v := range msgParsingRules { if int64(v.ID) == i { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s\n", v.Rule), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } } c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Could not find rule %s\n", m.Payload), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("/parse_rule \n"), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botGStock(m *tb.Message) { if !m.Private() { return } p := JobPayloadGStock{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) _, err := createJob(objSubTypeJobGStock, objJobPriority, int64(m.Chat.ID), 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Stock requested", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botGDepositAll(m *tb.Message) { if !m.Private() { return } p := JobPayloadGDeposit{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, ResObjID64: nil, Status: 0, } p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`04`, `Bone`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`09`, `Cloth`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`21`, `Bone powder`)) b, _ := json.Marshal(p) t := time.Now().UTC() _, err := createJob(objSubTypeJobGDeposit, objJobPriority, int64(m.Chat.ID), 0, t, b) log.Printf("botGDepositAll : json : %s\n", string(b)) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botTimer(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() r := regexp.MustCompile("^(?P([0-9]*(s|m|h))+) \"(?P(.*))\"$") if r.MatchString(m.Payload) { d, err := time.ParseDuration(r.ReplaceAllString(m.Payload, "${Duration}")) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { p := JobPayloadMsgClient{ Text: r.ReplaceAllString(m.Payload, "${Msg}"), MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(d) objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Chat.ID), 0, t, b) logOnError(err, "botTimer : createJob") if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Job #%d scheduled at %s", objID64, t.Format(time.RFC850)), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } } return } func botGetItemId(m *tb.Message) { if len(m.Payload) > 0 { objItemID64 := getObjItemID(``, m.Payload) if objItemID64 != 0 { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Identified item #%d", objItemID64), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Can''t identify item", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } else { c := TGCommand{ Type: commandReplyMsg, Text: "No item name to identify", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return }