porting for docker image

this code is hell
This commit is contained in:
giomba 2020-12-05 19:28:53 +01:00
parent cb0b224594
commit a710ce6718
7 changed files with 225 additions and 227 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
config.php

3
README
View File

@ -1,3 +0,0 @@
Further info (in Italian) at
http://golem.linux.it/wiki/GOLEM_Telegram_Bot

8
README.md Normal file
View File

@ -0,0 +1,8 @@
# golem-telegram-bot
http://golem.linux.it/wiki/GOLEM_Telegram_Bot
## sqlite3 database creation
```
CREATE TABLE telegram_post (ID INTEGER PRIMARY KEY NOT NULL, MessageID INTEGER NOT NULL, ChatID INTEGER NOT NULL, DateTime DATETIME NOT NULL, Text TEXT NOT NULL, Author TEXT);
```

View File

@ -1,15 +1,13 @@
<?php <?php
/* Telegram API URL - this must not be changed */ /* Telegram API URL - this must not be changed */
const API_URL = 'https://api.telegram.org/bot'; const API_URL = 'https://api.telegram.org/bot';
/* Your API authorization token */ /* Your API authorization token */
const API_TOKEN = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw'; const API_TOKEN = '';
/* Your channel ID */ /* Your channel ID */
const API_CHANNEL_ID = '@'; const API_CHANNEL_ID = '@';
/* Users whose messages are forwarded to mailing list and channel */ /* Users whose messages are forwarded to mailing list and channel */
const BOT_ALLOWED_USERS = array (); const BOT_ALLOWED_USERS = array ();
/* Users who can use the new bot interface */
const BOT_BETA_TESTERS = array ();
/* Address to forward to */ /* Address to forward to */
const MAIL_TO_ADDR = 'example@example.com'; const MAIL_TO_ADDR = 'example@example.com';
/* Bot email identity */ /* Bot email identity */
@ -20,14 +18,12 @@
const FORWARD_SILENT = true; const FORWARD_SILENT = true;
/* Log file name */ /* Log file name */
const LOGFILE = 'botlog.log'; const LOGFILE = 'botlog.log';
/* MySQL informations */ /* Database settings */
const MYSQL_USER = ''; const DBFILE = '/data/database.sqlite3';
const MYSQL_PASSWORD = ''; /* Memcached server connection */
const MYSQL_DB = ''; const MEMCACHED_PORT = '11211';
const MYSQL_TABLE = ''; const MEMCACHED_HOST = 'memcached';
/* Memcached local server port */
const MEMCACHED_PORT = '11211';
$EMOJI_THUMBSUP = mb_convert_encoding('&#x1F44D;', 'UTF-8', 'HTML-ENTITIES'); $EMOJI_THUMBSUP = mb_convert_encoding('&#x1F44D;', 'UTF-8', 'HTML-ENTITIES');
$EMOJI_THUMBSDOWN = mb_convert_encoding('&#x1F44E;', 'UTF-8', 'HTML-ENTITIES'); $EMOJI_THUMBSDOWN = mb_convert_encoding('&#x1F44E;', 'UTF-8', 'HTML-ENTITIES');
$EMOJI_CLOCK = mb_convert_encoding('&#x23F0;', 'UTF-8', 'HTML-ENTITIES'); $EMOJI_CLOCK = mb_convert_encoding('&#x23F0;', 'UTF-8', 'HTML-ENTITIES');
@ -35,14 +31,14 @@
$EMOJI_MSG = mb_convert_encoding('&#x1F4E7;', 'UTF-8', 'HTML-ENTITIES'); $EMOJI_MSG = mb_convert_encoding('&#x1F4E7;', 'UTF-8', 'HTML-ENTITIES');
$EMOJI_X = mb_convert_encoding('&#x274C;', 'UTF-8', 'HTML-ENTITIES'); $EMOJI_X = mb_convert_encoding('&#x274C;', 'UTF-8', 'HTML-ENTITIES');
$EMOJI_TOOLS = mb_convert_encoding('&#x1F528;', 'UTF-8', 'HTML-ENTITIES'); $EMOJI_TOOLS = mb_convert_encoding('&#x1F528;', 'UTF-8', 'HTML-ENTITIES');
// States Flags // States Flags
const STATE_IDLE = 'idle'; const STATE_IDLE = 'idle';
const STATE_WAIT_DATE = 'wait_date'; const STATE_WAIT_DATE = 'wait_date';
const STATE_WAIT_TIME = 'wait_time'; const STATE_WAIT_TIME = 'wait_time';
const STATE_MSG_ANSWER = 'msg_answer'; const STATE_MSG_ANSWER = 'msg_answer';
// Saved variables in memcached // Saved variables in memcached
const MC_STATUS = 'status'; const MC_STATUS = 'status';
const MC_FORWARD_MSG = 'msg_forward'; const MC_FORWARD_MSG = 'msg_forward';
@ -51,14 +47,14 @@
const MC_FW_EDIT = 'fw_edit'; // should not be necessary anymore const MC_FW_EDIT = 'fw_edit'; // should not be necessary anymore
const MC_LIST_NUMBER = 'list_number'; const MC_LIST_NUMBER = 'list_number';
const MC_DELETE_SCHEDULED_ID = 'delete_sch_id'; const MC_DELETE_SCHEDULED_ID = 'delete_sch_id';
// buttons costants // buttons costants
const MSG_YES = 'y'; const MSG_YES = 'y';
const MSG_NO = 'n'; const MSG_NO = 'n';
const MSG_SCHEDULE = 'sch'; const MSG_SCHEDULE = 'sch';
const MSG_ABORT = 'abort'; const MSG_ABORT = 'abort';
const MSG_DELETE = 'delete'; const MSG_DELETE = 'delete';
$WELCOME_MESSAGE = "$EMOJI_BOT GOLEMbot $WELCOME_MESSAGE = "$EMOJI_BOT GOLEMbot
$EMOJI_MSG Se scrivi un messaggio ti chiedo se inoltrarlo al canale $EMOJI_MSG Se scrivi un messaggio ti chiedo se inoltrarlo al canale
Supporto anche **<b>grassetto</b>**, __<i>corsivo</i>__ e `<code>codice</code>` Supporto anche **<b>grassetto</b>**, __<i>corsivo</i>__ e `<code>codice</code>`

245
main.php
View File

@ -1,30 +1,29 @@
<?php <?php
/* /*
* Codice beta v0.99.1 del GOLEMbot - 28 set 2017 * codice schifo del GOLEMbot - dal 28 set 2017
* *
* Opera by giomba, lucam e giuliof released by GPLv3 license * Opera by giomba, lucam e giuliof released under GPLv3 license
* *
*/ */
// male, dovrebbero adeguarsi al sistema internazionale
require_once('config.php'); require_once('config.php');
require_once('shared.php'); require_once('shared.php');
// === BEGIN === // === BEGIN ===
if (DEBUG) botlog("\n\n\n".'[II] BOT request starting at ' . date('Y-m-d H:i:s') . ' ====================' . "\n\n"); DEBUG and botlog('[II] BOT request starting at ' . date('Y-m-d H:i:s'));
$json = file_get_contents('php://input'); $json = file_get_contents('php://input');
$row = json_decode($json); $row = json_decode($json);
if (DEBUG) botlog("[LOG] ".$json."\n"); DEBUG and botlog("[II] Incoming JSON ".$json."\n");
// Start up Memcached // Start up Memcached
$mc = new Memcached(); $mc = new Memcached();
$mc->addServer('localhost', MEMCACHED_PORT) if ($mc == null) error('no memcached object instance');
or error("Something is wrong connecting to memcached daemon"); if (! $mc->addServer(MEMCACHED_HOST, MEMCACHED_PORT)) {
error("something is wrong connecting to memcached daemon");
}
// Structure of managed telegram messages // Structure of managed telegram messages
/* /*
@ -45,7 +44,7 @@ $mc->addServer('localhost', MEMCACHED_PORT)
* |_ text § string (message_text) * |_ text § string (message_text)
* |_ entities: [{"offset":...,"length":...,"type":"italic"}] * |_ entities: [{"offset":...,"length":...,"type":"italic"}]
* |_ photo, location, video * |_ photo, location, video
* *
* **Callback Query** * **Callback Query**
* $row * $row
* |_ callback_query * |_ callback_query
@ -54,34 +53,35 @@ $mc->addServer('localhost', MEMCACHED_PORT)
* |_ message § Message * |_ message § Message
* |_ inline_message_id § integer (inlineID) * |_ inline_message_id § integer (inlineID)
* |_ ... * |_ ...
*/ */
// HANDLE A CALLBACK // HANDLE A CALLBACK
if (isset ($row->callback_query)) { if (isset ($row->callback_query)) {
DEBUG and botlog('handle callback query');
$chatID = $row->callback_query->message->chat->id; $chatID = $row->callback_query->message->chat->id;
// Check if it was thrown by admitted users // Check if it was thrown by admitted users
in_array($row->callback_query->message->chat->username, BOT_BETA_TESTERS) or not_authorized($chatID); in_array($row->callback_query->message->chat->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
botlog('Ok authorized user pass!');
// Answer to the callback query (remove the loading sign) // Answer to the callback query (remove the loading sign)
if (DEBUG) info("Callback Query, answering"); info("Callback Query, answering");
$url = API_URL.API_TOKEN."/answerCallbackQuery?callback_query_id=".$row->callback_query->id; $url = API_URL.API_TOKEN."/answerCallbackQuery?callback_query_id=".$row->callback_query->id;
file_get_contents($url); file_get_contents($url);
// Do something - insert here useful variables // Do something - insert here useful variables
$callback_data = $row->callback_query->data; $callback_data = $row->callback_query->data;
//$callback_id = $row->callback_query->id;
$inlineID = $row->callback_query->message->message_id; $inlineID = $row->callback_query->message->message_id;
$inlineID_old = $mc->get($chatID.MC_INLINE_ID); $inlineID_old = $mc->get($chatID.MC_INLINE_ID);
// check if request comes from active inline item // check if request comes from active inline item
if ($inlineID != $inlineID_old) { // expired session if ($inlineID != $inlineID_old) { // expired session
// If called callback comes from an old inline item do not answer // If called callback comes from an old inline item do not answer
if (DEBUG) warning("That's an old callback!"); if (DEBUG) warning("That's an old callback!");
die(); die();
} }
// Finally, check actual state and do things // Finally, check actual state and do things
$status = $mc->get($chatID.MC_STATUS); $status = $mc->get($chatID.MC_STATUS);
if (!$status) { if (!$status) {
@ -90,7 +90,7 @@ if (isset ($row->callback_query)) {
} }
if (DEBUG) info("Starting from status $status"); if (DEBUG) info("Starting from status $status");
// Callback state machine // Callback state machine
switch ($status) { switch ($status) {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ -100,17 +100,17 @@ if (isset ($row->callback_query)) {
case STATE_IDLE: case STATE_IDLE:
switch ($callback_data){ switch ($callback_data){
case MSG_DELETE: case MSG_DELETE:
// remove current message // remove current message
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); $sql = new Sqlite3(DBFILE);
$IDdelete = $mc->get($chatID.MC_DELETE_SCHEDULED_ID) or error("No ID to delete"); $IDdelete = $mc->get($chatID.MC_DELETE_SCHEDULED_ID) or error("No ID to delete");
$sql->query("DELETE FROM ".MYSQL_TABLE." WHERE ID=$IDdelete") $sql->query("DELETE FROM telegram_post WHERE ID=$IDdelete")
or error("Can't make the query, SQL error ".$sql->error); or error("Can't make the query, SQL error ".$sql->lastErrorMsg());
$sql->close(); $sql->close();
$offset = $mc->get($chatID.MC_LIST_NUMBER); $offset = $mc->get($chatID.MC_LIST_NUMBER);
if ($offset > 0) { if ($offset > 0) {
$offset--; $offset--;
} }
@ -128,37 +128,37 @@ if (isset ($row->callback_query)) {
if (DEBUG) warning("Callback request is not accepted in status $status"); if (DEBUG) warning("Callback request is not accepted in status $status");
die(); die();
} }
$archivedMsg = navigateMsgArchive($offset, $mc, $chatID); $archivedMsg = navigateMsgArchive($offset, $mc, $chatID);
$answer = editMessageText($chatID, $inlineID, $archivedMsg['text'], $answer = editMessageText($chatID, $inlineID, $archivedMsg['text'],
"HTML", false, $archivedMsg['kbd'] ); "HTML", false, $archivedMsg['kbd'] );
$answer = json_decode($answer); $answer = json_decode($answer);
$mc->set($chatID.MC_LIST_NUMBER, $offset) $mc->set($chatID.MC_LIST_NUMBER, $offset)
or $mc->replace($chatID.MC_LIST_NUMBER, $offset); or $mc->replace($chatID.MC_LIST_NUMBER, $offset);
die(); die();
break; break;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END STATE_IDLE * END STATE_IDLE
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* entering STATE_MSG_ANSWER - callback * entering STATE_MSG_ANSWER - callback
* you can: (after you sent a message) choose if forward to * you can: (after you sent a message) choose if forward to
* channel or schedule it * channel or schedule it
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
case STATE_MSG_ANSWER: // only accessible by callback case STATE_MSG_ANSWER: // only accessible by callback
/////// spostare di là /////// spostare di là
if (!(isset($callback_data))) { if (!(isset($callback_data))) {
//$mc->replace($chatID.MC_STATUS, STATE_IDLE); //$mc->replace($chatID.MC_STATUS, STATE_IDLE);
wrong_action($chatID); wrong_action($chatID);
die(); die();
} }
switch ($callback_data) { switch ($callback_data) {
case MSG_YES: case MSG_YES:
// forward previously sent message // forward previously sent message
@ -167,9 +167,9 @@ if (isset ($row->callback_query)) {
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
$msg = $mc->get($chatID.MC_FORWARD_MSG) $msg = $mc->get($chatID.MC_FORWARD_MSG)
or error("Can't forward message\n"); or error("Can't forward message\n");
forwardMessage(API_CHANNEL_ID, $msg->message->chat->id, $msg->message->message_id, FORWARD_SILENT); forwardMessage(API_CHANNEL_ID, $msg->message->chat->id, $msg->message->message_id, FORWARD_SILENT);
// Remove kbd // Remove kbd
$text = "Invio effettuato correttamente"; $text = "Invio effettuato correttamente";
editMessageText($chatID, $inlineID, $text); editMessageText($chatID, $inlineID, $text);
@ -178,32 +178,34 @@ if (isset ($row->callback_query)) {
//~ "&text=".urlencode($text); //~ "&text=".urlencode($text);
//~ file_get_contents($url); //~ file_get_contents($url);
break; break;
case MSG_NO: case MSG_NO:
$status = $mc->replace($chatID.MC_STATUS, STATE_IDLE) $status = $mc->replace($chatID.MC_STATUS, STATE_IDLE)
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
// destroy saved message? // destroy saved message?
$text = "Invio annullato correttamente"; $text = "Invio annullato correttamente";
editMessageText($chatID, $inlineID, $text); editMessageText($chatID, $inlineID, $text);
break; break;
case MSG_SCHEDULE: case MSG_SCHEDULE:
// == from here can be put in a function == // == from here can be put in a function ==
$status = $mc->replace($chatID.MC_STATUS, STATE_WAIT_DATE) or die(); info('status MSG_SCHEDULE');
$currentTab = array( $status = $mc->replace($chatID.MC_STATUS, STATE_WAIT_DATE) or error(__FILE__ . __LINE__);
$currentTab = array(
'month' => date('n'), 'month' => date('n'),
'year' => date('Y'), 'year' => date('Y'),
'day' => null 'day' => null
); );
$reply = getCalendarTab($currentTab['month'], $currentTab['year']); $reply = getCalendarTab($currentTab['month'], $currentTab['year']);
info('calendar tab: ' . $reply);
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or die ();
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or error(__FILE__ . __LINE__);
$text="Quando vuoi inviare il messaggio?"; $text="Quando vuoi inviare il messaggio?";
editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); editMessageText($chatID, $inlineID, $text, "HTML", false, $reply);
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID) $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID)
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
// == end == // == end ==
@ -213,15 +215,15 @@ if (isset ($row->callback_query)) {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END STATE_MSG_ANSWER * END STATE_MSG_ANSWER
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* entering STATE_WAIT_DATE - callback * entering STATE_WAIT_DATE - callback
* you can: choose the date of scheduled message(s) * you can: choose the date of scheduled message(s)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
case STATE_WAIT_DATE: case STATE_WAIT_DATE:
$currentTab = $mc->get($chatID.MC_DATE_MSG) $currentTab = $mc->get($chatID.MC_DATE_MSG)
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
// two cases: change page or confirm date // two cases: change page or confirm date
if ($callback_data == '>') { if ($callback_data == '>') {
// add 1 to month // add 1 to month
@ -233,9 +235,9 @@ if (isset ($row->callback_query)) {
} }
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) $mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab)
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
$text = "Il calendario del ".$currentTab['month']."-".$currentTab['year']; $text = "Il calendario del ".$currentTab['month']."-".$currentTab['year'];
// get new inline container // get new inline container
} }
elseif ($callback_data == '<') { elseif ($callback_data == '<') {
@ -244,9 +246,9 @@ if (isset ($row->callback_query)) {
else { else {
$currentTab['month'] = 12; $currentTab['month'] = 12;
$currentTab['year'] -=1; $currentTab['year'] -=1;
} }
$text = "Il calendario del ".$currentTab['month']."-".$currentTab['year']; $text = "Il calendario del ".$currentTab['month']."-".$currentTab['year'];
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) $mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab)
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
} }
@ -266,59 +268,59 @@ if (isset ($row->callback_query)) {
error("Not a numerical day, how you did it?!?"); error("Not a numerical day, how you did it?!?");
$currentTab['day'] = $callback_data; $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"); $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 // Display time inline keyboard
$kbd = new InlineKbd; $kbd = new InlineKbd;
$firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']? $firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']?
date('G')+1 : 8; date('G')+1 : 8;
for ($i = 8; $i <= 21; $i++) { for ($i = 8; $i <= 21; $i++) {
if ($i < $firstHour) if ($i < $firstHour)
$kbd->insertItem(" ", "null"); $kbd->insertItem(" ", "null");
else else
$kbd->insertItem("$i".":00", $i); $kbd->insertItem("$i".":00", $i);
if ($i == 14) if ($i == 14)
$kbd->pushLine(); $kbd->pushLine();
} }
$kbd->pushLine(); $kbd->pushLine();
$kbd->insertItem("$EMOJI_X Annulla", MSG_ABORT); $kbd->insertItem("$EMOJI_X Annulla", MSG_ABORT);
$kbd->pushLine(); $kbd->pushLine();
$text="A che ora vuoi inviare il messaggio?"; $text="A che ora vuoi inviare il messaggio?";
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
//~ "&message_id=".$inlineID . //~ "&message_id=".$inlineID .
//~ "&text=".urlencode($text)."&reply_markup=".$reply; //~ "&text=".urlencode($text)."&reply_markup=".$reply;
//~ file_get_contents($url); //~ file_get_contents($url);
editMessageText($chatID, $inlineID, $text, "HTML", editMessageText($chatID, $inlineID, $text, "HTML",
false, $kbd->getKeyboard()); false, $kbd->getKeyboard());
$mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die(); $mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die();
break; break;
} }
// If answer is < or > remain in current state and update calendar // If answer is < or > remain in current state and update calendar
$reply = getCalendarTab($currentTab['month'], $currentTab['year']); $reply = getCalendarTab($currentTab['month'], $currentTab['year']);
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
//~ "&message_id=".$inlineID . //~ "&message_id=".$inlineID .
//~ "&text=".urlencode($text)."&reply_markup=".$reply; //~ "&text=".urlencode($text)."&reply_markup=".$reply;
//~ file_get_contents($url); //~ file_get_contents($url);
editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); editMessageText($chatID, $inlineID, $text, "HTML", false, $reply);
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID); $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);
break; break;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END STATE_WAIT_DATE * END STATE_WAIT_DATE
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* entering STATE_WAIT_TIME - callback * entering STATE_WAIT_TIME - callback
* you can: choose the time of scheduled message(s) * you can: choose the time of scheduled message(s)
@ -335,52 +337,51 @@ if (isset ($row->callback_query)) {
if (DEBUG) botlog("[EE] Not a numeric time\n"); if (DEBUG) botlog("[EE] Not a numeric time\n");
die(); die();
} }
$currentTab = $mc->get($chatID.MC_DATE_MSG); $currentTab = $mc->get($chatID.MC_DATE_MSG);
$msg = $mc->get($chatID.MC_FORWARD_MSG); $msg = $mc->get($chatID.MC_FORWARD_MSG);
// Message preview in scheduling list is not formatted! // Message preview in scheduling list is not formatted!
$text = htmlspecialchars($msg->message->text); $text = htmlspecialchars($msg->message->text);
$dateObj = DateTime::createFromFormat('Y-n-j-G', $currentTab['year'] . '-' . $currentTab['month'] . '-' . $currentTab['day'] . '-' . $callback_data);
$formattedDate = $dateObj->format('Y-m-d H:i:s');
// add to database (mysql) // add to database (mysql)
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); $sql = new SQLite3(DBFILE);
// Build up mysql query // Build up mysql query
$value = "("; $value = "(";
$value .= $msg->message->message_id; $value .= $msg->message->message_id;
$value .= ","; $value .= ",";
$value .= $msg->message->chat->id; $value .= $msg->message->chat->id;
$value .= ","; $value .= ",";
$value .= "'".$currentTab['year'].'-'; $value .= "'$formattedDate'";
$value .= $currentTab['month'].'-';
$value .= $currentTab['day'].' ';
$value .= $callback_data.":00:00'";
$value .= ","; $value .= ",";
if (isset($text)) if (isset($text))
$value .= "'".$sql->escape_string($text)."'"; $value .= "'".$sql->escapeString($text)."'";
else else
$value .= "''"; $value .= "''";
$value .= ","; $value .= ",";
if (isset($msg->message->forward_from->username)) if (isset($msg->message->forward_from->username))
$value .= "'".$sql->escape_string($msg->message->forward_from->username)."'"; $value .= "'".$sql->escapeString($msg->message->forward_from->username)."'";
elseif (isset($msg->message->from->username)) elseif (isset($msg->message->from->username))
$value .= "'".$sql->escape_string($msg->message->from->username)."'"; $value .= "'".$sql->escapeString($msg->message->from->username)."'";
else else
$value .= "''"; $value .= "''";
$value .= ");"; $value .= ");";
info("Saving post in ".MYSQL_TABLE.", scheduled date: ".$value); $query = "INSERT INTO telegram_post (MessageID,ChatID,DateTime,Text,Author) VALUES ".$value;
info('saving post in database, query: ' . $query);
$sql->query($query) or error("Can't make the query, SQL error ".$sql->lastErrorMsg());
$query = $sql->query("INSERT INTO ".MYSQL_TABLE." (MessageID,ChatID,DateTime,Text,Author) VALUE ".$value)
or error("Can't make the query, SQL error ".$sql->error);
$sql->close(); $sql->close();
$text = "Programmazione avvenuta con successo"; $text = "Programmazione avvenuta con successo";
editMessageText($chatID, $inlineID, $text); editMessageText($chatID, $inlineID, $text);
break; break;
} }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ -391,23 +392,21 @@ if (isset ($row->callback_query)) {
// HANDLE A COMMON MESSAGE (text/commands/photos...) // HANDLE A COMMON MESSAGE (text/commands/photos...)
elseif (isset($row->message)) { elseif (isset($row->message)) {
info('handling common message');
// Load variables // Load variables
$chatID = $row->message->chat->id; $chatID = $row->message->chat->id;
// Check if it was thrown by admitted users // Check if it was thrown by admitted users
if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) { if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) {
info( ' Telegram: forwarding to channel... '); info('authorized user ok');
forwardMessage(API_CHANNEL_ID, $row->message->chat->id, $row->message->message_id, FORWARD_SILENT);
}
else if (in_array($row->message->from->username, BOT_BETA_TESTERS)) {
// Finally, check actual state and do things // Finally, check actual state and do things
$status = $mc->get($chatID.MC_STATUS); $status = $mc->get($chatID.MC_STATUS);
if (!$status) { if (!$status) {
$mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE $mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE
$status = STATE_IDLE; $status = STATE_IDLE;
} }
if (DEBUG) info("Starting from status $status"); info("Starting from status $status");
// Reset CMD // Reset CMD
if (($command = getCommand($row->message))['command'] == '/reset') { if (($command = getCommand($row->message))['command'] == '/reset') {
warning("Reset command received"); warning("Reset command received");
@ -432,32 +431,32 @@ elseif (isset($row->message)) {
case STATE_IDLE: case STATE_IDLE:
if (($command = getCommand($row->message)) != null) { if (($command = getCommand($row->message)) != null) {
info("Command received: $command[command] with options $command[options]"); info("Command received: $command[command] with options $command[options]");
switch ($command['command']) { switch ($command['command']) {
case '/help': case '/help':
case '/start': case '/start':
// Send some explainations about this bot // Send some explainations about this bot
sendMessage($chatID,$WELCOME_MESSAGE, "HTML"); sendMessage($chatID,$WELCOME_MESSAGE, "HTML");
break; break;
case '/edit': case '/edit':
// not yet implemented // not yet implemented
sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su"); sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su");
break; break;
case '/list': case '/list':
$archivedMsg = navigateMsgArchive(0, $mc, $chatID); $archivedMsg = navigateMsgArchive(0, $mc, $chatID);
$answer = sendMessage($chatID, $archivedMsg['text'], "HTML", false, false, null, $archivedMsg['kbd']); $answer = sendMessage($chatID, $archivedMsg['text'], "HTML", false, false, null, $archivedMsg['kbd']);
$answer = json_decode($answer); $answer = json_decode($answer);
// Should forward the message if is not pure text // Should forward the message if is not pure text
//forwardMessage($chatID, $row['ChatID'], $row['MessageID']); //forwardMessage($chatID, $row['ChatID'], $row['MessageID']);
$mc->set($chatID.MC_INLINE_ID, $answer->result->message_id) $mc->set($chatID.MC_INLINE_ID, $answer->result->message_id)
or $mc->replace($chatID.MC_INLINE_ID, $answer->result->message_id); or $mc->replace($chatID.MC_INLINE_ID, $answer->result->message_id);
$mc->set($chatID.MC_LIST_NUMBER, 0) $mc->set($chatID.MC_LIST_NUMBER, 0)
or $mc->replace($chatID.MC_LIST_NUMBER, 0); or $mc->replace($chatID.MC_LIST_NUMBER, 0);
break; break;
@ -469,14 +468,14 @@ elseif (isset($row->message)) {
} }
else { // not a command (text?) else { // not a command (text?)
// you probably want to forward a message? // you probably want to forward a message?
// Check what kind of message is this // Check what kind of message is this
$kbd = new InlineKbd; $kbd = new InlineKbd;
$kbd->insertItem("SI ".$EMOJI_THUMBSUP,MSG_YES); $kbd->insertItem("SI ".$EMOJI_THUMBSUP,MSG_YES);
$kbd->insertItem("NO ".$EMOJI_THUMBSDOWN,MSG_NO); $kbd->insertItem("NO ".$EMOJI_THUMBSDOWN,MSG_NO);
$kbd->insertItem("Programma ".$EMOJI_CLOCK,MSG_SCHEDULE); $kbd->insertItem("Programma ".$EMOJI_CLOCK,MSG_SCHEDULE);
$kbd->pushLine(); $kbd->pushLine();
// This bot accepts only text, images, videos and locations // This bot accepts only text, images, videos and locations
if (!(isset($row->message->photo) or isset($row->message->location) if (!(isset($row->message->photo) or isset($row->message->location)
or isset($row->message->video) or isset($row->message->text) )) { or isset($row->message->video) or isset($row->message->text) )) {
@ -484,27 +483,27 @@ elseif (isset($row->message)) {
error("Not supported content"); error("Not supported content");
// Program stops here // Program stops here
} }
$mc->set($chatID.MC_FORWARD_MSG, $row) $mc->set($chatID.MC_FORWARD_MSG, $row)
or $mc->replace($chatID.MC_FORWARD_MSG, $row) or $mc->replace($chatID.MC_FORWARD_MSG, $row)
or error("Something is wrong with memcached"); or error("Something is wrong with memcached");
// SetUp inline messages (OK, NO, SCHEDULE) // SetUp inline messages (OK, NO, SCHEDULE)
$text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?"; $text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?";
$query = sendMessage($row->message->chat->id, $query = sendMessage($row->message->chat->id,
$text, $text,
null, false, false, null, null, false, false, null,
$kbd->getKeyboard()); $kbd->getKeyboard());
//~ $answer = file_get_contents($query); //~ $answer = file_get_contents($query);
$answer = json_decode($query); $answer = json_decode($query);
$inlineID = $answer->result->message_id; $inlineID = $answer->result->message_id;
if (DEBUG) info("Callback message id: $inlineID"); if (DEBUG) info("Callback message id: $inlineID");
$mc->replace($chatID.MC_STATUS, STATE_MSG_ANSWER); $mc->replace($chatID.MC_STATUS, STATE_MSG_ANSWER);
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID); $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);
} }
@ -512,7 +511,7 @@ elseif (isset($row->message)) {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END STATE_IDLE * END STATE_IDLE
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
default: default:
wrong_action($chatID); wrong_action($chatID);
} }

View File

@ -8,17 +8,16 @@ require_once('shared.php');
if (DEBUG) botlog("\n\n\n".'[II] Scheduler starting at ' . date('Y-m-d H:i:s') . ' ====================' . "\n\n"); if (DEBUG) botlog("\n\n\n".'[II] Scheduler starting at ' . date('Y-m-d H:i:s') . ' ====================' . "\n\n");
// Get current date in MySQL DATETIME default format // Get current date in sqlite datetime format
$dateID = date('Y-n-d G:00'); $dateID = date('Y-m-d H:i:s');
// Connect to MySQL DB // Connect to MySQL DB
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); $sql = new Sqlite3(DBFILE);
$query = $sql->query("SELECT MessageID,ChatID,DateTime,Text,Author FROM ".MYSQL_TABLE. $query = $sql->query("SELECT MessageID,ChatID,DateTime,Text,Author FROM telegram_post WHERE DateTime<='".$dateID."'")
" WHERE DateTime<='".$dateID."'")
or error("Can't make the query, SQL error ".$sql->error); or error("Can't make the query, SQL error ".$sql->error);
// Post all messages (only text) // Post all messages (only text)
while ($row = $query->fetch_assoc()) { while ($row = $query->fetchArray()) {
info("Publishing message $row[MessageID]"); info("Publishing message $row[MessageID]");
/* /*
$bot_query = API_URL . API_TOKEN . '/sendMessage?' . $bot_query = API_URL . API_TOKEN . '/sendMessage?' .
@ -32,7 +31,7 @@ while ($row = $query->fetch_assoc()) {
} }
// Remove just posted messages (and any eventual previous one) // Remove just posted messages (and possibly any previous one)
$sql->query("DELETE FROM ".MYSQL_TABLE." WHERE DateTime<='".$dateID."'"); $sql->query("DELETE FROM telegram_post WHERE DateTime<='".$dateID."'");
$sql->close(); $sql->close();
?> ?>

View File

@ -1,31 +1,29 @@
<?php <?php
require_once('config.php'); require_once('config.php');
// Base logging function. All logging functions use this.
// Change this to output everything on stdout, on file, on socket, on whathever you want...
function botlog($info) { function botlog($info) {
if ($file = fopen(LOGFILE, 'a')) error_log($info);
{
fwrite($file, $info);
fclose($file);
}
} }
// Unrecoverable error, program must be terminated // Unrecoverable error, program must be terminated
function error($message) { function error($message) {
if (DEBUG) botlog("[EE] $message\n"); if (DEBUG) botlog("[EE] $message");
if (DEBUG) botlog('[II] BOT request (cruently) ended at ' . date('Y-m-d H:i:s') . ' ====================' . "\n"); if (DEBUG) botlog('[II] BOT request died');
die(); die();
} }
// Uncommon event // Uncommon event
function warning($message) { function warning($message) {
if (DEBUG) botlog("[WW] $message\n"); if (DEBUG) botlog("[WW] $message");
} }
// Routine debugging informations // Routine debugging informations
function info($message) { function info($message) {
if (DEBUG) botlog("[II] $message\n"); if (DEBUG) botlog("[II] $message");
} }
// Interpreta un comando inviato al bot - mettere null o simili invece di -1 // Interpreta un comando inviato al bot - mettere null o simili invece di -1
function getCommand($messaggio, $NoOffset = true) { function getCommand($messaggio, $NoOffset = true) {
if (!isset($messaggio->entities)) if (!isset($messaggio->entities))
@ -44,40 +42,40 @@
} }
return null; return null;
} }
/**** Telegram query functions ****/ /**** Telegram query functions ****/
// return a message object, null if is error // return a message object, null if is error
function sendMessage($chat_id, $text, $parse_mode = null, function sendMessage($chat_id, $text, $parse_mode = null,
$disable_web_preview = false, $disable_web_preview = false,
$disable_notification = false, $disable_notification = false,
$reply_to_message_id = null, $reply_to_message_id = null,
$reply_markup = null) { $reply_markup = null) {
$query = API_URL.API_TOKEN."/sendMessage?chat_id=".$chat_id. $query = API_URL.API_TOKEN."/sendMessage?chat_id=".$chat_id.
"&text=".urlencode($text); "&text=".urlencode($text);
if ($parse_mode != null) $query .= "&parse_mode=$parse_mode"; if ($parse_mode != null) $query .= "&parse_mode=$parse_mode";
if ($reply_markup != null) $query .= "&reply_markup=$reply_markup"; if ($reply_markup != null) $query .= "&reply_markup=$reply_markup";
if ($disable_web_preview != false) $query .= "&disable_web_preview=$disable_web_preview"; if ($disable_web_preview != false) $query .= "&disable_web_preview=$disable_web_preview";
if ($disable_notification != false) $query .= "&disable_notification=$disable_notification"; 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_to_message_id != null) $query .= "&reply_to_message_id=$reply_to_message_id";
if ($reply_markup != null) $query .= "&reply_markup=$reply_markup"; if ($reply_markup != null) $query .= "&reply_markup=$reply_markup";
$answer = file_get_contents($query); $answer = file_get_contents($query);
if ($answer === false) warning("Something is wrong in sendMessage - chat_id: $chat_id - text: $text"); if ($answer === false) warning("Something is wrong in sendMessage - chat_id: $chat_id - text: $text");
return $answer; return $answer;
} }
// //
function forwardMessage($chat_id, $from_chat_id, function forwardMessage($chat_id, $from_chat_id,
$message_id, $message_id,
$disable_notification = null) { $disable_notification = null) {
$query = API_URL.API_TOKEN."/forwardMessage?" . $query = API_URL.API_TOKEN."/forwardMessage?" .
"chat_id=".urlencode($chat_id) . "chat_id=".urlencode($chat_id) .
"&from_chat_id=" . urlencode($from_chat_id) . "&from_chat_id=" . urlencode($from_chat_id) .
"&message_id=" . urlencode($message_id); "&message_id=" . urlencode($message_id);
if ($disable_notification != null) $query .= "&disable_notification=$disable_notification"; if ($disable_notification != null) $query .= "&disable_notification=$disable_notification";
$answer = file_get_contents($query); $answer = file_get_contents($query);
@ -93,110 +91,112 @@
$parse_mode = null, $parse_mode = null,
$disable_web_page_preview = false, $disable_web_page_preview = false,
$reply_markup = null) { $reply_markup = null) {
$query = API_URL . API_TOKEN . "/editMessageText?" . $query = API_URL . API_TOKEN . "/editMessageText?" .
"chat_id=".urlencode($chat_id) . "chat_id=".urlencode($chat_id) .
"&message_id=" . urlencode($message_id). "&message_id=" . urlencode($message_id).
"&text=" . urlencode($text); "&text=" . urlencode($text);
if ($parse_mode != null) $query .= "&parse_mode=$parse_mode"; if ($parse_mode != null) $query .= "&parse_mode=$parse_mode";
if ($disable_web_page_preview != false) $query .= "&disable_web_page_preview=$disable_web_page_preview"; if ($disable_web_page_preview != false) $query .= "&disable_web_page_preview=$disable_web_page_preview";
if ($reply_markup != null) $query .= "&reply_markup=$reply_markup"; if ($reply_markup != null) $query .= "&reply_markup=$reply_markup";
$answer = file_get_contents($query); $answer = file_get_contents($query);
if ($answer === false) warning("Something is wrong in editMessageText - chat_id: $chat_id - message_id: $message_id"); if ($answer === false) warning("Something is wrong in editMessageText - chat_id: $chat_id - message_id: $message_id");
info("edit message answer: $answer");
return $answer; return $answer;
} }
// //
function deleteMessage($chat_id, $message_id) { function deleteMessage($chat_id, $message_id) {
$query = API_URL . API_TOKEN . "/deleteMessage?" . $query = API_URL . API_TOKEN . "/deleteMessage?" .
"chat_id=".urlencode($chat_id) . "chat_id=".urlencode($chat_id) .
"&message_id=" . urlencode($message_id); "&message_id=" . urlencode($message_id);
$answer = file_get_contents($query); $answer = file_get_contents($query);
if ($answer === false) warning("Something is wrong in deleteMessage - chat_id: $chat_id - message_id: $message_id"); if ($answer === false) warning("Something is wrong in deleteMessage - chat_id: $chat_id - message_id: $message_id");
return $answer; return $answer;
} }
function sendPhoto ($chat_id, $photo, function sendPhoto ($chat_id, $photo,
$caption = null, $caption = null,
$reply_markup = null, $reply_markup = null,
$disable_notification = false, $disable_notification = false,
$reply_to_message_id = null ) { $reply_to_message_id = null ) {
$query = API_URL . API_TOKEN . "/sendPhoto?" . $query = API_URL . API_TOKEN . "/sendPhoto?" .
"chat_id=".urlencode($chat_id) . "chat_id=".urlencode($chat_id) .
"&photo=" . urlencode($photo); "&photo=" . urlencode($photo);
$query .= "&disable_notification=$disable_notification"; $query .= "&disable_notification=$disable_notification";
if ($reply_to_message_id != null) $query .= "&reply_to_message_id=$disable_web_page_preview"; 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 ($reply_markup != null) $query .= "&reply_markup=$reply_markup";
if ($caption != null) $query .= "&caption=$caption"; if ($caption != null) $query .= "&caption=$caption";
$answer = file_get_contents($query); $answer = file_get_contents($query);
} }
// Makes the calendar inline keyboard // Makes the calendar inline keyboard
function getCalendarTab ($month, $year) { function getCalendarTab ($month, $year) {
info('getting calendar...');
$kbd = new InlineKbd; $kbd = new InlineKbd;
$days = array("L", "M", "M", "G", "V", "S", "D"); $days = array("L", "M", "M", "G", "V", "S", "D");
for ($i = 0; $i < 7; $i++) for ($i = 0; $i < 7; $i++)
$kbd->insertItem("$days[$i]", "null"); $kbd->insertItem("$days[$i]", "null");
$kbd->pushLine(); $kbd->pushLine();
$daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year); $daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year);
$date = $year."-".$month; $date = $year."-".$month;
$isCurrentMonth = date('nY') == $month.$year; $isCurrentMonth = date('nY') == $month.$year;
$startingDay = $isCurrentMonth ? date('j') : 1; $startingDay = $isCurrentMonth ? date('j') : 1;
if (DEBUG) botlog("[II] First day: ".$date.'-'.$startingDay."\n"); info("[II] First day: ".$date.'-'.$startingDay."\n");
$firstDay = date("N", strtotime($date.'-'.$startingDay)); $firstDay = date("N", strtotime($date.'-'.$startingDay));
// blank buttons // blank buttons
for ($i = 1; $i < $firstDay; $i++) for ($i = 1; $i < $firstDay; $i++)
$kbd->insertItem(" ", "null"); $kbd->insertItem(" ", "null");
for ($numDay = $startingDay; $numDay <= $daysInMonth; $numDay++, $i++) { for ($numDay = $startingDay; $numDay <= $daysInMonth; $numDay++, $i++) {
$kbd->insertItem("$numDay", "$numDay"); $kbd->insertItem("$numDay", "$numDay");
if ($i >= 7) { if ($i >= 7) {
$kbd->pushLine(); $kbd->pushLine();
$i = 0; $i = 0;
} }
} }
// empty buttons // empty buttons
if ($i != 0) { if ($i != 0) {
for (; $i > 1 and $i <= 7; $i++) for (; $i > 1 and $i <= 7; $i++)
$kbd->insertItem(" ", "null"); $kbd->insertItem(" ", "null");
$kbd->pushLine(); $kbd->pushLine();
} }
if ($isCurrentMonth) if ($isCurrentMonth)
$kbd->insertItem(" ", "null"); $kbd->insertItem(" ", "null");
else else
$kbd->insertItem("<", "<"); $kbd->insertItem("<", "<");
$kbd->insertItem("$EMOJI_X Annulla",MSG_ABORT); $kbd->insertItem("$EMOJI_X Annulla",MSG_ABORT);
$kbd->insertItem(">", ">"); $kbd->insertItem(">", ">");
$kbd->pushLine(); $kbd->pushLine();
return $kbd->getKeyboard(); return $kbd->getKeyboard();
} }
// Function used to get an archived message // Function used to get an archived message
// and make an appropriate keyboard // and make an appropriate keyboard
// -- I dont' like so much -- // -- I dont' like so much --
function navigateMsgArchive ($msgNumber, $mc, $chatID) { function navigateMsgArchive ($msgNumber, $mc, $chatID) {
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); $sql = new Sqlite3(DBFILE);
$numbers = $sql->query("SELECT COUNT(*) FROM ".MYSQL_TABLE); $numbers = $sql->query("SELECT COUNT(*) FROM telegram_post");
$numbers = $numbers->fetch_array()[0]; $numbers = $numbers->fetchArray()[0];
if ($numbers == 0) { if ($numbers == 0) {
return array( "text" => "Nessun messaggio programmato", return array( "text" => "Nessun messaggio programmato",
"kbd" => null); "kbd" => null);
@ -207,17 +207,15 @@
//~ die(); //~ die();
} }
else { else {
$query = $sql->query("SELECT DATE_FORMAT(DateTime,'%d/%m/%Y ore %H') as DateTimeFormat,ChatID, MessageID,Text,Author,ID FROM ".MYSQL_TABLE." $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);
ORDER BY DateTime ASC LIMIT 1 OFFSET $msgNumber") or error("Can't make the query, SQL error ".$sql->error);
$row = $query->fetchArray();
//while($row = $query->fetch_assoc()) {
$row = $query->fetch_assoc();
$mc->set($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']) $mc->set($chatID.MC_DELETE_SCHEDULED_ID, $row['ID'])
or $mc->replace($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']); or $mc->replace($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']);
$sql->close(); $sql->close();
$kbd = new InlineKbd; $kbd = new InlineKbd;
$kbd->insertItem($row['DateTimeFormat'], "null"); $kbd->insertItem($row['DateTimeFormat'], "null");
$kbd->pushLine(); $kbd->pushLine();
@ -232,45 +230,45 @@
$kbd->insertItem(" ","null"); $kbd->insertItem(" ","null");
$kbd->pushLine(); $kbd->pushLine();
$kbd = $kbd->getKeyboard(); $kbd = $kbd->getKeyboard();
if ($row['Text'] != ''){ if ($row['Text'] != ''){
$text="$row[Text]\n<i>$row[Author]</i>"; // NOME CANALE ".API_CHANNEL_ID."?"; $text="$row[Text]\n<i>$row[Author]</i>"; // NOME CANALE ".API_CHANNEL_ID."?";
} }
else else
$text="Non testo puro"; $text="Non testo puro";
return array( "text" => $text, return array( "text" => $text,
"kbd" => $kbd); "kbd" => $kbd);
} }
} }
function not_authorized($chatID) { function not_authorized($chatID) {
$query = API_URL . API_TOKEN . '/sendMessage?' . $query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode($chatID) . 'chat_id=' . urlencode($chatID) .
"&text=".urlencode("Utente non autorizzato\nhttps://www.youtube.com/watch?v=HO8ctP_QNZc"); "&text=".urlencode("Questo bot è solo per il Consiglio Direttivo.\nForse cercavi @golem_empoli (canale) oppure https://golem.linux.it/ (sito)");
file_get_contents($query); file_get_contents($query);
die(); die();
} }
function wrong_action($chatID) { function wrong_action($chatID) {
$query = API_URL . API_TOKEN . '/sendMessage?' . $query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode($chatID) . 'chat_id=' . urlencode($chatID) .
"&text=Azione non corretta"; "&text=Azione non corretta";
file_get_contents($query); file_get_contents($query);
die(); die();
} }
class InlineKbd class InlineKbd
{ {
private $keyboard = array(); private $keyboard = array();
private $currentLine = array(); private $currentLine = array();
public function pushLine() { public function pushLine() {
$this->keyboard[] = $this->currentLine; $this->keyboard[] = $this->currentLine;
$this->currentLine = array(); $this->currentLine = array();
@ -282,7 +280,7 @@
); );
} }
public function getKeyboard() { public function getKeyboard() {
$kbd = array("inline_keyboard" => $this->keyboard); $kbd = array("inline_keyboard" => $this->keyboard);
return json_encode($kbd); return json_encode($kbd);
} }
} }