chirpnest/bot.go

789 lines
19 KiB
Go
Raw Normal View History

2019-05-03 05:58:36 +02:00
package main
import (
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)
2019-08-23 08:03:22 +02:00
b.Handle("/test_delay", botTestDelay)
2019-06-10 11:05:59 +02:00
b.Handle("/test_html", botTestHTML)
2019-05-17 10:30:01 +02:00
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-08-16 12:29:07 +02:00
b.Handle("/g_deposit_all", botGDepositAll)
2019-05-31 12:41:26 +02:00
2019-06-11 16:50:01 +02:00
b.Handle("/backup_export", botBackupExport)
b.Handle("/backup_import", botBackupImport)
2019-06-09 10:47:44 +02:00
b.Handle("/help", botHelp)
2019-06-08 17:28:07 +02:00
2019-06-28 13:04:08 +02:00
b.Handle("/get_item_id", botGetItemId)
2019-07-09 09:48:21 +02:00
b.Handle("/clients", botGetClients)
2019-06-28 13:04:08 +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,
2019-06-09 10:49:32 +02:00
Text: `/help - this help
/msg_rescan <id> - rescan one message
/msg_rescan_all - rescan all messages
/parse_rules - list parsing rules\n
/parse_rule <id> - detail for one rule
/timer <ETA> "msg" - schedule msg for client in ETA
/g_stock - check guild's vault
2019-06-11 17:59:09 +02:00
/backup_export - export message database
2019-07-09 09:48:21 +02:00
/backup_import <URL> - import message database from URL
/get_item_id <string> - identify item_id from string
/clients - list all connected clients`,
2019-06-09 10:47:44 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-06-10 11:05:59 +02:00
func botTestHTML(m *tb.Message) {
if !m.Private() {
return
}
c := TGCommand{
2019-06-10 11:31:53 +02:00
Type: commandReplyMsg,
2019-06-10 11:34:46 +02:00
Text: `<b>bold</b>,
<strong>bold</strong>,
<i>italic</i>,
<em>italic</em>,
<a href="https://t.me/share/url?url=/tu_def jgm2v8">inline URL</a>,
<code>inline fixed-width code</code>,
2019-06-10 11:31:53 +02:00
<pre>pre-formatted fixed-width code block</pre>`,
2019-06-10 11:05:59 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
ParseMode: cmdParseModeHTML,
}
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
}
2019-07-31 08:24:52 +02:00
if clt, ok := getLockedClient(m.Chat.ID, false); ok {
clt.Mux.Unlock()
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,
2019-08-23 08:03:22 +02:00
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()
2019-08-24 08:08:31 +02:00
clientSendCWMsgDelay(m.Chat.ID, "🏅Me", 5*time.Second)
2019-08-23 08:03:22 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: "Test sent",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
} else {
c := TGCommand{
Type: commandReplyMsg,
2019-05-17 10:30:01 +02:00
Text: "Client not registered",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
return
}
2019-07-09 09:48:21 +02:00
func botGetClients(m *tb.Message) {
if !m.Private() {
return
}
2019-07-31 07:07:12 +02:00
if clt, ok := getLockedClient(m.Chat.ID, false); ok {
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
muxClients.RLock()
2019-07-09 09:49:02 +02:00
var ret string
2019-07-31 07:07:12 +02:00
for id, c := range clients {
if c.Active {
2019-07-31 09:53:04 +02:00
ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (online)\n", ret, c.Login, c.CWUserID64, id)
2019-07-09 10:06:02 +02:00
} else {
2019-07-31 09:53:04 +02:00
ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (offline)\n", ret, c.Login, c.CWUserID64, id)
2019-07-09 10:01:02 +02:00
}
2019-07-31 07:07:12 +02:00
}
muxClients.RUnlock()
2019-07-09 09:48:21 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: ret,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
2019-07-09 10:01:02 +02:00
ParseMode: cmdParseModeHTML,
2019-07-09 09:48:21 +02:00
}
TGCmdQueue <- c
} else {
c := TGCommand{
Type: commandReplyMsg,
Text: "Client not registered",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
return
}
2019-05-17 10:30:01 +02:00
func botMsgRescan(m *tb.Message) {
if !m.Private() {
return
2019-05-10 04:26:37 +02:00
}
2019-07-31 07:07:12 +02:00
clt, ok := getLockedClient(m.Chat.ID, false)
if !ok {
2019-06-11 17:39:04 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: "Client not registered",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
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-08-21 05:46:42 +02:00
_, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, int64(m.Sender.ID), 0, time.Now().UTC(), 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
}
2019-07-31 07:07:12 +02:00
clt, ok := getLockedClient(m.Chat.ID, false)
if !ok {
2019-06-11 17:39:04 +02:00
c := TGCommand{
Type: commandReplyMsg,
Text: "Client not registered",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
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-08-21 05:46:42 +02:00
_, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, int64(m.Sender.ID), 0, time.Now().UTC(), 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-11 17:39:04 +02:00
func botBackupExport(m *tb.Message) {
2019-06-08 17:28:07 +02:00
if !m.Private() {
return
}
2019-07-31 07:07:12 +02:00
clt, ok := getLockedClient(m.Chat.ID, false)
if !ok {
2019-06-08 17:28:07 +02:00
c := TGCommand{
Type: commandReplyMsg,
2019-06-11 17:39:04 +02:00
Text: "Client not registered",
2019-06-08 17:28:07 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-06-08 17:28:07 +02:00
2019-06-11 17:39:04 +02:00
p := JobPayloadBackupExport{
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
2019-06-08 17:28:07 +02:00
}
2019-06-11 17:39:04 +02:00
b, _ := json.Marshal(p)
2019-08-21 05:46:42 +02:00
_, err := createJob(objSubTypeJobBackupExport, objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b)
2019-06-11 17:39:04 +02:00
logOnError(err, "botBackupExport : createJob(objSubTypeJobBackupExport)")
2019-06-08 17:28:07 +02:00
return
}
2019-06-11 17:39:04 +02:00
func botBackupImport(m *tb.Message) {
2019-06-09 10:47:44 +02:00
if !m.Private() {
return
}
2019-07-31 07:07:12 +02:00
clt, ok := getLockedClient(m.Chat.ID, false)
if !ok {
2019-06-10 11:05:59 +02:00
c := TGCommand{
Type: commandReplyMsg,
2019-06-11 17:39:04 +02:00
Text: "Client not registered",
2019-06-10 11:05:59 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-06-11 17:39:04 +02:00
return
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-06-11 17:39:04 +02:00
r := regexp.MustCompile(`^((http[s]?\:)\/\/)?([^\?\:\/#]+)(\:([0-9]+))?(\/[^\?\#]*)?(\?([^#]*))?(#.*)?.zip$`)
if !r.MatchString(m.Payload) {
2019-06-09 15:02:56 +02:00
c := TGCommand{
2019-06-09 15:00:15 +02:00
Type: commandReplyMsg,
2019-06-11 17:39:04 +02:00
Text: "URL not valid.",
2019-06-09 15:00:15 +02:00
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-06-11 17:39:04 +02:00
return
2019-06-09 15:00:15 +02:00
}
2019-06-09 14:17:05 +02:00
2019-06-11 17:55:31 +02:00
p := JobPayloadBackupImport{
2019-06-11 17:39:04 +02:00
URL: m.Payload,
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
}
b, _ := json.Marshal(p)
2019-08-21 05:46:42 +02:00
_, err := createJob(objSubTypeJobBackupImport, objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b)
2019-06-11 17:39:04 +02:00
logOnError(err, "botBackupImport : createJob(objSubTypeJobBackupImport)")
2019-06-09 10:47:44 +02:00
return
2019-06-11 17:39:04 +02:00
2019-06-09 10:47:44 +02:00
}
2019-05-18 09:44:25 +02:00
func botMsgDump(m *tb.Message) {
var res string
2019-07-31 07:07:12 +02:00
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
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-05-18 09:44:25 +02:00
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-06-09 14:17:05 +02:00
cwm, _ := getObjMsg(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-07-31 07:07:12 +02:00
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
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
2019-05-25 16:36:14 +02:00
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-07-31 07:07:12 +02:00
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
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
if clt.TGUserID64 != cfg.Bot.Admin {
c := TGCommand{
Type: commandReplyMsg,
Text: "Admin only",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
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
}
2019-08-08 14:39:23 +02:00
p := JobPayloadGStock{
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
}
b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second)
2019-08-21 05:46:42 +02:00
_, err := createJob(objSubTypeJobGStock, objJobPriority, int64(m.Chat.ID), 0, t, b)
2019-05-31 12:45:24 +02:00
2019-08-12 09:36:28 +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,
Text: "Stock requested",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
2019-05-31 12:45:24 +02:00
}
2019-08-12 09:36:28 +02:00
2019-05-31 12:41:26 +02:00
return
}
2019-08-16 12:29:07 +02:00
func botGDepositAll(m *tb.Message) {
if !m.Private() {
return
}
p := JobPayloadGDeposit{
2019-08-19 12:41:43 +02:00
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
ResObjID64: nil,
2019-08-21 05:46:42 +02:00
Status: 0,
2019-08-16 12:29:07 +02:00
}
2019-08-19 12:41:43 +02:00
2019-08-29 14:15:26 +02:00
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`01`, `Thread`))
2019-08-30 04:50:06 +02:00
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`03`, `Pelt`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`04`, `Bone`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`07`, `Powder`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`08`, `Iron Ore`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`09`, `Cloth`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`10`, `Silver Ore`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`11`, `Bauxite`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`13`, `Magic Stone`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`14`, `Wooden Shaft`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`15`, `Sapphire`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`17`, `Ruby`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`18`, `Hardener`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`19`, `Steel`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`21`, `Bone Powder`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`22`, `String`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`23`, `Coke`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`24`, `Purified Powder`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`31`, `Rope`))
2019-08-30 05:07:37 +02:00
2019-08-30 05:10:04 +02:00
list := getSQLListID64(fmt.Sprintf("select o.id from obj o where o.obj_sub_type_id = %d;", objSubTypeItemRecipe))
2019-08-30 05:07:37 +02:00
p.ResObjID64 = append(p.ResObjID64, list...)
2019-08-30 05:10:04 +02:00
list = getSQLListID64(fmt.Sprintf("select o.id from obj o where o.obj_sub_type_id = %d;", objSubTypeItemPart))
2019-08-30 05:07:37 +02:00
p.ResObjID64 = append(p.ResObjID64, list...)
2019-08-29 14:15:26 +02:00
/*
2019-08-30 04:50:06 +02:00
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`39`, `Stinky Sumac`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`s01`, `📕Scroll of Rage`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`r01`, `Champion Sword recipe`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`k01`, `Champion blade`))
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`w07`, `Rapier`))
2019-08-29 14:15:26 +02:00
*/
2019-08-19 12:41:43 +02:00
2019-08-16 12:29:07 +02:00
b, _ := json.Marshal(p)
t := time.Now().UTC()
2019-08-21 05:46:42 +02:00
_, err := createJob(objSubTypeJobGDeposit, objJobPriority, int64(m.Chat.ID), 0, t, b)
2019-08-16 12:29:07 +02:00
2019-08-21 06:08:11 +02:00
log.Printf("botGDepositAll : json : %s\n", string(b))
2019-08-16 12:29:07 +02:00
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: fmt.Sprintf("%s", err),
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
return
}
2019-05-26 13:30:21 +02:00
func botTimer(m *tb.Message) {
2019-07-31 07:07:12 +02:00
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
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 07:07:12 +02:00
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-08-08 14:39:23 +02:00
t := time.Now().UTC().Add(d)
2019-08-21 05:46:42 +02:00
objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Chat.ID), 0, 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
}
2019-06-28 13:04:08 +02:00
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
}