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 )
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-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-26 13:30:21 +02:00
fmt . Printf ( "botDocument : %s (%d bytes)" , 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-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 {
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
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
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 ) {
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
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-26 15:06:12 +02:00
c := TGCommand {
2019-05-26 15:07:28 +02:00
Type : commandSendMsg ,
Text : r . ReplaceAllString ( m . Payload , "${Msg}" ) ,
2019-05-26 15:06:12 +02:00
FromUserID64 : m . Chat . ID ,
ToChatID64 : userID64ChtWrsBot ,
}
b , _ := json . Marshal ( c )
2019-05-26 15:46:54 +02:00
t := time . Now ( ) . Add ( d )
objID64 , err := createJob ( objSubTypeJobMsgClient , objJobPriority , int64 ( m . Sender . 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
}