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"
tb "gopkg.in/tucnak/telebot.v2"
2019-05-11 07:08:12 +02:00
"log"
2019-05-10 13:05:25 +02:00
"regexp"
2019-05-03 05:58:36 +02:00
"time"
)
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-10 04:58:28 +02:00
b . Handle ( "/msg_rescan" , func ( m * tb . Message ) {
s , err := botMsgRescan ( m )
logOnError ( err , "/msg_rescan" )
if err == nil {
b . Send ( m . Sender , s )
}
} )
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 )
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-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-10 04:58:28 +02:00
func botMsgRescan ( m * tb . Message ) ( string , error ) {
2019-05-10 04:26:37 +02:00
fmt . Println ( "botRescanMsg :" , m . Text )
if ! m . Private ( ) {
fmt . Println ( "botRescanMsg : !m.Private()" )
2019-05-10 04:59:05 +02:00
return ` ` , nil
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-11 07:20:44 +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 ) ,
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-11 06:54:12 +02:00
err := createJob ( objSubTypeJobRescanMsg , objJobPriorityRescanMsg , time . Now ( ) , b )
2019-05-10 15:57:01 +02:00
logOnError ( err , "botMsgRescan : createJob(objSubTypeJobRescanMsg)" )
if err != nil {
return "Error scheduling the rescan for msg #" + m . Payload , nil
} else {
return "Rescaning msg #" + m . Payload , nil
}
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-11 07:14:59 +02:00
p := JobPayloadRescanMsg {
2019-05-11 07:20:44 +02:00
Query : fmt . Sprintf ( "SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d;" , objTypeMessage , objSubTypeMessageUnknown ) ,
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-11 06:54:12 +02:00
err := createJob ( objSubTypeJobRescanMsg , objJobPriorityRescanAllMsg , time . Now ( ) , b )
2019-05-11 05:15:42 +02:00
logOnError ( err , "botMsgRescan : createJob(objSubTypeJobRescanMsg)" )
2019-05-10 15:57:01 +02:00
if err != nil {
return "Error scheduling the rescan for all msg" , nil
} else {
return "Rescaning all msg scheduled" , nil
}
2019-05-10 13:04:21 +02:00
}
2019-05-10 15:57:01 +02:00
return "/msg_rescan msg_id or /msg_rescan all" , nil
2019-05-10 04:26:37 +02:00
}