Double bot interface, silent flag

This commit is contained in:
giuliof 2018-01-01 22:09:45 +01:00
parent d889e59a31
commit d0d6964400
3 changed files with 124 additions and 119 deletions

View File

@ -8,12 +8,16 @@
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 */
const MAIL_FROM_ADDR = 'example@example.com'; const MAIL_FROM_ADDR = 'example@example.com';
/* Debug messages on log file */ /* Debug messages on log file */
const DEBUG = true; const DEBUG = true;
/* Be silent when forwarding/writing messages to channel */
const FORWARD_SILENT = true;
/* Log file name */ /* Log file name */
const LOGFILE = 'botlog.log'; const LOGFILE = 'botlog.log';
/* MySQL informations */ /* MySQL informations */

237
main.php
View File

@ -61,7 +61,7 @@ if (isset ($row->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_ALLOWED_USERS) or not_authorized($chatID); in_array($row->callback_query->message->chat->username, BOT_BETA_TESTERS) or not_authorized($chatID);
// Answer to the callback query (remove the loading sign) // Answer to the callback query (remove the loading sign)
if (DEBUG) info("Callback Query, answering"); if (DEBUG) info("Callback Query, answering");
@ -168,7 +168,7 @@ if (isset ($row->callback_query)) {
$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, true); 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";
@ -256,10 +256,7 @@ if (isset ($row->callback_query)) {
} }
elseif ($callback_data == MSG_ABORT) { elseif ($callback_data == MSG_ABORT) {
$text = "Invio annullato correttamente"; $text = "Invio annullato correttamente";
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). editMessageText($chatID, $inlineID, $text);
"&message_id=".$inlineID .
"&text=".urlencode($text);
file_get_contents($url);
$mc->replace($chatID.MC_STATUS, STATE_IDLE) or die(); $mc->replace($chatID.MC_STATUS, STATE_IDLE) or die();
die(); die();
} }
@ -330,10 +327,7 @@ if (isset ($row->callback_query)) {
$mc->replace($chatID.MC_STATUS, STATE_IDLE); $mc->replace($chatID.MC_STATUS, STATE_IDLE);
if ($callback_data == MSG_ABORT) { if ($callback_data == MSG_ABORT) {
$text = "Invio annullato correttamente"; $text = "Invio annullato correttamente";
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). editMessageText($chatID, $inlineID, $text);
"&message_id=".$inlineID .
"&text=".urlencode($text);
file_get_contents($url);
$mc->replace($chatID.MC_STATUS, STATE_IDLE) or die(); $mc->replace($chatID.MC_STATUS, STATE_IDLE) or die();
die(); die();
} }
@ -400,125 +394,132 @@ elseif (isset($row->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
in_array($row->message->from->username, BOT_ALLOWED_USERS) or not_authorized($chatID); if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) {
info( ' Telegram: forwarding to channel... ');
// Finally, check actual state and do things forwardMessage(API_CHANNEL_ID, $row->message->chat->id, $row->message->message_id, FORWARD_SILENT);
$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"); else if (in_array($row->message->from->username, BOT_BETA_TESTERS)) {
// Reset CMD // Finally, check actual state and do things
if (($command = getCommand($row->message))['command'] == '/reset') { $status = $mc->get($chatID.MC_STATUS);
warning("Reset command received"); if (!$status) {
$all_keys = $mc->getAllKeys(); $mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE
foreach ($all_keys as $index => $key) { $status = STATE_IDLE;
if (strpos($key,$chatID) !== 0) {
$mc->delete($key);
info("Deleting $key");
}
else {
unset($all_keys[$index]);
}
} }
sendMessage($chatID, "Reset completato"); if (DEBUG) info("Starting from status $status");
}
else { // Reset CMD
switch($status) { if (($command = getCommand($row->message))['command'] == '/reset') {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * warning("Reset command received");
* entering STATE_IDLE - message * $all_keys = $mc->getAllKeys();
* you can: give a command, send messages to forward * foreach ($all_keys as $index => $key) {
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ if (strpos($key,$chatID) !== 0) {
case STATE_IDLE: $mc->delete($key);
if (($command = getCommand($row->message)) != null) { info("Deleting $key");
info("Command received: $command[command] with options $command[options]"); }
else {
switch ($command['command']) { unset($all_keys[$index]);
case '/help': }
case '/start': }
// Send some explainations about this bot sendMessage($chatID, "Reset completato");
sendMessage($chatID,$WELCOME_MESSAGE, "HTML"); }
break; 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]");
case '/edit': switch ($command['command']) {
// not yet implemented case '/help':
sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su"); case '/start':
break; // Send some explainations about this bot
sendMessage($chatID,$WELCOME_MESSAGE, "HTML");
case '/list': break;
$archivedMsg = navigateMsgArchive(0, $mc, $chatID);
$answer = sendMessage($chatID, $archivedMsg['text'], "HTML", false, false, null, $archivedMsg['kbd']); case '/edit':
$answer = json_decode($answer); // not yet implemented
// Should forward the message if is not pure text sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su");
//forwardMessage($chatID, $row['ChatID'], $row['MessageID']); break;
case '/list':
$mc->set($chatID.MC_INLINE_ID, $answer->result->message_id) $archivedMsg = navigateMsgArchive(0, $mc, $chatID);
or $mc->replace($chatID.MC_INLINE_ID, $answer->result->message_id);
$answer = sendMessage($chatID, $archivedMsg['text'], "HTML", false, false, null, $archivedMsg['kbd']);
$mc->set($chatID.MC_LIST_NUMBER, 0) $answer = json_decode($answer);
or $mc->replace($chatID.MC_LIST_NUMBER, 0); // Should forward the message if is not pure text
break; //forwardMessage($chatID, $row['ChatID'], $row['MessageID']);
$mc->set($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)
or $mc->replace($chatID.MC_LIST_NUMBER, 0);
break;
default: default:
sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help"); sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help");
warning("$command[command] is not a valid command"); warning("$command[command] is not a valid command");
}
} }
} 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) )) { sendMessage($chatID, "Questa roba non va bene");
sendMessage($chatID, "Questa roba non va bene"); error("Not supported content");
error("Not supported content"); // Program stops here
// Program stops here }
$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)
$text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?";
$query = sendMessage($row->message->chat->id,
$text,
null, false, false, null,
$kbd->getKeyboard());
//~ $answer = file_get_contents($query);
$answer = json_decode($query);
$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;
$mc->set($chatID.MC_FORWARD_MSG, $row) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
or $mc->replace($chatID.MC_FORWARD_MSG, $row) * END STATE_IDLE
or error("Something is wrong with memcached"); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// SetUp inline messages (OK, NO, SCHEDULE) default:
wrong_action($chatID);
$text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?"; }
$query = sendMessage($row->message->chat->id,
$text,
null, false, false, null,
$kbd->getKeyboard());
//~ $answer = file_get_contents($query);
$answer = json_decode($query);
$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;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END STATE_IDLE
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
default:
wrong_action($chatID);
} }
} }
else
not_authorized($chatID);
} }
// a not supported message (i.e. from a channel) // a not supported message (i.e. from a channel)
else { else {

View File

@ -27,7 +27,7 @@ while ($row = $query->fetch_assoc()) {
"&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']) or forwardMessage(API_CHANNEL_ID, $row['ChatID'], $row['MessageID'], FORWARD_SILENT) or
error("Scheduler couldn't forward the message. I'll try next hour"); error("Scheduler couldn't forward the message. I'll try next hour");
} }