2017-09-24 20:23:45 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2020-12-05 18:28:53 +00:00
|
|
|
* codice schifo del GOLEMbot - dal 28 set 2017
|
|
|
|
*
|
|
|
|
* Opera by giomba, lucam e giuliof released under GPLv3 license
|
|
|
|
*
|
2017-09-24 20:23:45 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-05 21:51:01 +00:00
|
|
|
require_once('/data/config.php');
|
2017-09-24 20:23:45 +00:00
|
|
|
require_once('shared.php');
|
|
|
|
|
|
|
|
// === BEGIN ===
|
|
|
|
|
2020-12-05 18:28:53 +00:00
|
|
|
DEBUG and botlog('[II] BOT request starting at ' . date('Y-m-d H:i:s'));
|
2017-09-24 20:23:45 +00:00
|
|
|
|
|
|
|
$json = file_get_contents('php://input');
|
|
|
|
$row = json_decode($json);
|
|
|
|
|
2020-12-05 18:28:53 +00:00
|
|
|
DEBUG and botlog("[II] Incoming JSON ".$json."\n");
|
2017-09-24 20:23:45 +00:00
|
|
|
|
|
|
|
// Start up Memcached
|
|
|
|
$mc = new Memcached();
|
2020-12-05 18:28:53 +00:00
|
|
|
if ($mc == null) error('no memcached object instance');
|
|
|
|
if (! $mc->addServer(MEMCACHED_HOST, MEMCACHED_PORT)) {
|
|
|
|
error("something is wrong connecting to memcached daemon");
|
|
|
|
}
|
2017-09-24 20:23:45 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// Structure of managed telegram messages
|
|
|
|
/*
|
|
|
|
* **Generic message**
|
|
|
|
* $row
|
|
|
|
* |_ message § Message
|
2017-11-03 13:41:29 +00:00
|
|
|
* |_ message_id § integer
|
2017-11-02 20:08:09 +00:00
|
|
|
* |_ chat § chat
|
|
|
|
* |_ id (chatID) § integer
|
|
|
|
* |_ username (if set - user name for private chat, else channel or group name) § string
|
|
|
|
* |_ ...not relevant things...
|
|
|
|
* |_ from § User
|
|
|
|
* |_ id (userID) § integer
|
|
|
|
* |_ username (if set - user name) § string
|
|
|
|
* |_ ....
|
|
|
|
* |_ forward_from § User
|
|
|
|
* |_ forward_from_chat § Chat
|
|
|
|
* |_ text § string (message_text)
|
2017-11-25 15:58:29 +00:00
|
|
|
* |_ entities: [{"offset":...,"length":...,"type":"italic"}]
|
2017-11-02 20:08:09 +00:00
|
|
|
* |_ photo, location, video
|
2020-12-05 18:28:53 +00:00
|
|
|
*
|
2017-11-02 20:08:09 +00:00
|
|
|
* **Callback Query**
|
|
|
|
* $row
|
|
|
|
* |_ callback_query
|
|
|
|
* |_ id § integer
|
|
|
|
* |_ from § User
|
|
|
|
* |_ message § Message
|
|
|
|
* |_ inline_message_id § integer (inlineID)
|
|
|
|
* |_ ...
|
2020-12-05 18:28:53 +00:00
|
|
|
*/
|
2017-11-02 20:08:09 +00:00
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
// HANDLE A CALLBACK
|
2017-11-02 20:08:09 +00:00
|
|
|
if (isset ($row->callback_query)) {
|
2020-12-05 18:28:53 +00:00
|
|
|
DEBUG and botlog('handle callback query');
|
2017-09-24 20:23:45 +00:00
|
|
|
$chatID = $row->callback_query->message->chat->id;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
// Check if it was thrown by admitted users
|
2020-12-05 18:28:53 +00:00
|
|
|
in_array($row->callback_query->message->chat->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
|
|
|
|
botlog('Ok authorized user pass!');
|
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
// Answer to the callback query (remove the loading sign)
|
2020-12-05 18:28:53 +00:00
|
|
|
info("Callback Query, answering");
|
2017-09-24 20:23:45 +00:00
|
|
|
$url = API_URL.API_TOKEN."/answerCallbackQuery?callback_query_id=".$row->callback_query->id;
|
|
|
|
file_get_contents($url);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
// Do something - insert here useful variables
|
|
|
|
$callback_data = $row->callback_query->data;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
$inlineID = $row->callback_query->message->message_id;
|
|
|
|
$inlineID_old = $mc->get($chatID.MC_INLINE_ID);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// Finally, check actual state and do things
|
|
|
|
$status = $mc->get($chatID.MC_STATUS);
|
|
|
|
if (!$status) {
|
|
|
|
$mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE
|
|
|
|
$status = STATE_IDLE;
|
|
|
|
}
|
|
|
|
if (DEBUG) info("Starting from status $status");
|
2017-09-24 20:23:45 +00:00
|
|
|
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// Callback state machine
|
|
|
|
switch ($status) {
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* entering STATE_IDLE - callback *
|
|
|
|
* you can: list and delete messages given by /list *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
case STATE_IDLE:
|
|
|
|
switch ($callback_data){
|
|
|
|
case MSG_DELETE:
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-26 17:12:06 +00:00
|
|
|
// remove current message
|
2020-12-05 18:28:53 +00:00
|
|
|
$sql = new Sqlite3(DBFILE);
|
2017-11-06 08:23:41 +00:00
|
|
|
$IDdelete = $mc->get($chatID.MC_DELETE_SCHEDULED_ID) or error("No ID to delete");
|
2020-12-05 18:28:53 +00:00
|
|
|
$sql->query("DELETE FROM telegram_post WHERE ID=$IDdelete")
|
|
|
|
or error("Can't make the query, SQL error ".$sql->lastErrorMsg());
|
|
|
|
|
2017-12-26 17:12:06 +00:00
|
|
|
$sql->close();
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-21 22:54:42 +00:00
|
|
|
$offset = $mc->get($chatID.MC_LIST_NUMBER);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-26 17:12:06 +00:00
|
|
|
if ($offset > 0) {
|
2017-11-21 22:54:42 +00:00
|
|
|
$offset--;
|
|
|
|
}
|
2017-11-02 20:08:09 +00:00
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
$offset = $mc->get($chatID.MC_LIST_NUMBER);
|
2017-12-26 17:12:06 +00:00
|
|
|
$offset++; // nobody cares if goes over # of programmed messages
|
2017-11-02 20:08:09 +00:00
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
$offset = $mc->get($chatID.MC_LIST_NUMBER);
|
2017-12-26 17:12:06 +00:00
|
|
|
if ($offset > 0)
|
|
|
|
$offset--;
|
2017-11-02 20:08:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (DEBUG) warning("Callback request is not accepted in status $status");
|
|
|
|
die();
|
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-26 17:12:06 +00:00
|
|
|
$archivedMsg = navigateMsgArchive($offset, $mc, $chatID);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-26 17:12:06 +00:00
|
|
|
$answer = editMessageText($chatID, $inlineID, $archivedMsg['text'],
|
|
|
|
"HTML", false, $archivedMsg['kbd'] );
|
2017-12-16 22:58:36 +00:00
|
|
|
$answer = json_decode($answer);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-16 22:58:36 +00:00
|
|
|
$mc->set($chatID.MC_LIST_NUMBER, $offset)
|
|
|
|
or $mc->replace($chatID.MC_LIST_NUMBER, $offset);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-16 22:58:36 +00:00
|
|
|
die();
|
2017-11-02 20:08:09 +00:00
|
|
|
break;
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* END STATE_IDLE
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* entering STATE_MSG_ANSWER - callback
|
|
|
|
* you can: (after you sent a message) choose if forward to
|
|
|
|
* channel or schedule it
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
case STATE_MSG_ANSWER: // only accessible by callback
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
/////// spostare di là
|
|
|
|
if (!(isset($callback_data))) {
|
|
|
|
//$mc->replace($chatID.MC_STATUS, STATE_IDLE);
|
|
|
|
wrong_action($chatID);
|
|
|
|
die();
|
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
switch ($callback_data) {
|
|
|
|
case MSG_YES:
|
|
|
|
// forward previously sent message
|
|
|
|
// set status?
|
|
|
|
$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");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
forwardMessage(API_CHANNEL_ID, $msg->message->chat->id, $msg->message->message_id, FORWARD_SILENT);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// Remove kbd
|
|
|
|
$text = "Invio effettuato correttamente";
|
|
|
|
editMessageText($chatID, $inlineID, $text);
|
|
|
|
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
|
|
|
//~ "&message_id=".$inlineID .
|
|
|
|
//~ "&text=".urlencode($text);
|
|
|
|
//~ file_get_contents($url);
|
|
|
|
break;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
case MSG_NO:
|
|
|
|
$status = $mc->replace($chatID.MC_STATUS, STATE_IDLE)
|
|
|
|
or error("Something is wrong with memcached");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// destroy saved message?
|
|
|
|
$text = "Invio annullato correttamente";
|
|
|
|
editMessageText($chatID, $inlineID, $text);
|
|
|
|
break;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
case MSG_SCHEDULE:
|
2020-12-05 18:28:53 +00:00
|
|
|
// == from here can be put in a function ==
|
|
|
|
info('status MSG_SCHEDULE');
|
|
|
|
$status = $mc->replace($chatID.MC_STATUS, STATE_WAIT_DATE) or error(__FILE__ . __LINE__);
|
|
|
|
$currentTab = array(
|
2017-11-02 20:08:09 +00:00
|
|
|
'month' => date('n'),
|
|
|
|
'year' => date('Y'),
|
|
|
|
'day' => null
|
|
|
|
);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-16 22:58:36 +00:00
|
|
|
$reply = getCalendarTab($currentTab['month'], $currentTab['year']);
|
2020-12-05 18:28:53 +00:00
|
|
|
info('calendar tab: ' . $reply);
|
|
|
|
|
|
|
|
$mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or error(__FILE__ . __LINE__);
|
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$text="Quando vuoi inviare il messaggio?";
|
2017-11-25 15:58:29 +00:00
|
|
|
editMessageText($chatID, $inlineID, $text, "HTML", false, $reply);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID)
|
|
|
|
or error("Something is wrong with memcached");
|
|
|
|
// == end ==
|
|
|
|
break;
|
2017-09-24 20:23:45 +00:00
|
|
|
}
|
2017-11-02 20:08:09 +00:00
|
|
|
break;
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* END STATE_MSG_ANSWER
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* entering STATE_WAIT_DATE - callback
|
|
|
|
* you can: choose the date of scheduled message(s)
|
2020-12-05 18:28:53 +00:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2017-11-02 20:08:09 +00:00
|
|
|
case STATE_WAIT_DATE:
|
|
|
|
$currentTab = $mc->get($chatID.MC_DATE_MSG)
|
2017-09-24 20:23:45 +00:00
|
|
|
or error("Something is wrong with memcached");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// 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");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$text = "Il calendario del ".$currentTab['month']."-".$currentTab['year'];
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// get new inline container
|
|
|
|
}
|
|
|
|
elseif ($callback_data == '<') {
|
|
|
|
if ($currentTab['month'] > 1)
|
|
|
|
$currentTab['month'] -=1;
|
|
|
|
else {
|
|
|
|
$currentTab['month'] = 12;
|
|
|
|
$currentTab['year'] -=1;
|
2020-12-05 18:28:53 +00:00
|
|
|
}
|
2017-11-02 20:08:09 +00:00
|
|
|
$text = "Il calendario del ".$currentTab['month']."-".$currentTab['year'];
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$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) {
|
2017-09-24 20:23:45 +00:00
|
|
|
$text = "Invio annullato correttamente";
|
2018-01-01 21:09:45 +00:00
|
|
|
editMessageText($chatID, $inlineID, $text);
|
2017-11-02 20:08:09 +00:00
|
|
|
$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");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// Display time inline keyboard
|
2017-12-16 22:58:36 +00:00
|
|
|
$kbd = new InlineKbd;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']?
|
|
|
|
date('G')+1 : 8;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
for ($i = 8; $i <= 21; $i++) {
|
2017-12-16 22:58:36 +00:00
|
|
|
if ($i < $firstHour)
|
2017-12-26 16:35:14 +00:00
|
|
|
$kbd->insertItem(" ", "null");
|
2017-12-16 22:58:36 +00:00
|
|
|
else
|
|
|
|
$kbd->insertItem("$i".":00", $i);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-16 22:58:36 +00:00
|
|
|
if ($i == 14)
|
|
|
|
$kbd->pushLine();
|
2017-11-02 20:08:09 +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
|
|
|
$kbd->insertItem("$EMOJI_X Annulla", MSG_ABORT);
|
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-11-02 20:08:09 +00:00
|
|
|
$text="A che ora vuoi inviare il messaggio?";
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
|
|
|
//~ "&message_id=".$inlineID .
|
|
|
|
//~ "&text=".urlencode($text)."&reply_markup=".$reply;
|
|
|
|
//~ file_get_contents($url);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-12-16 22:58:36 +00:00
|
|
|
editMessageText($chatID, $inlineID, $text, "HTML",
|
|
|
|
false, $kbd->getKeyboard());
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die();
|
2017-09-24 20:23:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// If answer is < or > remain in current state and update calendar
|
2017-12-16 22:58:36 +00:00
|
|
|
$reply = getCalendarTab($currentTab['month'], $currentTab['year']);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
|
|
|
//~ "&message_id=".$inlineID .
|
|
|
|
//~ "&text=".urlencode($text)."&reply_markup=".$reply;
|
|
|
|
//~ file_get_contents($url);
|
2017-11-25 15:58:29 +00:00
|
|
|
editMessageText($chatID, $inlineID, $text, "HTML", false, $reply);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
break;
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* END STATE_WAIT_DATE
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* entering STATE_WAIT_TIME - callback
|
|
|
|
* you can: choose the time of scheduled message(s)
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
case STATE_WAIT_TIME:
|
|
|
|
$mc->replace($chatID.MC_STATUS, STATE_IDLE);
|
|
|
|
if ($callback_data == MSG_ABORT) {
|
|
|
|
$text = "Invio annullato correttamente";
|
2018-01-01 21:09:45 +00:00
|
|
|
editMessageText($chatID, $inlineID, $text);
|
2017-11-02 20:08:09 +00:00
|
|
|
$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();
|
2017-09-24 20:23:45 +00:00
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$currentTab = $mc->get($chatID.MC_DATE_MSG);
|
|
|
|
$msg = $mc->get($chatID.MC_FORWARD_MSG);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
|
|
|
|
2017-12-16 22:58:36 +00:00
|
|
|
// Message preview in scheduling list is not formatted!
|
|
|
|
$text = htmlspecialchars($msg->message->text);
|
|
|
|
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2020-12-05 21:51:01 +00:00
|
|
|
$dateObj = DateTime::createFromFormat('Y-n-j-G-e', $currentTab['year'] . '-' . $currentTab['month'] . '-' . $currentTab['day'] . '-' . $callback_data . '-Europe/Rome');
|
|
|
|
$dateObj->setTimezone(new DateTimeZone('UTC'));
|
2020-12-05 18:28:53 +00:00
|
|
|
$formattedDate = $dateObj->format('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
// add to database (mysql)
|
2020-12-05 18:28:53 +00:00
|
|
|
$sql = new SQLite3(DBFILE);
|
2017-11-02 20:08:09 +00:00
|
|
|
// Build up mysql query
|
|
|
|
$value = "(";
|
|
|
|
$value .= $msg->message->message_id;
|
|
|
|
$value .= ",";
|
|
|
|
$value .= $msg->message->chat->id;
|
|
|
|
$value .= ",";
|
2020-12-05 18:28:53 +00:00
|
|
|
$value .= "'$formattedDate'";
|
2017-11-02 20:08:09 +00:00
|
|
|
$value .= ",";
|
2017-11-25 15:58:29 +00:00
|
|
|
if (isset($text))
|
2020-12-05 18:28:53 +00:00
|
|
|
$value .= "'".$sql->escapeString($text)."'";
|
2017-11-03 13:41:29 +00:00
|
|
|
else
|
|
|
|
$value .= "''";
|
2017-11-02 20:08:09 +00:00
|
|
|
$value .= ",";
|
2017-11-03 13:41:29 +00:00
|
|
|
if (isset($msg->message->forward_from->username))
|
2020-12-05 18:28:53 +00:00
|
|
|
$value .= "'".$sql->escapeString($msg->message->forward_from->username)."'";
|
2017-11-03 13:41:29 +00:00
|
|
|
elseif (isset($msg->message->from->username))
|
2020-12-05 18:28:53 +00:00
|
|
|
$value .= "'".$sql->escapeString($msg->message->from->username)."'";
|
2017-11-03 13:41:29 +00:00
|
|
|
else
|
2020-12-05 18:28:53 +00:00
|
|
|
$value .= "''";
|
2017-11-02 20:08:09 +00:00
|
|
|
$value .= ");";
|
2020-12-05 18:28:53 +00:00
|
|
|
|
|
|
|
$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());
|
2017-11-02 20:08:09 +00:00
|
|
|
$sql->close();
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
$text = "Programmazione avvenuta con successo";
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-11-02 20:08:09 +00:00
|
|
|
editMessageText($chatID, $inlineID, $text);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2017-09-24 20:23:45 +00:00
|
|
|
break;
|
2017-11-02 20:08:09 +00:00
|
|
|
}
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* END STATE_WAIT_TIME
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// HANDLE A COMMON MESSAGE (text/commands/photos...)
|
|
|
|
elseif (isset($row->message)) {
|
2020-12-05 18:28:53 +00:00
|
|
|
info('handling common message');
|
2017-11-02 20:08:09 +00:00
|
|
|
// Load variables
|
|
|
|
$chatID = $row->message->chat->id;
|
|
|
|
// Check if it was thrown by admitted users
|
2020-12-05 18:28:53 +00:00
|
|
|
if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) {
|
|
|
|
info('authorized user ok');
|
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
// Finally, check actual state and do things
|
|
|
|
$status = $mc->get($chatID.MC_STATUS);
|
|
|
|
if (!$status) {
|
|
|
|
$mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE
|
|
|
|
$status = STATE_IDLE;
|
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
info("Starting from status $status");
|
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
// Reset CMD
|
|
|
|
if (($command = getCommand($row->message))['command'] == '/reset') {
|
|
|
|
warning("Reset command received");
|
|
|
|
$all_keys = $mc->getAllKeys();
|
|
|
|
foreach ($all_keys as $index => $key) {
|
|
|
|
if (strpos($key,$chatID) !== 0) {
|
|
|
|
$mc->delete($key);
|
|
|
|
info("Deleting $key");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unset($all_keys[$index]);
|
|
|
|
}
|
2017-12-26 16:35:14 +00:00
|
|
|
}
|
2018-01-01 21:09:45 +00:00
|
|
|
sendMessage($chatID, "Reset completato");
|
2017-12-26 16:35:14 +00:00
|
|
|
}
|
2018-01-01 21:09:45 +00:00
|
|
|
else {
|
|
|
|
switch($status) {
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* entering STATE_IDLE - message *
|
|
|
|
* you can: give a command, send messages to forward *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
case STATE_IDLE:
|
|
|
|
if (($command = getCommand($row->message)) != null) {
|
|
|
|
info("Command received: $command[command] with options $command[options]");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
switch ($command['command']) {
|
|
|
|
case '/help':
|
|
|
|
case '/start':
|
|
|
|
// Send some explainations about this bot
|
|
|
|
sendMessage($chatID,$WELCOME_MESSAGE, "HTML");
|
|
|
|
break;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
case '/edit':
|
|
|
|
// not yet implemented
|
|
|
|
sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su");
|
|
|
|
break;
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
case '/list':
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$archivedMsg = navigateMsgArchive(0, $mc, $chatID);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$answer = sendMessage($chatID, $archivedMsg['text'], "HTML", false, false, null, $archivedMsg['kbd']);
|
|
|
|
$answer = json_decode($answer);
|
|
|
|
// Should forward the message if is not pure text
|
|
|
|
//forwardMessage($chatID, $row['ChatID'], $row['MessageID']);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$mc->set($chatID.MC_INLINE_ID, $answer->result->message_id)
|
|
|
|
or $mc->replace($chatID.MC_INLINE_ID, $answer->result->message_id);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$mc->set($chatID.MC_LIST_NUMBER, 0)
|
|
|
|
or $mc->replace($chatID.MC_LIST_NUMBER, 0);
|
|
|
|
break;
|
2017-12-26 16:35:14 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
default:
|
|
|
|
sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help");
|
|
|
|
warning("$command[command] is not a valid command");
|
|
|
|
}
|
2017-12-26 16:35:14 +00:00
|
|
|
}
|
2018-01-01 21:09:45 +00:00
|
|
|
else { // not a command (text?)
|
|
|
|
// you probably want to forward a message?
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
// Check what kind of message is this
|
|
|
|
$kbd = new InlineKbd;
|
|
|
|
$kbd->insertItem("SI ".$EMOJI_THUMBSUP,MSG_YES);
|
|
|
|
$kbd->insertItem("NO ".$EMOJI_THUMBSDOWN,MSG_NO);
|
|
|
|
$kbd->insertItem("Programma ".$EMOJI_CLOCK,MSG_SCHEDULE);
|
|
|
|
$kbd->pushLine();
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
// This bot accepts only text, images, videos and locations
|
|
|
|
if (!(isset($row->message->photo) or isset($row->message->location)
|
|
|
|
or isset($row->message->video) or isset($row->message->text) )) {
|
|
|
|
sendMessage($chatID, "Questa roba non va bene");
|
|
|
|
error("Not supported content");
|
|
|
|
// Program stops here
|
|
|
|
}
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$mc->set($chatID.MC_FORWARD_MSG, $row)
|
|
|
|
or $mc->replace($chatID.MC_FORWARD_MSG, $row)
|
|
|
|
or error("Something is wrong with memcached");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
// SetUp inline messages (OK, NO, SCHEDULE)
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?";
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$query = sendMessage($row->message->chat->id,
|
|
|
|
$text,
|
|
|
|
null, false, false, null,
|
|
|
|
$kbd->getKeyboard());
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
//~ $answer = file_get_contents($query);
|
|
|
|
$answer = json_decode($query);
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$inlineID = $answer->result->message_id;
|
|
|
|
if (DEBUG) info("Callback message id: $inlineID");
|
2020-12-05 18:28:53 +00:00
|
|
|
|
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
$mc->replace($chatID.MC_STATUS, STATE_MSG_ANSWER);
|
|
|
|
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);
|
2017-12-26 16:35:14 +00:00
|
|
|
}
|
2018-01-01 21:09:45 +00:00
|
|
|
break;
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* END STATE_IDLE
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2020-12-05 18:28:53 +00:00
|
|
|
|
2018-01-01 21:09:45 +00:00
|
|
|
default:
|
|
|
|
wrong_action($chatID);
|
|
|
|
}
|
2017-12-26 16:35:14 +00:00
|
|
|
}
|
2017-11-02 20:08:09 +00:00
|
|
|
}
|
2018-01-01 21:09:45 +00:00
|
|
|
else
|
|
|
|
not_authorized($chatID);
|
2017-11-02 20:08:09 +00:00
|
|
|
}
|
|
|
|
// a not supported message (i.e. from a channel)
|
|
|
|
else {
|
|
|
|
error("This is not a supported request. Please check JSON dump");
|
2017-09-24 20:23:45 +00:00
|
|
|
}
|
|
|
|
?>
|