Refactor readEmails script
[SonOfLokstallBot.git] / purjolok.php
index 1678cb2..185da44 100644 (file)
@@ -1,19 +1,17 @@
 <?php declare(strict_types=1);
 
-require 'common.php';
-
-use function Common\{
-        getTelegram,
-        getMessagesFromInbox,
-        getInbox,
-        splitBill,
-        getMessageSender,
-        getMessageSenderDisplayName,
-        canChatWith
-};
+require_once('common.php');
+
 use Telegram\Bot\Actions;
 use Telegram\Bot\Commands\Command;
 
+if(!canChatWith(getTelegram()->getWebhookUpdates())) {
+    getTelegram()->sendMessage([
+        'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('from')->get('id'),
+        'text' => "Sorry, Dad says I can't talk to you."
+    ]);
+    exit(0);
+}
 
 getTelegram()->addCommand(
     new class extends Command {
@@ -58,11 +56,6 @@ getTelegram()->addCommand(
         protected $description = 'List my bills';
 
         public function handle($arguments) {
-            if (!canChatWith($this->getUpdate())) {
-                $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
-                return;
-            }
-
             $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate())  . "'s unpaid bills. Just a sec ..."]);
             $this->replyWithChatAction(['action' => Actions::TYPING]);
             $this->replyWithMessage([
@@ -98,11 +91,6 @@ getTelegram()->addCommand(
         protected $description = 'Mark a bill as paid';
 
         public function handle($arguments) {
-            if (!canChatWith($this->getUpdate())) {
-                $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
-                return;
-            }
-
             if (!$arguments) {
                 $this->replyWithMessage(['text' => "I need the bill id. The /mybills command lists all your bills with the ids in brackets. Here, I'll show you..."]);
                 $this->triggerCommand('mybills');