2019-05-11 06:54:12 +02:00
package main
import (
2019-06-11 16:50:01 +02:00
"bytes"
2019-05-11 06:55:05 +02:00
"encoding/json"
2019-05-11 06:54:12 +02:00
"fmt"
2019-06-11 16:50:01 +02:00
"log"
"net/http"
"regexp"
2019-05-27 05:11:16 +02:00
"strconv"
2019-05-11 06:55:05 +02:00
"time"
2019-06-11 16:50:01 +02:00
tb "gopkg.in/tucnak/telebot.v2"
2019-05-11 06:54:12 +02:00
)
2019-06-11 16:50:01 +02:00
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 )
}
} )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Handle ( "/test" , botTest )
b . Handle ( "/test_html" , botTestHTML )
b . Handle ( "/msg_rescan" , botMsgRescan )
b . Handle ( "/msg_rescan_all" , botMsgRescanAll )
b . Handle ( "/msg_dump" , botMsgDump )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Handle ( "/parse_rules" , botListParsingRules )
b . Handle ( "/parse_rule" , botListParsingRule )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Handle ( "/timer" , botTimer )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Handle ( "/g_stock" , botGStock )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Handle ( "/backup_export" , botBackupExport )
b . Handle ( "/backup_import" , botBackupImport )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Handle ( "/help" , botHelp )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
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 )
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
b . Start ( )
}
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
func botPhoto ( m * tb . Message ) {
fmt . Println ( "botPhoto :" , m . Text )
// photos only
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
func botDocument ( m * tb . Message ) {
fmt . Printf ( "botDocument : %s (%d bytes)\n" , m . Document . FileName , m . Document . File . FileSize )
// documents only
}
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
func botHello ( m * tb . Message ) ( string , error ) {
fmt . Println ( "botHello :" , m . Text )
if ! m . Private ( ) {
fmt . Println ( "botHello : !m.Private()" )
return ` ` , nil
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText ( m )
return ` hello world ` , nil
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
func botChannelPost ( m * tb . Message ) {
fmt . Println ( "botChannelPost :" , m . Text )
PrintText ( m )
// channel posts only
}
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
func botQuery ( q * tb . Query ) {
fmt . Println ( "botQuery" )
// incoming inline queries
}
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
func botText ( m * tb . Message ) {
fmt . Println ( "botText :" , m . Text )
PrintText ( m )
// all the text messages that weren't
// captured by existing handlers
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
func botHelp ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
c := TGCommand {
Type : commandReplyMsg ,
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
/ backup_export - export backup database
/ backup_import < URL > - import backup database from URL ` ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
return
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
func botTestHTML ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
c := TGCommand {
Type : commandReplyMsg ,
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 > ,
< pre > pre - formatted fixed - width code block < / pre > ` ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
ParseMode : cmdParseModeHTML ,
}
TGCmdQueue <- c
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
return
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
func botTest ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
clientSendCWMsg ( m . Chat . ID , "🏅Me" )
2019-05-19 05:24:45 +02:00
2019-06-11 16:50:01 +02:00
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
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
func botMsgRescan ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
Type : commandReplyMsg ,
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-05-11 06:54:12 +02:00
}
2019-06-11 16:50:01 +02:00
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 ,
2019-05-11 06:54:12 +02:00
}
2019-05-19 05:20:01 +02:00
b , _ := json . Marshal ( p )
2019-06-11 16:50:01 +02:00
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 {
2019-05-16 05:06:38 +02:00
Type : commandReplyMsg ,
2019-06-11 16:50:01 +02:00
Text : fmt . Sprintf ( "Error scheduling the rescan for msg #%s" , m . Payload ) ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-05-16 05:06:38 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
} else {
c := TGCommand {
2019-05-16 05:06:38 +02:00
Type : commandReplyMsg ,
2019-06-11 16:50:01 +02:00
Text : fmt . Sprintf ( "Rescaning msg #%s" , m . Payload ) ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-05-16 05:06:38 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
2019-05-16 04:49:34 +02:00
}
2019-05-11 06:54:12 +02:00
}
2019-06-11 16:50:01 +02:00
r = regexp . MustCompile ( "^all$" )
if r . MatchString ( m . Payload ) {
botMsgRescanAll ( m )
}
2019-05-11 06:54:12 +02:00
return
}
2019-06-11 16:50:01 +02:00
func botMsgRescanAll ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
Type : commandReplyMsg ,
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
return
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
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)" )
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
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
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
func botBackupExport ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
2019-05-11 07:06:40 +02:00
2019-06-11 16:50:01 +02:00
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
Type : commandReplyMsg ,
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
return
2019-05-16 04:52:30 +02:00
}
2019-06-11 16:50:01 +02:00
p := JobPayloadBackupExport {
MsgID64 : int64 ( m . ID ) ,
ChatID64 : m . Chat . ID ,
}
b , _ := json . Marshal ( p )
t := time . Now ( )
objID64 , err := createJob ( objSubTypeJobBackupExport , objJobPriorityBackup , int64 ( m . Chat . ID ) , t , b )
logOnError ( err , "botBackupExport : createJob" )
2019-05-16 04:52:30 +02:00
2019-05-11 06:54:12 +02:00
return
}
2019-05-16 14:39:12 +02:00
2019-06-11 16:50:01 +02:00
func botBackupImport ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
Type : commandReplyMsg ,
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
2019-05-27 05:08:10 +02:00
return
}
2019-06-11 16:50:01 +02:00
r := regexp . MustCompile ( "^(http|https)://[a-z0-9./]+.zip$" ) // https://dump.siteop.biz/20190609163137.backup.zip
if ! r . MatchString ( m . Payload ) {
c := TGCommand {
Type : commandReplyMsg ,
Text : "Missing URL" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
return
}
2019-05-27 05:08:10 +02:00
2019-06-11 16:50:01 +02:00
p := JobPayloadBackupExport {
URL : m . Payload ,
MsgID64 : int64 ( m . ID ) ,
ChatID64 : m . Chat . ID ,
2019-05-27 05:08:10 +02:00
}
2019-06-11 16:50:01 +02:00
b , _ := json . Marshal ( p )
t := time . Now ( )
objID64 , err := createJob ( objSubTypeJobBackupImport , objJobPriorityBackup , int64 ( m . Chat . ID ) , t , b )
logOnError ( err , "botBackupImport : createJob" )
2019-05-27 05:08:10 +02:00
2019-05-16 14:39:12 +02:00
return
}
2019-05-26 15:06:12 +02:00
2019-06-11 16:50:01 +02:00
func botMsgDump ( m * tb . Message ) {
var res string
2019-06-10 06:03:47 +02:00
2019-06-11 16:50:01 +02:00
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
2019-06-11 09:44:28 +02:00
} else {
2019-06-11 16:50:01 +02:00
c := TGCommand {
Type : commandReplyMsg ,
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
2019-06-11 09:44:28 +02:00
return
}
2019-06-10 06:03:47 +02:00
2019-06-11 16:50:01 +02:00
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 <msg_id> `
}
2019-06-10 06:03:47 +02:00
2019-06-11 16:50:01 +02:00
c := TGCommand {
Type : commandReplyMsg ,
Text : res ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
2019-06-10 06:03:47 +02:00
return
}
2019-06-11 16:50:01 +02:00
func botListParsingRules ( m * tb . Message ) {
var s string = ""
if ! m . Private ( ) {
return
}
2019-05-26 15:06:12 +02:00
2019-06-11 16:50:01 +02:00
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
2019-06-03 03:01:18 +02:00
Type : commandReplyMsg ,
2019-06-11 16:50:01 +02:00
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-06-03 03:01:18 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
return
2019-05-30 06:12:01 +02:00
}
2019-05-26 15:06:12 +02:00
2019-06-11 16:50:01 +02:00
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
2019-05-26 15:06:12 +02:00
return
}
2019-06-11 03:59:20 +02:00
2019-06-11 16:50:01 +02:00
func botListParsingRule ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
2019-06-11 03:59:20 +02:00
2019-06-11 16:50:01 +02:00
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
2019-06-11 03:59:20 +02:00
Type : commandReplyMsg ,
2019-06-11 16:50:01 +02:00
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-06-11 03:59:20 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
return
2019-06-11 03:59:20 +02:00
}
2019-06-11 16:50:01 +02:00
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 <id>\n" ) ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
}
2019-06-11 03:59:20 +02:00
return
}
2019-06-11 16:50:01 +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" )
c := TGCommand {
Type : commandReplyMsg ,
Text : "Stock requested" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
}
TGCmdQueue <- c
return
}
2019-06-11 03:59:20 +02:00
2019-06-11 16:50:01 +02:00
func botTimer ( m * tb . Message ) {
if ! m . Private ( ) {
return
}
if _ , ok := clientsKeepAlive [ m . Chat . ID ] ; ok {
} else {
c := TGCommand {
2019-06-11 03:59:20 +02:00
Type : commandReplyMsg ,
2019-06-11 16:50:01 +02:00
Text : "Client not registered" ,
FromMsgID64 : int64 ( m . ID ) ,
FromChatID64 : m . Chat . ID ,
2019-06-11 03:59:20 +02:00
}
2019-06-11 16:50:01 +02:00
TGCmdQueue <- c
return
}
r := regexp . MustCompile ( "^(?P<Duration>([0-9]*(s|m|h))+) \"(?P<Msg>(.*))\"$" )
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 ( ) . Add ( d )
objID64 , err := createJob ( objSubTypeJobMsgClient , objJobPriority , int64 ( m . Chat . ID ) , 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
}
}
2019-06-11 03:59:20 +02:00
}
return
}