Double bot interface, silent flag
This commit is contained in:
parent
d889e59a31
commit
d0d6964400
|
@ -8,12 +8,16 @@
|
|||
const API_CHANNEL_ID = '@';
|
||||
/* Users whose messages are forwarded to mailing list and channel */
|
||||
const BOT_ALLOWED_USERS = array ();
|
||||
/* Users who can use the new bot interface */
|
||||
const BOT_BETA_TESTERS = array ();
|
||||
/* Address to forward to */
|
||||
const MAIL_TO_ADDR = 'example@example.com';
|
||||
/* Bot email identity */
|
||||
const MAIL_FROM_ADDR = 'example@example.com';
|
||||
/* Debug messages on log file */
|
||||
const DEBUG = true;
|
||||
/* Be silent when forwarding/writing messages to channel */
|
||||
const FORWARD_SILENT = true;
|
||||
/* Log file name */
|
||||
const LOGFILE = 'botlog.log';
|
||||
/* MySQL informations */
|
||||
|
|
237
main.php
237
main.php
|
@ -61,7 +61,7 @@ if (isset ($row->callback_query)) {
|
|||
$chatID = $row->callback_query->message->chat->id;
|
||||
|
||||
// Check if it was thrown by admitted users
|
||||
in_array($row->callback_query->message->chat->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
|
||||
in_array($row->callback_query->message->chat->username, BOT_BETA_TESTERS) or not_authorized($chatID);
|
||||
|
||||
// Answer to the callback query (remove the loading sign)
|
||||
if (DEBUG) info("Callback Query, answering");
|
||||
|
@ -168,7 +168,7 @@ if (isset ($row->callback_query)) {
|
|||
$msg = $mc->get($chatID.MC_FORWARD_MSG)
|
||||
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
|
||||
$text = "Invio effettuato correttamente";
|
||||
|
@ -256,10 +256,7 @@ if (isset ($row->callback_query)) {
|
|||
}
|
||||
elseif ($callback_data == MSG_ABORT) {
|
||||
$text = "Invio annullato correttamente";
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
editMessageText($chatID, $inlineID, $text);
|
||||
$mc->replace($chatID.MC_STATUS, STATE_IDLE) or die();
|
||||
die();
|
||||
}
|
||||
|
@ -330,10 +327,7 @@ if (isset ($row->callback_query)) {
|
|||
$mc->replace($chatID.MC_STATUS, STATE_IDLE);
|
||||
if ($callback_data == MSG_ABORT) {
|
||||
$text = "Invio annullato correttamente";
|
||||
$url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
|
||||
"&message_id=".$inlineID .
|
||||
"&text=".urlencode($text);
|
||||
file_get_contents($url);
|
||||
editMessageText($chatID, $inlineID, $text);
|
||||
$mc->replace($chatID.MC_STATUS, STATE_IDLE) or die();
|
||||
die();
|
||||
}
|
||||
|
@ -400,125 +394,132 @@ elseif (isset($row->message)) {
|
|||
// Load variables
|
||||
$chatID = $row->message->chat->id;
|
||||
// Check if it was thrown by admitted users
|
||||
in_array($row->message->from->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
|
||||
|
||||
// Finally, check actual state and do things
|
||||
$status = $mc->get($chatID.MC_STATUS);
|
||||
if (!$status) {
|
||||
$mc->set($chatID.MC_STATUS, STATE_IDLE) or die(); // set status or force it to STATE_IDLE
|
||||
$status = STATE_IDLE;
|
||||
if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) {
|
||||
info( ' Telegram: forwarding to channel... ');
|
||||
forwardMessage(API_CHANNEL_ID, $row->message->chat->id, $row->message->message_id, FORWARD_SILENT);
|
||||
}
|
||||
if (DEBUG) info("Starting from status $status");
|
||||
|
||||
// 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]);
|
||||
}
|
||||
else if (in_array($row->message->from->username, BOT_BETA_TESTERS)) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
sendMessage($chatID, "Reset completato");
|
||||
}
|
||||
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]");
|
||||
|
||||
switch ($command['command']) {
|
||||
case '/help':
|
||||
case '/start':
|
||||
// Send some explainations about this bot
|
||||
sendMessage($chatID,$WELCOME_MESSAGE, "HTML");
|
||||
break;
|
||||
if (DEBUG) info("Starting from status $status");
|
||||
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
sendMessage($chatID, "Reset completato");
|
||||
}
|
||||
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':
|
||||
// not yet implemented
|
||||
sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su");
|
||||
break;
|
||||
|
||||
case '/list':
|
||||
|
||||
$archivedMsg = navigateMsgArchive(0, $mc, $chatID);
|
||||
switch ($command['command']) {
|
||||
case '/help':
|
||||
case '/start':
|
||||
// Send some explainations about this bot
|
||||
sendMessage($chatID,$WELCOME_MESSAGE, "HTML");
|
||||
break;
|
||||
|
||||
$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']);
|
||||
case '/edit':
|
||||
// not yet implemented
|
||||
sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su");
|
||||
break;
|
||||
|
||||
case '/list':
|
||||
|
||||
$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;
|
||||
$archivedMsg = navigateMsgArchive(0, $mc, $chatID);
|
||||
|
||||
$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']);
|
||||
|
||||
|
||||
$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:
|
||||
sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help");
|
||||
warning("$command[command] is not a valid command");
|
||||
default:
|
||||
sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help");
|
||||
warning("$command[command] is not a valid command");
|
||||
}
|
||||
}
|
||||
}
|
||||
else { // not a command (text?)
|
||||
// you probably want to forward a message?
|
||||
|
||||
// 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();
|
||||
|
||||
// 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
|
||||
else { // not a command (text?)
|
||||
// you probably want to forward a message?
|
||||
|
||||
// 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();
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
$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;
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* END STATE_IDLE
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
default:
|
||||
wrong_action($chatID);
|
||||
break;
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* END STATE_IDLE
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
default:
|
||||
wrong_action($chatID);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
not_authorized($chatID);
|
||||
}
|
||||
// a not supported message (i.e. from a channel)
|
||||
else {
|
||||
|
|
|
@ -27,7 +27,7 @@ while ($row = $query->fetch_assoc()) {
|
|||
"&parse_mode=".urlencode("Markdown");
|
||||
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");
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue