golem-telegram-bot/shared.php

167 lines
4.0 KiB
PHP

<?php
require_once('config.php');
function botlog($info) {
if ($file = fopen(LOGFILE, 'a'))
{
fwrite($file, $info);
fclose($file);
}
}
// Unrecoverable error, program must be terminated
function error($message) {
if (DEBUG) botlog("[EE] $message\n");
die();
}
// Uncommon event
function warning($message) {
if (DEBUG) botlog("[WW] $message\n");
}
// Routine debugging informations
function info($message) {
if (DEBUG) botlog("[II] $message\n");
}
// Interpreta un comando inviato al bot - mettere null o simili invece di -1
function getCommand($messaggio, $NoOffset = true) {
if (!isset($messaggio->entities))
return -1;
foreach ($messaggio->entities as $entity) {
if ($entity->type == "bot_command") {
if ($NoOffset == true and $entity->offset != 0)
return -1;
//~ 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);
if (DEBUG) botlog("[II] Comando: ".$ret['command']."\n");
return $ret;
}
}
return -1;
}
// fabbrica una tabellina che in futuro sarà la pagina di calendario specificata da $mese e $anno
function getCalendarTab ($month, $year) {
$container = array();
$riga = array();
$days = array("L", "M", "M", "G", "V", "S", "D");
for ($i = 0; $i < 7; $i++) {
$element = array(
"text" => "$days[$i]",
"callback_data" => "null"
);
$riga[] = $element;
}
$container[] = $riga;
$riga = array();
$daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year);
$date = $year."-".$month;
$currentMonth = date('nY') == $month.$year; //magari anche l'anno?
$startingDay = $currentMonth ? date('j') : 1;
if (DEBUG) botlog("[II] First day: ".$date.'-'.$startingDay."\n");
$firstDay = date("N", strtotime($date.'-'.$startingDay));
// blank buttons
for ($i = 1; $i < $firstDay; $i++) {
$element = array(
"text" => " ",
"callback_data" => "null"
);
$riga[] = $element;
}
for ($numDay = $startingDay; $numDay <= $daysInMonth; $numDay++, $i++) {
//if ($today <= $numDay) {
$element = array(
"text" => "$numDay",
"callback_data" => "$numDay"
);
//}
//else {
/* $element = array(
"text" => " ",
"callback_data" => "null"
);*/
//}
$riga[] = $element;
if ($i >= 7) {
$container[] = $riga;
$riga = array();
$i = 0;
}
}
// empty buttons
for (; $i > 1 and $i <= 7; $i++) {
$element = array(
"text" => " ",
"callback_data" => "null"
);
$riga[] = $element;
}
if (!empty($riga)) {
$container[] = $riga;
}
$container[] = array(
$currentMonth ?
array(
"text" => " ",
"callback_data" => "null"
)
:
array(
"text" => "<",
"callback_data" => "<"
),
array(
"text" => "$EMOJI_X Annulla",
"callback_data" => MSG_ABORT
),
array(
"text" => ">",
"callback_data" => ">"
));
return $container;
}
function not_authorized($chatID) {
$query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode($chatID) .
"&text=".urlencode("Utente non autorizzato\nhttps://www.youtube.com/watch?v=HO8ctP_QNZc");
file_get_contents($query);
die();
}
function wrong_action($chatID) {
$query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode($chatID) .
"&text=Azione non corretta";
file_get_contents($query);
die();
}
?>