parent
b4f31851c2
commit
5c05d01234
@ -1 +1 @@
|
||||
config.php
|
||||
|
||||
|
@ -1,133 +0,0 @@
|
||||
<?php
|
||||
|
||||
require_once('config.php');
|
||||
require_once('shared.php');
|
||||
|
||||
/* ===== BEGIN of MAIN PROGRAM ===== */
|
||||
if (DEBUG) botlog('[II] Start at ' . date('Y-m-d H:i:s') . ' ====================' . "\n");
|
||||
|
||||
$json = file_get_contents('php://input');
|
||||
$row = json_decode($json);
|
||||
|
||||
if (DEBUG) botlog("\n" . $json . "\n");
|
||||
|
||||
if (isset($row->message))
|
||||
{
|
||||
if (isset($row->message->from->username))
|
||||
{
|
||||
if (DEBUG) botlog('[II] ' . $row->message->from->username . ' has written «' . $row->message->text . '»' . "\n");
|
||||
|
||||
if (! isCommand($row->message))
|
||||
{
|
||||
if (in_array($row->message->from->username, BOT_ALLOWED_USERS))
|
||||
{
|
||||
if (DEBUG) botlog( ' Telegram: forwarding to channel... ');
|
||||
$r = forwardChannel($row->message);
|
||||
if (DEBUG) botlog(($r ? 'done' : 'error') . "\n");
|
||||
|
||||
if (DEBUG) botlog(' Mail: mailing to address... ');
|
||||
$r = sendEmail($row->message);
|
||||
if (DEBUG) botlog(($r ? 'done' : 'not done') . "\n");
|
||||
}
|
||||
else if (DEBUG) botlog('[EE] User ' . $row->message->from->username . ' not authorized!' . "\n");
|
||||
}
|
||||
else if (DEBUG) botlog('[WW] Command not allowed' . "\n");
|
||||
}
|
||||
else if (DEBUG) botlog('[EE] from->username not set!' . "\n");
|
||||
}
|
||||
|
||||
echo '{}' . "\n";
|
||||
botlog('[II] End at ' . date('Y-m-d H:i:s') . ' ====================' . "\n");
|
||||
/* ===== END of MAIN PROGRAM ===== */
|
||||
|
||||
/* ===== FUNCTIONS ===== */
|
||||
function forwardChannel($message)
|
||||
{
|
||||
$query = API_URL . API_TOKEN . '/forwardMessage?' .
|
||||
'chat_id=' . urlencode(API_CHANNEL_ID) .
|
||||
'&from_chat_id=' . urlencode($message->chat->id) .
|
||||
'&message_id=' . urlencode($message->message_id);
|
||||
if (DEBUG) botlog($query);
|
||||
return file_get_contents($query);
|
||||
}
|
||||
|
||||
function sendEmail($message)
|
||||
{
|
||||
$link = isLink($message);
|
||||
if ($link)
|
||||
{
|
||||
$title = getHtmlTitle($link);
|
||||
if ($title)
|
||||
{
|
||||
$subject = $title;
|
||||
$text = mb_convert_encoding($message->text, 'UTF-8');
|
||||
$headers = 'From: ' . MAIL_FROM_ADDR . "\n" . 'Content-Type: text/plain; charset=UTF-8';
|
||||
|
||||
if (mail(MAIL_TO_ADDR, $subject, $text, $headers))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieves link content, then, if it is HTML, returns the title (if any), else returns false
|
||||
* http://stackoverflow.com/questions/399332/fastest-way-to-retrieve-a-title-in-php
|
||||
*/
|
||||
function getHtmlTitle($link)
|
||||
{
|
||||
$html = file_get_contents($link);
|
||||
if ($html)
|
||||
{
|
||||
if (! preg_match('/<title>(.*)<\/title>/siU', $html, $titleMatches))
|
||||
{
|
||||
if (DEBUG) botlog('[II] Not HTML compliant link');
|
||||
return false;
|
||||
}
|
||||
|
||||
$title = trim(preg_replace('/\s+/', ' ', $titleMatches[1]));
|
||||
return $title;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks wheter message has a link (and returns it) or not (and returns false)
|
||||
* Only the first link is checked, bicos unn'aveo voglia di fanne altri - in inglisc un rend
|
||||
*/
|
||||
function isLink($message)
|
||||
{
|
||||
if (isset($message->entities))
|
||||
{
|
||||
foreach($message->entities as $entity)
|
||||
{
|
||||
if ($entity->type == 'url')
|
||||
{
|
||||
return substr(utf8_decode($message->text), $entity->offset, $entity->length);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks wheter message contains commands
|
||||
* Sì, il codice fa schifo e non è ottimizzato,
|
||||
* ma del resto è stato fatto in fretta e furia e, come detto,
|
||||
* è più per farsi un'idea e capire un po' come funziona 'sta cosa
|
||||
* Verrà abbellito durante qualche ora del GOLEM
|
||||
*/
|
||||
|
||||
function isCommand($message)
|
||||
{
|
||||
if (isset($message->entities))
|
||||
{
|
||||
foreach($message->entities as $entity)
|
||||
{
|
||||
if ($entity->type == 'bot_command') return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/* Telegram API URL - this must not be changed */
|
||||
const API_URL = 'https://api.telegram.org/bot';
|
||||
/* Your API authorization token */
|
||||
const API_TOKEN = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw';
|
||||
/* Your channel ID */
|
||||
const API_CHANNEL_ID = '@';
|
||||
/* Users whose messages are forwarded to mailing list and channel */
|
||||
const BOT_ALLOWED_USERS = array ();
|
||||
/* Address to forward to */
|
||||
const MAIL_TO_ADDR = 'example@example.com';
|
||||
/* Bot email identity */
|
||||
const MAIL_FROM_ADDR = 'example@example.com';
|
||||
/* Debug messages on log file */
|
||||
const DEBUG = true;
|
||||
/* Log file name */
|
||||
const LOGFILE = 'botlog.log';
|
||||
|
||||
$EMOJI_THUMBSUP = mb_convert_encoding('👍', 'UTF-8', 'HTML-ENTITIES');
|
||||
$EMOJI_THUMBSDOWN = mb_convert_encoding('👎', 'UTF-8', 'HTML-ENTITIES');
|
||||
$EMOJI_CLOCK = mb_convert_encoding('⏰', 'UTF-8', 'HTML-ENTITIES');
|
||||
$EMOJI_BOT = mb_convert_encoding('🤖', 'UTF-8', 'HTML-ENTITIES');
|
||||
$EMOJI_MSG = mb_convert_encoding('📧', 'UTF-8', 'HTML-ENTITIES');
|
||||
$EMOJI_X = mb_convert_encoding('❌', 'UTF-8', 'HTML-ENTITIES');
|
||||
$EMOJI_TOOLS = mb_convert_encoding('🔨', 'UTF-8', 'HTML-ENTITIES');
|
||||
|
||||
|
||||
// States Flags
|
||||
const STATE_IDLE = 'idle';
|
||||
const STATE_WAIT_DATE = 'wait_date';
|
||||
const STATE_WAIT_TIME = 'wait_time';
|
||||
const STATE_MSG_ANSWER = 'msg_answer';
|
||||
|
||||
// Saved variables in memcached
|
||||
const MC_STATUS = 'status';
|
||||
const MC_FORWARD_MSG = 'msg_forward';
|
||||
const MC_DATE_MSG = 'msg_date';
|
||||
const MC_INLINE_ID = 'inline_id';
|
||||
|
||||
// buttons costants
|
||||
const MSG_YES = 'y';
|
||||
const MSG_NO = 'n';
|
||||
const MSG_SCHEDULE = 'sch';
|
||||
const MSG_ABORT = 'abort';
|
||||
|
||||
const MYSQL_USER = '';
|
||||
const MYSQL_PASSWORD = '';
|
||||
const MYSQL_DB = '';
|
||||
const MYSQL_TABLE = '';
|
||||
|
||||
const MEMCACHED_PORT = '11211';
|
||||
|
||||
$WELCOME_MESSAGE = "$EMOJI_BOT GOLEMbot
|
||||
$EMOJI_MSG Se scrivi un messaggio ti chiedo se inoltrarlo al canale
|
||||
$EMOJI_TOOLS Se scrivi un comando eseguo operazioni.
|
||||
Comandi disponibili: /help, /list";
|
||||
?>
|
@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
require_once('config.php');
|
||||
|
||||
/* ===== BEGIN of MAIN PROGRAM ===== */
|
||||
if (! $latestGetTime = file_get_contents('latestFeedTimestamp.conf')) $latestGetTime = 0;
|
||||
|
||||
$content = file_get_contents('http://golem.linux.it/wp/feed/');
|
||||
$newLatestGetTime = time();
|
||||
|
||||
$content = str_replace('<![CDATA[', '', $content);
|
||||
$content = str_replace(']]>', '', $content);
|
||||
|
||||
$feed = new SimpleXMLElement($content); /* KABOOM */
|
||||
|
||||
foreach($feed->channel->item as $item)
|
||||
{
|
||||
$timestamp = strtotime($item->pubDate);
|
||||
if ($timestamp > $latestGetTime)
|
||||
{
|
||||
echo 'Now publishing: ' . $item->title . "\n";
|
||||
|
||||
/* Send to Telegram Channel */
|
||||
if (DEBUG) echo 'Publishing on Telegram channel...';
|
||||
$message = $item->title . "\n\n" . $item->description . "\n" . $item->link;
|
||||
$r = file_get_contents(API_URL . API_TOKEN . '/sendMessage?chat_id=' . urlencode(API_CHANNEL_ID) . '&text=' . urlencode($message));
|
||||
echo ($r ? 'done' : 'error') . "\n";
|
||||
|
||||
/* Send to Email/Mailing List */
|
||||
if (DEBUG) echo 'Sending to email address...';
|
||||
$title = mb_convert_encoding($item->title, 'ASCII');
|
||||
$text = mb_convert_encoding($item->description, 'UTF-8') . "\n" . $item->link;
|
||||
$r = mail(MAIL_TO_ADDR, $title, $text, 'From: ' . MAIL_FROM_ADDR . "\n" . 'Content-Type: text/plain; charset=UTF-8');
|
||||
echo ($r ? 'done' : 'error') . "\n";
|
||||
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents('latestFeedTimestamp.conf', $newLatestGetTime );
|
||||
/* ===== END of MAIN PROGRAM ===== */
|
||||
|
||||
?>
|
@ -0,0 +1,418 @@
|
||||
<?php
|
||||
/*
|
||||
* Codice beta v0.99 del GOLEMbot - 24 set 2017
|
||||
*
|
||||
* Opera by giomba e giuliof rilasciata sotto licenza GPLv3
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
// male, dovrebbero adeguarsi al sistema internazionale
|
||||
require_once('config.php');
|
||||
require_once('shared.php');
|
||||
|
||||
|
||||
// === BEGIN ===
|
||||
|
||||
if (DEBUG) botlog("\n\n\n".'[II] BOT request starting at ' . date('Y-m-d H:i:s') . ' ====================' . "\n\n");
|
||||
|
||||
$json = file_get_contents('php://input');
|
||||
$row = json_decode($json);
|
||||
|
||||
if (DEBUG) botlog("[LOG] ".$json."\n");
|
||||
|
||||
// Start up Memcached
|
||||
$mc = new Memcached();
|
||||
$mc->addServer('localhost', MEMCACHED_PORT)
|
||||
or error("Something is wrong connecting to memcached daemon");
|
||||
|
||||
// Accept only messages from a private chat
|
||||
if (isset($row->message->chat->type) and $row->message->chat->type != "private") {
|
||||
if (DEBUG) warning("Message from ".$row->message->chat->type);
|
||||
die();
|
||||
}
|
||||
// HANDLE A CALLBACK
|
||||
elseif (isset ($row->callback_query)) {
|
||||
$chatID = $row->callback_query->message->chat->id;
|
||||
|
||||
// Check if it was thrown by admitted users
|
||||
in_array($row->callback_query->message->chat->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
|
||||
|
||||
// Answer to the callback query (remove the loading sign)
|
||||
if (DEBUG) info("Callback Query, answering");
|
||||
$url = API_URL.API_TOKEN."/answerCallbackQuery?callback_query_id=".$row->callback_query->id;
|
||||
file_get_contents($url);
|
||||
|
||||
// Do something - insert here useful variables
|
||||
$callback_data = $row->callback_query->data;
|
||||
//$callback_id = $row->callback_query->id;
|
||||
|
||||
$inlineID = $row->callback_query->message->message_id;
|
||||
$inlineID_old = $mc->get($chatID.MC_INLINE_ID);
|
||||
|
||||
// check if request comes from active inline item
|
||||
if ($inlineID != $inlineID_old) { // expired session
|
||||
// If called callback comes from an old inline item do not answer
|
||||
if (DEBUG) warning("That's an old callback!");
|
||||
die();
|
||||
}
|
||||
}
|
||||
// HANDLE A COMMON MESSAGE (text/commands/keyboard)
|
||||
else {
|
||||
// Load variables
|
||||
$chatID = $row->message->chat->id;
|
||||
|
||||
|
||||
// Check if it was thrown by admitted users
|
||||
in_array($row->message->from->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Finally, check actual state and do things
|
||||
$status = $mc->get($chatID.MC_STATUS) or $mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE
|
||||
if (DEBUG) info("Starting from status $status");
|
||||
|
||||
// ricordati di schiantare quando ricevi un messaggio invece di una callback
|
||||
|
||||
switch ($status) {
|
||||
case STATE_IDLE:
|
||||
// In this state are not accepted callback requests
|
||||
if (isset($callback_data)) {
|
||||
if (DEBUG) warning("Callback request is not accepted in status $status");
|
||||
die();
|
||||
}
|
||||
|
||||
// Search for a command
|
||||
if (($command = getCommand($row->message)) != -1) {
|
||||
info("Command received: $command[command] with options $command[options]");
|
||||
|
||||
switch ($command['command']) {
|
||||
case '/help':
|
||||
$resp = array("remove_keyboard" => true);
|
||||
$resp = json_encode($resp);
|
||||
|
||||
$query = API_URL . API_TOKEN . '/sendmessage?' .
|
||||
'chat_id=' . urlencode($chatID) .
|
||||
'&text='.urlencode($WELCOME_MESSAGE).
|
||||
'&reply_markup='.$resp;
|
||||
file_get_contents($query);
|
||||
break;
|
||||
|
||||
case '/list':
|
||||
$dateID = date('Y-n-j G:*');
|
||||
|
||||
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
|
||||
$query = $sql->query("SELECT DateTime FROM ".MYSQL_TABLE." WHERE DateTime>='$dateID'
|
||||
ORDER BY DateTime ASC LIMIT 10") or error("Can't make the query, SQL error ".$sql->error);
|
||||
|
||||
$text = "Prossimi 10 messaggi programmati";
|
||||
$i = 1;
|
||||
while ($row = $query->fetch_assoc()) {
|
||||
$text .= "\n".$i." - ". $row['DateTime'];
|
||||
$i++;
|
||||
}
|
||||
$sql->close();
|
||||
|
||||
$query = API_URL.API_TOKEN."/sendmessage?chat_id=".($chatID).
|
||||
"&text=".urlencode($text).
|
||||
"&reply_markup=".$reply;
|
||||
$answer = file_get_contents($query);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (DEBUG) warning("$command[command] is not a valid command");
|
||||
}
|
||||
}
|
||||
else { // not a command (pure text)
|
||||
// you probably want to forward a message?
|
||||
$mc->set($chatID.MC_FORWARD_MSG, $row)
|
||||
or $mc->replace($chatID.MC_FORWARD_MSG, $row)
|
||||
or error("Something is wrong with memcached");
|
||||
|
||||
// SetUp inline messages (OK, NO, SCHEDULE)
|
||||
$container[] = array(
|
||||
array(
|
||||
"text" => "SI ".$EMOJI_THUMBSUP,
|
||||
"callback_data" => MSG_YES
|
||||
),
|
||||
array(
|
||||
"text" => "NO ".$EMOJI_THUMBSDOWN,
|
||||
"callback_data" => MSG_NO
|
||||
),
|
||||
array(
|
||||
"text" => "Programma ".$EMOJI_CLOCK,
|
||||
"callback_data" => MSG_SCHEDULE
|
||||
));
|
||||
$resp = array(
|
||||
"inline_keyboard" => $container);
|
||||
$reply = json_encode($resp);
|
||||
|
||||
$text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?";
|
||||
|
||||
$query = API_URL.API_TOKEN."/sendmessage?chat_id=".($row->message->chat->id).
|
||||
"&text=".urlencode($text).
|
||||
"&reply_markup=".$reply;
|
||||
$answer = file_get_contents($query);
|
||||
//if (DEBUG) botlog("\nINLINE ANSWER\n\n".$answer);
|
||||
$answer = json_decode($answer);
|
||||
|
||||
$inlineID = $answer->result->message_id;
|
||||
if (DEBUG) info("Callback message id: $inlineID");
|
||||
|
||||
$mc->replace($chatID.MC_STATUS, STATE_MSG_ANSWER);
|
||||
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case STATE_MSG_ANSWER: // only accessible by callback
|
||||
if (!(isset($callback_data))) {
|
||||
//$mc->replace($chatID.MC_STATUS, STATE_IDLE);
|
||||
wrong_action($chatID);
|
||||
die();
|
||||
}
|
||||
|
||||
switch ($callback_data) {
|
||||
case MSG_YES:
|
||||
// forward previously sent message
|
||||
$mc->replace($chatID.MC_STATUS, STATE_IDLE)
|
||||
or error("Something is wrong with memcached");
|
||||
$msg = $mc->get($chatID.MC_FORWARD_MSG)
|
||||
or error("Can't forward message\n");
|
||||
|
||||
$query = API_URL . API_TOKEN . '/forwardMessage?' .
|
||||
'chat_id=' . urlencode(API_CHANNEL_ID) .
|
||||
'&from_chat_id=' . urlencode($msg->message->chat->id) .
|
||||
'&message_id=' . urlencode($msg->message->message_id);
|
||||
file_get_contents($query);
|
||||
|
||||
// Remove kbd
|
||||
$text = "Invio effettuato correttamente";
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
break;
|
||||
|
||||
case MSG_NO:
|
||||
$status = $mc->replace($chatID.MC_STATUS, STATE_IDLE)
|
||||
or error("Something is wrong with memcached");
|
||||
|
||||
// destroy saved message?
|
||||
$text = "Invio annullato correttamente";
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
break;
|
||||
|
||||
case MSG_SCHEDULE:
|
||||
// == from here can be put in a function ==
|
||||
$status = $mc->replace($chatID.MC_STATUS, STATE_WAIT_DATE) or die();
|
||||
$currentTab = array(
|
||||
'month' => date('n'),
|
||||
'year' => date('Y'),
|
||||
'day' => null
|
||||
);
|
||||
|
||||
$container = getCalendarTab($currentTab['month'], $currentTab['year']);
|
||||
|
||||
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or die ();
|
||||
|
||||
$container = array(
|
||||
"inline_keyboard" => $container);
|
||||
|
||||
$text="Quando vuoi inviare il messaggio?";
|
||||
$reply = json_encode($container);
|
||||
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text).
|
||||
"&reply_markup=".$reply;
|
||||
|
||||
file_get_contents($url);
|
||||
|
||||
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID)
|
||||
or error("Something is wrong with memcached");
|
||||
// == end ==
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_WAIT_DATE:
|
||||
if (!isset($callback_data)) {
|
||||
wrong_action($chatID);
|
||||
die();
|
||||
}
|
||||
|
||||
$currentTab = $mc->get($chatID.MC_DATE_MSG)
|
||||
or error("Something is wrong with memcached");
|
||||
|
||||
// two cases: change page or confirm date
|
||||
if ($callback_data == '>') {
|
||||
// add 1 to month
|
||||
if ($currentTab['month'] < 12)
|
||||
$currentTab['month'] +=1;
|
||||
else {
|
||||
$currentTab['month'] = 1;
|
||||
$currentTab['year'] +=1;
|
||||
}
|
||||
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab)
|
||||
or error("Something is wrong with memcached");
|
||||
|
||||
$text = "Il calendario del ".$currentTab['month']."-".$currentTab['year'];
|
||||
|
||||
// get new inline container
|
||||
}
|
||||
elseif ($callback_data == '<') {
|
||||
if ($currentTab['month'] > 1)
|
||||
$currentTab['month'] -=1;
|
||||
else {
|
||||
$currentTab['month'] = 12;
|
||||
$currentTab['year'] -=1;
|
||||
}
|
||||
$text = "Il calendario del ".$currentTab['month']."-".$currentTab['year'];
|
||||
|
||||
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab)
|
||||
or error("Something is wrong with memcached");
|
||||
}
|
||||
elseif ($callback_data == 'null') {
|
||||
// not a valid button (like week days buttons)
|
||||
die();
|
||||
}
|
||||
elseif ($callback_data == MSG_ABORT) {
|
||||
$text = "Invio annullato correttamente";
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
$mc->replace($chatID.MC_STATUS, STATE_IDLE) or die();
|
||||
die();
|
||||
}
|
||||
else {
|
||||
// read current day, verify it and then change state
|
||||
if (!$callback_data = intval($callback_data))
|
||||
error("Not a numerical day, how you did it?!?");
|
||||
$currentTab['day'] = $callback_data;
|
||||
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or die ("Failed to save data at Memcached server");
|
||||
|
||||
// Display time inline keyboard
|
||||
$container = array();
|
||||
$riga = array();
|
||||
|
||||
$firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']?
|
||||
date('G')+1 : 8;
|
||||
|
||||
for ($i = 8; $i <= 21; $i++) {
|
||||
if ($i < $firstHour) {
|
||||
$riga[] = array(
|
||||
"text" => " ",
|
||||
"callback_data" => "null"
|
||||
);
|
||||
}
|
||||
else {
|
||||
$riga[] = array(
|
||||
"text" => "$i".":00",
|
||||
"callback_data" => "$i"
|
||||
);
|
||||
}
|
||||
|
||||
if ($i == 14) {
|
||||
$container[] = $riga;
|
||||
$riga = array();
|
||||
}
|
||||
}
|
||||
$container[] = $riga;
|
||||
|
||||
$container[] = array(array(
|
||||
"text" => "$EMOJI_X Annulla",
|
||||
"callback_data" => MSG_ABORT
|
||||
));
|
||||
|
||||
$container = array(
|
||||
"inline_keyboard" => $container);
|
||||
|
||||
$text="A che ora vuoi inviare il messaggio?";
|
||||
$reply = json_encode($container);
|
||||
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text)."&reply_markup=".$reply;
|
||||
file_get_contents($url);
|
||||
|
||||
$mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die();
|
||||
break;
|
||||
}
|
||||
|
||||
// If answer is < or > remain in current state and update calendar
|
||||
$container = getCalendarTab($currentTab['month'], $currentTab['year']);
|
||||
|
||||
$resp = array(
|
||||
"inline_keyboard" => $container);
|
||||
|
||||
$reply = json_encode($resp);
|
||||
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text)."&reply_markup=".$reply;
|
||||
file_get_contents($url);
|
||||
|
||||
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);
|
||||
|
||||
break;
|
||||
|
||||
case STATE_WAIT_TIME:
|
||||
$mc->replace($chatID.MC_STATUS, STATE_IDLE);
|
||||
if (!isset($callback_data)) {
|
||||
die();
|
||||
}
|
||||
|
||||
if ($callback_data == MSG_ABORT) {
|
||||
$text = "Invio annullato correttamente";
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
$mc->replace($chatID.MC_STATUS, STATE_IDLE) or die();
|
||||
die();
|
||||
}
|
||||
elseif (!$time = intval($callback_data)) {
|
||||
if (DEBUG) botlog("[EE] Not a numeric time\n");
|
||||
die();
|
||||
}
|
||||
|
||||
$currentTab = $mc->get($chatID.MC_DATE_MSG);
|
||||
$msg = $mc->get($chatID.MC_FORWARD_MSG);
|
||||
|
||||
// add to database (mysql)
|
||||
|
||||
// Build up mysql query
|
||||
$value = "(";
|
||||
$value .= $msg->message->message_id;
|
||||
$value .= ",";
|
||||
$value .= $msg->message->chat->id;
|
||||
$value .= ",";
|
||||
$value .= "'".$currentTab['year'].'-';
|
||||
$value .= $currentTab['month'].'-';
|
||||
$value .= $currentTab['day'].' ';
|
||||
$value .= $callback_data.":00:00'";
|
||||
$value .= ");";
|
||||
|
||||
info("Saving post in ".MYSQL_TABLE.", scheduled date: ".$value);
|
||||
|
||||
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
|
||||
$query = $sql->query("INSERT INTO ".MYSQL_TABLE." (MessageID,ChatID,DateTime) VALUE ".$value);
|
||||
$sql->close();
|
||||
|
||||
$text = "Programmazione avvenuta con successo";
|
||||
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
require_once('config.php');
|
||||
require_once('shared.php');
|
||||
|
||||
if (DEBUG) botlog("\n\n\n".'[II] Scheduler starting at ' . date('Y-m-d H:i:s') . ' ====================' . "\n\n");
|
||||
|
||||
$dateID = date('Y-n-j G:*');
|
||||
echo $dateID;
|
||||
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
|
||||
$query = $sql->query("SELECT MessageID,ChatID FROM ".MYSQL_TABLE." WHERE DateTime='".$dateID."'");
|
||||
while ($row = $query->fetch_assoc()) {
|
||||
info("Publishing message $row[MessageID]");
|
||||
$bot_query = API_URL . API_TOKEN . '/forwardMessage?' .
|
||||
'chat_id=' . urlencode(API_CHANNEL_ID) .
|
||||
'&from_chat_id=' . urlencode($row['ChatID']) .
|
||||
'&message_id=' . urlencode($row['MessageID']);
|
||||
file_get_contents($bot_query);
|
||||
}
|
||||
// clean events <= today
|
||||
|
||||
$sql->query("DELETE FROM ".MYSQL_TABLE." WHERE DateTime<='".$dateID."'");
|
||||
$sql->close();
|
||||
?>
|
@ -1,15 +1,166 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Logs text to file
|
||||
*/
|
||||
function botlog($text)
|
||||
{
|
||||
require_once('config.php');
|
||||
|
||||
function botlog($info) {
|
||||
if ($file = fopen(LOGFILE, 'a'))
|
||||
{
|
||||
fwrite($file, $text);
|
||||
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();
|
||||
}
|
||||
?>
|
||||
|
Loading…
Reference in new issue