2016-10-27 16:17:04 +00:00
< ? php
2020-12-05 21:51:01 +00:00
require_once ( '/data/config.php' );
2020-12-05 18:28:53 +00:00
// Base logging function. All logging functions use this.
// Change this to output everything on stdout, on file, on socket, on whathever you want...
2017-09-24 20:23:45 +00:00
function botlog ( $info ) {
2020-12-05 18:28:53 +00:00
error_log ( $info );
2016-10-27 16:17:04 +00:00
}
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
// Unrecoverable error, program must be terminated
function error ( $message ) {
2020-12-05 18:28:53 +00:00
if ( DEBUG ) botlog ( " [EE] $message " );
if ( DEBUG ) botlog ( '[II] BOT request died' );
2017-09-24 20:23:45 +00:00
die ();
}
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
// Uncommon event
function warning ( $message ) {
2020-12-05 18:28:53 +00:00
if ( DEBUG ) botlog ( " [WW] $message " );
2017-09-24 20:23:45 +00:00
}
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
// Routine debugging informations
function info ( $message ) {
2020-12-05 18:28:53 +00:00
if ( DEBUG ) botlog ( " [II] $message " );
2017-09-24 20:23:45 +00:00
}
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
// Interpreta un comando inviato al bot - mettere null o simili invece di -1
2017-09-24 20:23:45 +00:00
function getCommand ( $messaggio , $NoOffset = true ) {
if ( ! isset ( $messaggio -> entities ))
2017-09-28 19:41:43 +00:00
return null ;
2017-09-24 20:23:45 +00:00
foreach ( $messaggio -> entities as $entity ) {
if ( $entity -> type == " bot_command " ) {
if ( $NoOffset == true and $entity -> offset != 0 )
2017-09-28 19:41:43 +00:00
return null ;
2017-09-24 20:23:45 +00:00
//~ if (DEBUG) botlog("[II] Comando: ".$entity->offset. " ".$messaggio->text);
//return substr($messaggio->text, $entity->offset, $entity->length);
$ret [ 'command' ] = substr ( $messaggio -> text , $entity -> offset , $entity -> length );
$ret [ 'options' ] = substr ( $messaggio -> text , $entity -> length + $entity -> offset + 1 );
2017-09-28 19:41:43 +00:00
info ( " Command: $ret[command] " );
2017-09-24 20:23:45 +00:00
return $ret ;
}
}
2017-09-28 19:41:43 +00:00
return null ;
2017-09-24 20:23:45 +00:00
}
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
/**** Telegram query functions ****/
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
// return a message object, null if is error
2020-12-05 18:28:53 +00:00
function sendMessage ( $chat_id , $text , $parse_mode = null ,
2017-09-28 19:41:43 +00:00
$disable_web_preview = false ,
2020-12-05 18:28:53 +00:00
$disable_notification = false ,
$reply_to_message_id = null ,
2017-09-28 19:41:43 +00:00
$reply_markup = null ) {
$query = API_URL . API_TOKEN . " /sendMessage?chat_id= " . $chat_id .
" &text= " . urlencode ( $text );
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
if ( $parse_mode != null ) $query .= " &parse_mode= $parse_mode " ;
if ( $reply_markup != null ) $query .= " &reply_markup= $reply_markup " ;
if ( $disable_web_preview != false ) $query .= " &disable_web_preview= $disable_web_preview " ;
if ( $disable_notification != false ) $query .= " &disable_notification= $disable_notification " ;
if ( $reply_to_message_id != null ) $query .= " &reply_to_message_id= $reply_to_message_id " ;
if ( $reply_markup != null ) $query .= " &reply_markup= $reply_markup " ;
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
$answer = file_get_contents ( $query );
if ( $answer === false ) warning ( " Something is wrong in sendMessage - chat_id: $chat_id - text: $text " );
return $answer ;
}
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
//
function forwardMessage ( $chat_id , $from_chat_id ,
$message_id ,
2017-11-03 13:41:29 +00:00
$disable_notification = null ) {
2020-12-05 18:28:53 +00:00
2017-11-03 13:41:29 +00:00
$query = API_URL . API_TOKEN . " /forwardMessage? " .
2017-09-28 19:41:43 +00:00
" chat_id= " . urlencode ( $chat_id ) .
" &from_chat_id= " . urlencode ( $from_chat_id ) .
" &message_id= " . urlencode ( $message_id );
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
if ( $disable_notification != null ) $query .= " &disable_notification= $disable_notification " ;
$answer = file_get_contents ( $query );
2017-11-03 13:41:29 +00:00
if ( $answer === false ) {
warning ( " Something is wrong in forwardMessage - chat_id: $chat_id - message_id: $message_id " );
warning ( $answer );
}
2017-09-28 19:41:43 +00:00
return $answer ;
}
// omitted "inline parameters", for inline bot funtions (not used here)
function editMessageText ( $chat_id , $message_id , $text ,
$parse_mode = null ,
$disable_web_page_preview = false ,
$reply_markup = null ) {
2020-12-05 18:28:53 +00:00
2017-11-02 20:08:09 +00:00
$query = API_URL . API_TOKEN . " /editMessageText? " .
2017-09-28 19:41:43 +00:00
" chat_id= " . urlencode ( $chat_id ) .
" &message_id= " . urlencode ( $message_id ) .
" &text= " . urlencode ( $text );
2020-12-05 18:28:53 +00:00
2017-11-06 08:23:41 +00:00
if ( $parse_mode != null ) $query .= " &parse_mode= $parse_mode " ;
if ( $disable_web_page_preview != false ) $query .= " &disable_web_page_preview= $disable_web_page_preview " ;
2017-09-28 19:41:43 +00:00
if ( $reply_markup != null ) $query .= " &reply_markup= $reply_markup " ;
$answer = file_get_contents ( $query );
if ( $answer === false ) warning ( " Something is wrong in editMessageText - chat_id: $chat_id - message_id: $message_id " );
2020-12-05 18:28:53 +00:00
info ( " edit message answer: $answer " );
2017-09-28 19:41:43 +00:00
return $answer ;
}
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
//
function deleteMessage ( $chat_id , $message_id ) {
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
$query = API_URL . API_TOKEN . " /deleteMessage? " .
" chat_id= " . urlencode ( $chat_id ) .
" &message_id= " . urlencode ( $message_id );
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
$answer = file_get_contents ( $query );
if ( $answer === false ) warning ( " Something is wrong in deleteMessage - chat_id: $chat_id - message_id: $message_id " );
return $answer ;
}
2020-12-05 18:28:53 +00:00
2017-11-02 20:08:09 +00:00
function sendPhoto ( $chat_id , $photo ,
$caption = null ,
$reply_markup = null ,
2020-12-05 18:28:53 +00:00
$disable_notification = false ,
2017-11-02 20:08:09 +00:00
$reply_to_message_id = null ) {
2020-12-05 18:28:53 +00:00
2017-11-02 20:08:09 +00:00
$query = API_URL . API_TOKEN . " /sendPhoto? " .
" chat_id= " . urlencode ( $chat_id ) .
" &photo= " . urlencode ( $photo );
2020-12-05 18:28:53 +00:00
2017-11-02 20:08:09 +00:00
$query .= " &disable_notification= $disable_notification " ;
if ( $reply_to_message_id != null ) $query .= " &reply_to_message_id= $disable_web_page_preview " ;
if ( $reply_markup != null ) $query .= " &reply_markup= $reply_markup " ;
if ( $caption != null ) $query .= " &caption= $caption " ;
2020-12-05 18:28:53 +00:00
2017-11-02 20:08:09 +00:00
$answer = file_get_contents ( $query );
}
2020-12-05 18:28:53 +00:00
2017-09-28 19:41:43 +00:00
// Makes the calendar inline keyboard
2017-09-24 20:23:45 +00:00
function getCalendarTab ( $month , $year ) {
2020-12-05 18:28:53 +00:00
info ( 'getting calendar...' );
2017-12-16 22:58:36 +00:00
$kbd = new InlineKbd ;
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
$days = array ( " L " , " M " , " M " , " G " , " V " , " S " , " D " );
2020-12-05 18:28:53 +00:00
for ( $i = 0 ; $i < 7 ; $i ++ )
2017-12-16 22:58:36 +00:00
$kbd -> insertItem ( " $days[$i] " , " null " );
$kbd -> pushLine ();
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
$daysInMonth = cal_days_in_month ( CAL_GREGORIAN , $month , $year );
$date = $year . " - " . $month ;
2017-09-28 19:41:43 +00:00
$isCurrentMonth = date ( 'nY' ) == $month . $year ;
$startingDay = $isCurrentMonth ? date ( 'j' ) : 1 ;
2020-12-05 18:28:53 +00:00
info ( " [II] First day: " . $date . '-' . $startingDay . " \n " );
2017-09-24 20:23:45 +00:00
$firstDay = date ( " N " , strtotime ( $date . '-' . $startingDay ));
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
// blank buttons
2017-12-16 22:58:36 +00:00
for ( $i = 1 ; $i < $firstDay ; $i ++ )
$kbd -> insertItem ( " " , " null " );
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
for ( $numDay = $startingDay ; $numDay <= $daysInMonth ; $numDay ++ , $i ++ ) {
2017-12-16 22:58:36 +00:00
$kbd -> insertItem ( " $numDay " , " $numDay " );
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
if ( $i >= 7 ) {
2017-12-16 22:58:36 +00:00
$kbd -> pushLine ();
2017-09-24 20:23:45 +00:00
$i = 0 ;
}
}
2020-12-05 18:28:53 +00:00
// empty buttons
2018-02-20 19:07:21 +00:00
if ( $i != 0 ) {
2017-12-16 22:58:36 +00:00
for (; $i > 1 and $i <= 7 ; $i ++ )
$kbd -> insertItem ( " " , " null " );
$kbd -> pushLine ();
2017-09-24 20:23:45 +00:00
}
2020-12-05 18:28:53 +00:00
2017-12-16 22:58:36 +00:00
if ( $isCurrentMonth )
$kbd -> insertItem ( " " , " null " );
else
$kbd -> insertItem ( " < " , " < " );
$kbd -> insertItem ( " $EMOJI_X Annulla " , MSG_ABORT );
$kbd -> insertItem ( " > " , " > " );
2020-12-05 18:28:53 +00:00
2017-12-16 22:58:36 +00:00
$kbd -> pushLine ();
2020-12-05 18:28:53 +00:00
2017-12-16 22:58:36 +00:00
return $kbd -> getKeyboard ();
2017-09-24 20:23:45 +00:00
}
2020-12-05 18:28:53 +00:00
2017-12-26 17:12:06 +00:00
// Function used to get an archived message
// and make an appropriate keyboard
2020-12-05 18:28:53 +00:00
2017-12-26 17:12:06 +00:00
// -- I dont' like so much --
function navigateMsgArchive ( $msgNumber , $mc , $chatID ) {
2020-12-05 18:28:53 +00:00
$sql = new Sqlite3 ( DBFILE );
$numbers = $sql -> query ( " SELECT COUNT(*) FROM telegram_post " );
$numbers = $numbers -> fetchArray ()[ 0 ];
2017-12-26 17:12:06 +00:00
if ( $numbers == 0 ) {
return array ( " text " => " Nessun messaggio programmato " ,
" kbd " => null );
$sql -> close ();
//~ $query = API_URL.API_TOKEN."/sendmessage?chat_id=".($chatID).
//~ "&text=Nessun messaggio programmato";
//~ $answer = file_get_contents($query);
//~ die();
}
else {
2020-12-05 18:28:53 +00:00
$query = $sql -> query ( " SELECT DateTime as DateTimeFormat,ChatID, MessageID,Text,Author,ID FROM telegram_post ORDER BY DateTime ASC LIMIT 1 OFFSET $msgNumber " ) or error ( " Can't make the query, SQL error " . $sql -> error );
$row = $query -> fetchArray ();
2017-12-26 17:12:06 +00:00
$mc -> set ( $chatID . MC_DELETE_SCHEDULED_ID , $row [ 'ID' ])
or $mc -> replace ( $chatID . MC_DELETE_SCHEDULED_ID , $row [ 'ID' ]);
$sql -> close ();
2020-12-05 18:28:53 +00:00
2017-12-26 17:12:06 +00:00
$kbd = new InlineKbd ;
$kbd -> insertItem ( $row [ 'DateTimeFormat' ], " null " );
$kbd -> pushLine ();
if ( $msgNumber == 0 )
$kbd -> insertItem ( " " , " null " );
else
$kbd -> insertItem ( " < " , " < " );
$kbd -> insertItem ( " Rimuovi " , MSG_DELETE );
if ( $numbers - $msgNumber > 1 )
$kbd -> insertItem ( " > " , " > " );
else
$kbd -> insertItem ( " " , " null " );
$kbd -> pushLine ();
$kbd = $kbd -> getKeyboard ();
2020-12-05 18:28:53 +00:00
2017-12-26 17:12:06 +00:00
if ( $row [ 'Text' ] != '' ){
$text = " $row[Text] \n <i> $row[Author] </i> " ; // NOME CANALE ".API_CHANNEL_ID."?";
}
else
$text = " Non testo puro " ;
2020-12-05 18:28:53 +00:00
2017-12-26 17:12:06 +00:00
return array ( " text " => $text ,
" kbd " => $kbd );
}
}
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
function not_authorized ( $chatID ) {
$query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode ( $chatID ) .
2020-12-05 18:28:53 +00:00
" &text= " . urlencode ( " Questo bot è solo per il Consiglio Direttivo. \n Forse cercavi @golem_empoli (canale) oppure https://golem.linux.it/ (sito) " );
2017-09-24 20:23:45 +00:00
file_get_contents ( $query );
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
die ();
}
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
function wrong_action ( $chatID ) {
$query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode ( $chatID ) .
" &text=Azione non corretta " ;
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
file_get_contents ( $query );
2020-12-05 18:28:53 +00:00
2017-09-24 20:23:45 +00:00
die ();
}
2020-12-05 18:28:53 +00:00
2017-12-16 22:58:36 +00:00
class InlineKbd
{
private $keyboard = array ();
private $currentLine = array ();
2020-12-05 18:28:53 +00:00
2017-12-16 22:58:36 +00:00
public function pushLine () {
$this -> keyboard [] = $this -> currentLine ;
$this -> currentLine = array ();
}
public function insertItem ( $text , $callback_data ) {
$this -> currentLine [] = array (
" text " => $text ,
" callback_data " => $callback_data
);
}
public function getKeyboard () {
2020-12-05 18:28:53 +00:00
$kbd = array ( " inline_keyboard " => $this -> keyboard );
2017-12-16 22:58:36 +00:00
return json_encode ( $kbd );
}
}
2016-10-27 16:17:04 +00:00
?>