chirpnest/bot.go

424 lines
9.6 KiB
Go
Raw Normal View History

2019-05-03 05:58:36 +02:00
package main
import (
2019-06-08 17:45:27 +02:00
"bytes"
2019-05-10 15:58:56 +02:00
"encoding/json"
2019-05-03 05:58:36 +02:00
"fmt"
2019-05-11 07:08:12 +02:00
"log"
2019-05-10 13:05:25 +02:00
"regexp"
2019-05-18 09:46:34 +02:00
"strconv"
2019-05-03 05:58:36 +02:00
"time"
2019-05-18 09:46:34 +02:00
tb "gopkg.in/tucnak/telebot.v2"
2019-05-03 05:58:36 +02:00
)
2019-05-11 05:37:33 +02:00
func BotHandlers(b *tb.Bot) {
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-17 10:30:01 +02:00
b.Handle("/test", botTest)
b.Handle("/msg_rescan", botMsgRescan)
2019-05-16 05:35:37 +02:00
b.Handle("/msg_rescan_all", botMsgRescanAll)
2019-05-18 09:44:25 +02:00
b.Handle("/msg_dump", botMsgDump)
2019-05-10 04:58:28 +02:00
2019-05-25 16:36:14 +02:00
b.Handle("/parse_rules", botListParsingRules)
b.Handle("/parse_rule", botListParsingRule)
2019-05-26 13:30:21 +02:00
b.Handle("/timer", botTimer)
2019-05-31 12:41:26 +02:00
b.Handle("/g_stock", botGStock)
2019-06-09 10:47:44 +02:00
b.Handle("/msg_export", botMsgExport)
b.Handle("/help", botHelp)
2019-06-08 17:28:07 +02:00
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)
2019-05-26 09:57:45 +02:00
b.Handle(tb.OnDocument, botDocument)
2019-05-03 05:58:36 +02:00
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-26 09:57:45 +02:00
func botDocument(m *tb.Message) {
2019-05-31 17:06:31 +02:00
fmt.Printf("botDocument : %s (%d bytes)\n", m.Document.FileName, m.Document.File.FileSize)
2019-05-26 09:57:45 +02:00
// documents 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-06-09 10:47:44 +02:00
func botHelp(m *tb.Message) {
if !m.Private() {
return
}
c := TGCommand{
Type: commandReplyMsg,
Text: `/help - this help\n` +
`/msg_rescan <id> - rescan one message\n` +
`/msg_rescan_all - rescan all messages\n` +
`/parse_rules - list parsing rules\n` +
`/parse_rule <id> - detail for one rule\n` +
`/timer <ETA> "msg" - schedule msg for client in ETA\n` +
`/g_stock - check guild's vault\n` +
`/msg_export - export message database\n` +
`/msg_load - import message database`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-05-17 10:30:01 +02:00
func botTest(m *tb.Message) {
2019-05-10 04:26:37 +02:00
if !m.Private() {
2019-05-17 10:30:01 +02:00
return
}
if _, ok := clientsKeepAlive[m.Chat.ID]; ok {
2019-05-30 06:12:01 +02:00
clientSendCWMsg(m.Chat.ID, "🏅Me")
2019-05-17 10:30:01 +02:00
2019-05-30 06:13:50 +02:00
c := TGCommand{
2019-05-17 10:30:01 +02:00
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
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-11 07:14:59 +02:00
p := JobPayloadRescanMsg{
2019-05-16 04:49:34 +02:00
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,
2019-05-11 07:14:59 +02:00
}
2019-05-10 15:57:01 +02:00
b, _ := json.Marshal(p)
2019-05-11 07:14:59 +02:00
log.Printf("botMsgRescan : json : %s\n", string(b))
2019-05-26 15:46:54 +02:00
_, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, int64(m.Sender.ID), time.Now(), b)
2019-05-10 15:57:01 +02:00
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
if err != nil {
2019-05-17 10:30:01 +02:00
c := TGCommand{
Type: commandReplyMsg,
2019-05-18 13:15:50 +02:00
Text: fmt.Sprintf("Error scheduling the rescan for msg #%s", m.Payload),
2019-05-17 10:30:01 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-05-10 15:57:01 +02:00
} else {
2019-05-17 10:30:01 +02:00
c := TGCommand{
2019-05-17 10:31:13 +02:00
Type: commandReplyMsg,
2019-05-18 13:15:50 +02:00
Text: fmt.Sprintf("Rescaning msg #%s", m.Payload),
2019-05-17 10:30:01 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-05-10 15:57:01 +02:00
}
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-17 10:30:01 +02:00
botMsgRescanAll(m)
2019-05-10 13:04:21 +02:00
}
2019-05-17 10:30:01 +02:00
return
2019-05-10 04:26:37 +02:00
}
2019-05-11 12:45:05 +02:00
2019-05-16 05:35:37 +02:00
func botMsgRescanAll(m *tb.Message) {
2019-05-11 12:45:05 +02:00
if !m.Private() {
2019-05-16 05:35:37 +02:00
return
2019-05-11 12:45:05 +02:00
}
p := JobPayloadRescanMsg{
2019-05-19 05:22:30 +02:00
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),
2019-05-15 12:31:03 +02:00
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
2019-05-11 12:45:05 +02:00
}
b, _ := json.Marshal(p)
2019-05-26 15:46:54 +02:00
_, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, int64(m.Sender.ID), time.Now(), b)
2019-05-11 12:45:05 +02:00
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
2019-05-16 05:35:37 +02:00
2019-05-11 12:45:05 +02:00
if err != nil {
2019-05-16 05:35:37 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: "Error scheduling the rescan for all msg.",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-05-11 12:45:05 +02:00
} else {
2019-05-16 05:35:37 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: "Rescaning all msg scheduled.",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-05-11 12:45:05 +02:00
}
2019-05-16 05:35:37 +02:00
return
2019-05-11 12:45:05 +02:00
}
2019-05-18 09:44:25 +02:00
2019-06-09 10:47:44 +02:00
func botMsgExport(m *tb.Message) {
2019-06-08 17:28:07 +02:00
if !m.Private() {
return
}
2019-06-08 18:06:50 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: `Starting messages export`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-06-08 17:41:50 +02:00
b, err := zipMessages()
2019-06-08 18:26:13 +02:00
log.Printf("botMsgExportAll : Data returned.\n")
2019-06-08 17:28:07 +02:00
logOnError(err, "botMsgExportAll : exportMessages")
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: `Error exporting messages`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
2019-06-08 18:17:02 +02:00
} else {
text := fmt.Sprintf("Document size : %d bytes.", len(b))
c := TGCommand{
Type: commandReplyMsg,
Text: text,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-06-08 17:28:07 +02:00
}
2019-06-08 18:55:34 +02:00
d := tb.Document{}
d.File = tb.FromReader(bytes.NewReader(b))
2019-06-09 10:26:05 +02:00
d.FileName = fmt.Sprintf("%s.backup.zip", time.Now().Format("20060102150405"))
2019-06-09 10:18:40 +02:00
d.Caption = d.FileName
2019-06-09 10:47:44 +02:00
d.MIME = `application/zip`
2019-06-08 17:41:50 +02:00
2019-06-08 18:08:54 +02:00
c = TGCommand{
2019-06-09 09:34:08 +02:00
Type: commandSendDocument,
Document: d,
ToChatID64: m.Chat.ID,
2019-06-08 17:28:07 +02:00
}
TGCmdQueue <- c
return
}
2019-06-09 10:47:44 +02:00
func botMsgLoad(m *tb.Message) {
if !m.Private() {
return
}
return
}
2019-05-18 09:44:25 +02:00
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)
2019-05-18 10:12:29 +02:00
objTypeId, err := getObjTypeId(objId)
logOnError(err, "botMsgDump : getObjSubTypeId")
2019-05-18 09:44:25 +02:00
if err != nil {
2019-05-18 10:12:29 +02:00
res = `Error retrieving the message`
} else if objTypeId != objTypeMessage {
2019-05-18 09:44:25 +02:00
res = `This is not a message reference`
} else {
2019-05-18 10:12:58 +02:00
cwm, _ := getMsg(objId)
2019-05-18 09:44:25 +02:00
b, _ := json.Marshal(cwm)
res = string(b)
}
} else {
res = `/msg_dump <msg_id>`
}
c := TGCommand{
Type: commandReplyMsg,
Text: res,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-05-25 16:36:14 +02:00
func botListParsingRules(m *tb.Message) {
2019-05-25 16:41:07 +02:00
var s string = ""
2019-05-25 16:36:14 +02:00
if !m.Private() {
return
}
2019-05-25 16:36:48 +02:00
for _, v := range msgParsingRules {
2019-05-25 16:41:07 +02:00
s = fmt.Sprintf("%s[%d] %s\n", s, v.ID, v.Description)
2019-05-25 16:36:14 +02:00
}
c := TGCommand{
Type: commandReplyMsg,
Text: s,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
func botListParsingRule(m *tb.Message) {
2019-05-31 12:41:26 +02:00
if !m.Private() {
return
}
2019-05-25 16:36:14 +02:00
r := regexp.MustCompile("^[0-9]+$")
if r.MatchString(m.Payload) {
i, _ := strconv.ParseInt(m.Payload, 10, 64)
2019-05-25 16:36:48 +02:00
for _, v := range msgParsingRules {
2019-05-25 16:37:54 +02:00
if int64(v.ID) == i {
2019-05-25 16:36:14 +02:00
c := TGCommand{
Type: commandReplyMsg,
2019-05-25 16:38:46 +02:00
Text: fmt.Sprintf("%s\n", v.Rule),
2019-05-25 16:36:14 +02:00
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 <id>\n"),
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
return
}
2019-05-26 13:30:21 +02:00
2019-05-31 12:41:26 +02:00
func botGStock(m *tb.Message) {
if !m.Private() {
return
}
clientSendCWMsg(m.Chat.ID, "/g_stock_res")
clientSendCWMsg(m.Chat.ID, "/g_stock_alch")
clientSendCWMsg(m.Chat.ID, "/g_stock_misc")
clientSendCWMsg(m.Chat.ID, "/g_stock_rec")
clientSendCWMsg(m.Chat.ID, "/g_stock_parts")
clientSendCWMsg(m.Chat.ID, "/g_stock_other")
2019-05-31 12:45:24 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: "Stock requested",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-05-31 12:41:26 +02:00
return
}
2019-05-26 13:30:21 +02:00
func botTimer(m *tb.Message) {
2019-05-26 14:02:27 +02:00
r := regexp.MustCompile("^(?P<Duration>([0-9]*(s|m|h))+) \"(?P<Msg>(.*))\"$")
2019-05-26 13:30:21 +02:00
if r.MatchString(m.Payload) {
2019-05-26 14:40:37 +02:00
d, err := time.ParseDuration(r.ReplaceAllString(m.Payload, "${Duration}"))
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
2019-05-26 14:41:35 +02:00
Text: fmt.Sprintf("%s", err),
2019-05-26 14:40:37 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
} else {
2019-05-30 06:12:01 +02:00
p := JobPayloadMsgClient{
2019-06-03 03:01:18 +02:00
Text: r.ReplaceAllString(m.Payload, "${Msg}"),
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
2019-05-26 15:06:12 +02:00
}
2019-05-30 06:12:01 +02:00
b, _ := json.Marshal(p)
2019-05-26 15:46:54 +02:00
t := time.Now().Add(d)
2019-05-30 06:12:01 +02:00
objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Chat.ID), t, b)
2019-05-26 15:08:14 +02:00
logOnError(err, "botTimer : createJob")
2019-05-26 15:46:54 +02:00
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,
2019-05-26 15:47:35 +02:00
Text: fmt.Sprintf("Job #%d scheduled at %s", objID64, t.Format(time.RFC850)),
2019-05-26 15:46:54 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
2019-05-26 14:40:37 +02:00
}
2019-05-26 13:30:21 +02:00
}
2019-05-26 15:06:12 +02:00
2019-05-26 13:30:21 +02:00
return
}