Better support for messages forwarding

This commit is contained in:
giuliof 2017-11-03 14:41:29 +01:00
parent 3c4ab2e0c9
commit 98a1573901
3 changed files with 48 additions and 21 deletions

View File

@ -31,6 +31,7 @@ $mc->addServer('localhost', MEMCACHED_PORT)
* **Generic message** * **Generic message**
* $row * $row
* |_ message § Message * |_ message § Message
* |_ message_id § integer
* |_ chat § chat * |_ chat § chat
* |_ id (chatID) § integer * |_ id (chatID) § integer
* |_ username (if set - user name for private chat, else channel or group name) § string * |_ username (if set - user name for private chat, else channel or group name) § string
@ -233,6 +234,11 @@ if (isset ($row->callback_query)) {
//~ '&text='.urlencode($msg->message->text."\n_".$msg->message->chat->username."_"). //~ '&text='.urlencode($msg->message->text."\n_".$msg->message->chat->username."_").
//~ "&parse_mode=".urlencode("Markdown"); //bah, un isset un ce lo vedrei male //~ "&parse_mode=".urlencode("Markdown"); //bah, un isset un ce lo vedrei male
//~ $result = file_get_contents($query); //~ $result = file_get_contents($query);
// A new test
forwardMessage(API_CHANNEL_ID, $msg->message->chat->id, $msg->message->message_id, true);
/*
if (isset($msg->message->text)) { if (isset($msg->message->text)) {
$result = sendMessage(API_CHANNEL_ID, $result = sendMessage(API_CHANNEL_ID,
$msg->message->text."\n_".$msg->message->chat->username."_", $msg->message->text."\n_".$msg->message->chat->username."_",
@ -243,7 +249,7 @@ if (isset ($row->callback_query)) {
sendPhoto(API_CHANNEL_ID,$msg->message->photo[0]->file_id, sendPhoto(API_CHANNEL_ID,$msg->message->photo[0]->file_id,
isset($msg->message->caption) ? $msg->message->caption : null); isset($msg->message->caption) ? $msg->message->caption : null);
} }
*/
// Remove kbd // Remove kbd
$text = "Invio effettuato correttamente"; $text = "Invio effettuato correttamente";
editMessageText($chatID, $inlineID, $text); editMessageText($chatID, $inlineID, $text);
@ -466,9 +472,17 @@ if (isset ($row->callback_query)) {
$value .= $currentTab['day'].' '; $value .= $currentTab['day'].' ';
$value .= $callback_data.":00:00'"; $value .= $callback_data.":00:00'";
$value .= ","; $value .= ",";
$value .= "'".$sql->escape_string($msg->message->text)."'"; if (isset($msg->message->text))
$value .= "'".$sql->escape_string($msg->message->text)."'";
else
$value .= "''";
$value .= ","; $value .= ",";
$value .= "'".$sql->escape_string($msg->message->from->username)."'"; // andrebbe fatto un isset, vabbé if (isset($msg->message->forward_from->username))
$value .= "'".$sql->escape_string($msg->message->forward_from->username)."'";
elseif (isset($msg->message->from->username))
$value .= "'".$sql->escape_string($msg->message->from->username)."'";
else
$value .= "''";
$value .= ");"; $value .= ");";
info("Saving post in ".MYSQL_TABLE.", scheduled date: ".$value); info("Saving post in ".MYSQL_TABLE.", scheduled date: ".$value);
@ -550,7 +564,7 @@ elseif (isset($row->message)) {
die(); die();
} }
$query = $sql->query("SELECT DATE_FORMAT(DateTime,'%d/%m/%Y ore %H') as DateTime,Text,Author,ID FROM ".MYSQL_TABLE." $query = $sql->query("SELECT DATE_FORMAT(DateTime,'%d/%m/%Y ore %H') as ChatID, MessageID,DateTime,Text,Author,ID FROM ".MYSQL_TABLE."
ORDER BY DateTime ASC LIMIT 1") or error("Can't make the query, SQL error ".$sql->error); ORDER BY DateTime ASC LIMIT 1") or error("Can't make the query, SQL error ".$sql->error);
//while($row = $query->fetch_assoc()) { //while($row = $query->fetch_assoc()) {
@ -592,15 +606,21 @@ elseif (isset($row->message)) {
"inline_keyboard" => $container); "inline_keyboard" => $container);
$reply = json_encode($resp); $reply = json_encode($resp);
$text="$row[Text]\n_$row[Author]_"; // NOME CANALE ".API_CHANNEL_ID."?"; if ($row[Text] != ''){
$text="$row[Text]\n_$row[Author]_"; // NOME CANALE ".API_CHANNEL_ID."?";
}
else
$text="Non testo puro";
$query = API_URL.API_TOKEN."/sendmessage?chat_id=".($chatID). $query = API_URL.API_TOKEN."/sendmessage?chat_id=".($chatID).
"&text=".urlencode($text). "&text=".urlencode($text).
"&parse_mode=".urlencode("Markdown"). "&parse_mode=".urlencode("Markdown").
"&reply_markup=".$reply; "&reply_markup=".$reply;
$answer = file_get_contents($query); $answer = file_get_contents($query);
$answer = json_decode($answer); $answer = json_decode($answer);
forwardMessage(API_CHANNEL_ID, $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);
@ -624,6 +644,10 @@ elseif (isset($row->message)) {
array( array(
"text" => "NO ".$EMOJI_THUMBSDOWN, "text" => "NO ".$EMOJI_THUMBSDOWN,
"callback_data" => MSG_NO "callback_data" => MSG_NO
),
array(
"text" => "Programma ".$EMOJI_CLOCK,
"callback_data" => MSG_SCHEDULE
) )
); );
@ -640,11 +664,7 @@ elseif (isset($row->message)) {
} }
// A common text message // A common text message
elseif (isset($row->message->text)) { elseif (isset($row->message->text)) {
$container[0][] = //.... nothing
array(
"text" => "Programma ".$EMOJI_CLOCK,
"callback_data" => MSG_SCHEDULE
);
} }
// else... this is not supported // else... this is not supported
else { else {

View File

@ -4,27 +4,31 @@
*/ */
require_once('config.php'); require_once('config.php');
require_once('functions.php'); 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 MySQL DATETIME default format
$dateID = date('Y-n-j G:*'); $dateID = date('Y-n-d G:00');
// Connect to MySQL DB // Connect to MySQL DB
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); $sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
$query = $sql->query("SELECT DateTime,Text,Author FROM ".MYSQL_TABLE. $query = $sql->query("SELECT MessageID,ChatID,DateTime,Text,Author FROM ".MYSQL_TABLE.
"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->fetch_assoc()) {
info("Publishing message $row[MessageID]"); info("Publishing message $row[MessageID]");
/*
$bot_query = API_URL . API_TOKEN . '/sendMessage?' . $bot_query = API_URL . API_TOKEN . '/sendMessage?' .
'chat_id=' . urlencode(API_CHANNEL_ID) . 'chat_id=' . urlencode(API_CHANNEL_ID) .
'&text=' . urlencode($row['Text']."\n_".$row['Author']."_"). '&text=' . urlencode($row['Text']."\n_".$row['Author']."_").
"&parse_mode=".urlencode("Markdown"); "&parse_mode=".urlencode("Markdown");
file_get_contents($bot_query); file_get_contents($bot_query);
* */
forwardMessage(API_CHANNEL_ID, $row['ChatID'], $row['MessageID']);
} }
// Remove just posted messages (and any eventual previous one) // Remove just posted messages (and any eventual previous one)

View File

@ -70,9 +70,9 @@
// //
function forwardMessage($chat_id, $from_chat_id, function forwardMessage($chat_id, $from_chat_id,
$message_id, $message_id,
$disable_notification = false) { $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);
@ -80,7 +80,10 @@
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);
if ($answer === false) warning("Something is wrong in forwardMessage - chat_id: $chat_id - message_id: $message_id"); if ($answer === false) {
warning("Something is wrong in forwardMessage - chat_id: $chat_id - message_id: $message_id");
warning($answer);
}
return $answer; return $answer;
} }